Pattern for Logging block

Topics: Logging Application Block
Jul 29, 2010 at 1:46 PM
hi folks, on our current project we decide to use enterprise library for loggin, upto now we used only log4net, but I can not find any advice how to really use the library in flexible way, what I mean is following in log4net this would be the way //define this in MyClass private static readonly ILog log = LogManager.GetLogger(typeof(MyClass)); //and than in the methods you can call it like this log.Info("info message"); this would enable me to go to application later and by chanign configuration I could turn info/warn levelels for particular classes without touching the application. The only sample of logging library is basically this: LogEntry logEntry = new LogEntry(); logEntry.EventId = 100; logEntry.Priority = 2; logEntry.Message = "Informational message"; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); Logger.Write(logEntry); but than I would be bound to filter based on some strings with typos and similar, and every developer would introduce his category-tokens, what is the best way to limit this, what are best practices about constructing messages, on another side it is not very developer friendly to place 10 lines for single log message, using some static methods for whole solution would reduce flexibility in filtering, so what is the best way to use logging? Is microsoft using this internally, if yes how? thanks almir
Jul 30, 2010 at 3:25 AM

The Logger.Write method has various overloads including one where you could simply pass the message you want to log.  Using that overload logs the message using the trace listeners defined under the default category you set.  There are other overloads which would not require you to create a LogEntry object, check those out. 

On the concern regarding possible typo errors on specifying categories, I would agree that's a valid point.  I'd probably define an enumeration for this and make sure to inform the developers about this. 

With regards to constructing messages, you could consider the following:

1. If the only information you need to be logged is the message itself, just go for using the overload passing the actual message or if necessary, with the addition of the category.

2. If you need other things, explore the other constructors of a LogEntry to avoid using 10 lines of code repeatedly.

3. If none of the constructors suit your need, consider creating a helper class that provides you with methods that encapsulates the logic of creating a LogEntry object parameterizing those values which you want to actually use. 

By the way, if you're using EntLib 5.0, it's encouraged that you use the LogWriter class instead of the Logger static class.

LogWriter logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
logWriter.Write(message, category);

The overloads of the Logger.Write and LogWriter.Write are pretty much the same.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.