Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Gain Control of your .NET Logging Using log4net : Page 3

Don't build logging capabilities for your applications from scratch; you can get robust and flexible logging functionality for your .NET applications with the free open source log4net framework, and then extend it to support custom needs.


advertisement
Custom Loggers: Adding Message Attributes
Another more advanced customization is required to implement a custom logging interface to allow adding of additional attributes or tags to each message logged. A possible scenario is an application component that's required to provide a CustomerId in with a logging call. This requires extending ILog interface, a custom Logger, and a custom LogManager that will create an instance of the logger. All three tasks are very straight forward.

I've based this example on the log4net documentation for a template (see the downloadable code for this article). Look at log4net\extensions\net\1.0\log4net.Ext.EventID\cs\src\ for the custom logger extension code.

Start by creating a public interface named ICustomLog that inherits from ILog. Add the definitions for the Debug(), Info(), Warn(), Error(), and Fatal() methods by using the following signatures

void Debug(stringCustomerId, object message); void Debug(stringCustomerId, object message, Exception t);

Next, using the EventIDLogImpl.cs sample file as a base, implement a CustomLogImpl class. In this class, declare a static variable called ThisDeclaringType as follows:

private readonly static Type ThisDeclaringType = typeof(CustomLogImpl);

Then implement the ICustomLog member methods using the following template



public void Debug(string CustomerId, object message) { Debug(CustomerId, message, null); } public void Debug(string CustomerId, object message, Exception t) { if (this.IsDebugEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Debug, message, t); loggingEvent.Properties["CustomerId"] = CustomerId; Logger.Log(loggingEvent); } }

Change this.IsDebugEnabled and Level.Debug to the appropriate values for the other functions.

The last class you'll need to implement is a CustomLogManager. Because the implementation is not exactly straightforward, I recommend that you make a copy of the EventIDLogManager class from the sample directory, and then run a search-and-replace command, changing "EventID" to "Custom." The code below illustrates the type (although not the full extent) of the required changes.

public class EventIDLogManager { #region Constructor /// <summary> /// Private constructor to prevent object creation /// </summary> private EventIDLogManager() { } #endregion // Becomes... public class CustomLogManager { #region Constructor /// <summary> /// Private constructor to prevent object creation /// </summary> private CustomLogManager() { } #endregion

Using a custom logger is as easy as using the original logger that comes with log4net. The only real difference is that you declare a log variable of the CustomLogManager type and use ICustomLog interfaces, for example:

private static readonly ICustomLog log = CustomLogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

A number of logging frameworks exist on the market, some with commercial backing, and some with open source origins. Log4net is an extremely powerful logging framework that has been proven time and time again in large production environments. It is highly scalable, extensible, and it's a logging option that makes it possible to easily implement flexible and reliable logging in all your applications.



Timur Fanshteyn is a senior architect at Bank of America Securities Prime Brokerage. He specializes in enterprise development projects using Microsoft technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date