Simple #logging in C#
If you have some software in C# that runs unattended, or doesn’t have a graphical interface, then hunting down problems with it, can be a pain, and a simple text logger comes to the rescue. – So at least you can check a log file to see what went wrong.
Writing a text logger isn’t complicated, in fact, it’s probably only a handful of lines of code, but it’s not something that a client or your boss is going to pay you for, it’s just a diagnostic tool, that adds nothing to the end user experience. So, it needs to be super-simple to add and use.
My favourite, so far is SimpleLogger, which you can install via nuget here;
Install-Package SimpleLogger
Then to use it, you firstly configure it as follows;
Logger.LoggerHandlerManager
.AddHandler(new ConsoleLoggerHandler())
.AddHandler(new FileLoggerHandler())
.AddHandler(new DebugConsoleLoggerHandler());
Then write out your progress as follows;
Logger.Log(strProgress);
And it outputs to the console, and to a file, which is date stamped, so you can look back over it.
The only downside is, that I guess there is no limit to the log file sizes, I’m not sure how to do cleanup, in case you write too much to the file. – And I did notice, that if you call this in a multi-threaded environment, if you get a collision (two logs at once), the logger will crash.
Anyway, top marks for simplicity!