Template App 0.0.1
Template App Description
Loading...
Searching...
No Matches
Logger.cs
Go to the documentation of this file.
1using Serilog;
2
3namespace Util
4{
8 public class Logger
9 {
13 public bool logToFile = false;
14
20 public Logger(string filename, bool toFile = true)
21 {
22 if (toFile)
23 {
24 Log.Logger = new LoggerConfiguration().WriteTo.File(filename).CreateLogger();
25 logToFile = true;
26 }
27 }
28
34 public void info(string info, bool writeToConsole = true)
35 {
36 if (logToFile) Log.Information(info);
37 if (writeToConsole)
38 {
39 Console.WriteLine(info);
40 }
41 }
42
48 public void info(string info, ConsoleColor color)
49 {
50 if (logToFile) Log.Information(info);
51 Console.ForegroundColor = color;
52 Console.WriteLine(info);
53 Console.ResetColor();
54 }
55
61 public void warning(string warning, bool writeToConsole = true)
62 {
63 if (logToFile) Log.Warning(warning);
64 if (writeToConsole)
65 {
66 Console.ForegroundColor = ConsoleColor.Red;
67 Console.WriteLine(warning);
68 Console.ResetColor();
69 }
70 }
71
77 public void error(string error, bool writeToConsole = true)
78 {
79 if (logToFile) Log.Error(error);
80 if (writeToConsole)
81 {
82 Console.ForegroundColor = ConsoleColor.Red;
83 Console.WriteLine(error);
84 Console.ResetColor();
85 }
86 }
87
93 public void success(string success, bool writeToConsole = true)
94 {
95 if (logToFile) Log.Information(success);
96 if (writeToConsole)
97 {
98 Console.ForegroundColor = ConsoleColor.Green;
99 Console.WriteLine(success);
100 Console.ResetColor();
101 }
102 }
103
107 public void dispose()
108 {
109 if (logToFile) Log.CloseAndFlush();
110 }
111 }
112}
Class for logging messages to a file.
Definition Logger.cs:9
Logger(string filename, bool toFile=true)
Logger constructor.
Definition Logger.cs:20
bool logToFile
If messages should be written to file or only printed to stdout.
Definition Logger.cs:13
void dispose()
Close and flush the logger.
Definition Logger.cs:107
void info(string info, ConsoleColor color)
Log information and print to stdout with specified color.
Definition Logger.cs:48
void success(string success, bool writeToConsole=true)
Log success.
Definition Logger.cs:93
void error(string error, bool writeToConsole=true)
Log error.
Definition Logger.cs:77
void info(string info, bool writeToConsole=true)
Log information.
Definition Logger.cs:34
void warning(string warning, bool writeToConsole=true)
Log warning.
Definition Logger.cs:61
Definition Logger.cs:4