Home > Uncategorized > Simple #logging in C#

Simple #logging in C#

6I3oEOP4

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!

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: