This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLogger.cs
More file actions
47 lines (40 loc) · 2.18 KB
/
Logger.cs
File metadata and controls
47 lines (40 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Colorful;
using System.Drawing;
using Console = Colorful.Console;
namespace UnmanagedString
{
public static class Logger
{
private const string MessageStyle = "[{0}] [{1}] {2}"; // The format for log messages
private static void Log(ReadOnlySpan<char> message, string level, Color levelColor)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message)); // Ensure the message is not null
}
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // Get current timestamp
// Explicitly define the type of the array (because C# loves strong typing!)
var replacements = new Formatter[]
{
new Formatter(timestamp, Color.Gray), // Timestamp in gray
new Formatter($" {level}", levelColor), // Log level in specified color
new Formatter(message.ToString(), Color.White) // Actual message in white
};
// Log the message with formatting (like magic, but with colors!)
Console.WriteLineFormatted(MessageStyle, Color.Gray, replacements);
}
public static void Information(ReadOnlySpan<char> message) => Log(message, "INFO", Color.LightGreen); // Informational message
public static void Success(ReadOnlySpan<char> message) => Log(message, "SUCCESS", Color.Green); // Success message (yay!)
public static void Warning(ReadOnlySpan<char> message) => Log(message, "WARNING", Color.Yellow); // Warning message (uh-oh!)
public static void Error(ReadOnlySpan<char> message) => Log(message, "ERROR", Color.Red); // Error message (time to panic!)
public static void Skipped(ReadOnlySpan<char> message) => Log(message, "SKIPPED", Color.DarkGray); // Skipped message (because we can!)
public static void Exception(Exception ex)
{
if (ex == null)
{
throw new ArgumentNullException(nameof(ex)); // Ensure the exception is not null
}
Log(ex.Message.AsSpan(), "EXCEPTION", Color.Red); // Log the exception message
}
}
}