Click here to Skip to main content
15,881,715 members
Articles / All Topics

Inventing Log Module

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Feb 2010CPOL 6.3K   2  
Inventing log module

This week, I’ve added Log module to LiveUI and I would like to share my experience. I should mention firstly that I was very busy and I did not want to spend more than one hour for the whole thing. It was reasonable deadline because functionality I needed was very simple: I wanted several methods to write arguments they receive to the text file, file name and format should have been configurable. Firstly, I’ve invented syntax I would like to use.

Let’s assume, we need to log MyMethod calls of MyType, I wanted code to look like:

Java
public class MyType {
  public void MyMethod(int arg1, int arg2) {
    Log.Get(this).Message("method call: MyMethod({0}, {1})", arg1, arg2);
    ...
  }
}

After that, I needed some logging framework and adapter to my syntax. Let’s talk about adapter first, I’ve followed the straightforward way and created interface ILogProvider.

Java
public interface ILogProvider {
  Log GetLog<TContext>(TContext context)
}

public abstract class Log
{
  public abstract Message(...);
  public abstract Warning(...);
  public abstract Error(...)

  public static Log Get<TContext>(TContext context)
  {
    ILogProvider logProvider = ...; // Nothing interesting there.
    returnlogProvider.GetLog<TContext>(context);
  }
}

The only thing left is to implement ILogProvider. I had been choosing between log4net and NLog for implementation (both are open source and popular). Comparing code and configuration samples, I’ve chosen NLog because its samples do not contain ugly words like “NullAppender” and it has better web site (yes, it does matter) :) That’s how NLog based ILogProvider implementation looks:

Java
public class NLogProvider : ILogProvider {
  public Log GetLog<TContext>(TContext context) {
    return NLog<TContext>.Instance;
  }
}

public class NLog<T> : Log 
{
  public static readonly NLog<T> Instance = new NLog<T>();

  private Logger logger;

  public override Message(string format, args) {
    logger.Info(...);
  }  

  public NLog(){
    logger = LogManager.GetLogger(typeof(T)));
  }
}

That’s it. After that, we can configure logging using Nlog configuration section as follows:

XML
<nlog>
  <logger name="MyNamespace.MyType" minlevel="Debug" writeTo="MyLog" />
  <target name="MyLog" xsi:type="File" filename="${basepath}/log.txt"/> 
</nlog>
This article was originally posted at http://alexilyin.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Xtensive
Russian Federation Russian Federation
I've been working at Xtensive company for 3 years.
My current project is LiveUI web framework which should make everybody happy. If I can be of any help to you feel free to contact me alexandr.ilyin at gmail.com.

By the way, I have a blog.


Comments and Discussions

 
-- There are no messages in this forum --