Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would a Log4Net Wrapper class look like?

I have been looking for a logging framework for .net (c#) and decided to give log4net a go after reading up on a few question/answer threads here on stackoverflow. I see people mentioning over and over that they use a wrapper class for log4net and I am wonder what that would look like.

I have my code split up into different projects (data access/business/webservice/..). How would a log4net wrapper class look like? Would the wrapper class need to be included in all of the projects? Should I build it as a separate project all together?

Should the wrapper be a singleton class?

like image 350
Xerx Avatar asked Oct 03 '08 11:10

Xerx


1 Answers

Essentially you create an interface and then a concrete implementation of that interface that wraps the classes and methods of Log4net directly. Additional logging systems can be wrapped by creating more concrete classes which wrap other classes and methods of those systems. Finally use a factory to create instances of your wrappers based on a configuration setting or line of code change. (Note: you can get more flexible - and complex - using an Inversion of Control container such as StructureMap.)

public interface ILogger {     void Debug(object message);     bool IsDebugEnabled { get; }      // continue for all methods like Error, Fatal ... }  public class Log4NetWrapper : ILogger {     private readonly log4net.ILog _logger;      public Log4NetWrapper(Type type)     {         _logger = log4net.LogManager.GetLogger(type);     }      public void Debug(object message)     {         _logger.Debug(message);     }      public bool IsDebugEnabled     {         get { return _logger.IsDebugEnabled; }     }      // complete ILogger interface implementation }  public static class LogManager {     public static ILogger GetLogger(Type type)     {         // if configuration file says log4net...         return new Log4NetWrapper(type);         // if it says Joe's Logger...         // return new JoesLoggerWrapper(type);     } } 

And an example of using this code in your classes (declared as a static readonly field):

private static readonly ILogger _logger =     LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 

You can get the same slightly more performance friendly effect using:

private static readonly ILogger _logger =      LogManager.GetLogger(typeof(YourTypeName)); 

The former example is considered more maintainable.

You would not want to create a Singleton to handle all logging because Log4Net logs for the invoking type; its much cleaner and useful to have each type use its own logger rather than just seeing a single type in the log file reporting all messages.

Because your implementation should be fairly reusable (other projects in your organization) you could make it its own assembly or ideally include it with your own personal/organization's framework/utility assembly. Do not re-declare the classes separately in each of your business/data/UI assemblies, that's not maintainable.

like image 84
cfeduke Avatar answered Sep 19 '22 10:09

cfeduke