Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2010
Views 2,804
Bookmarked 2 times

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
A Technical Blog article. View original blog here.[^]

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:

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 straightforward way and created interface ILogProvider

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

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 like following

<nlog>
  <logger name="MyNamespace.MyType" minlevel="Debug" writeTo="MyLog" />
  <target name="MyLog" xsi:type="File" filename="${basepath}/log.txt"/> 
</nlog>

License

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

About the Author

Alexandr Sergeevich Ilyin

Software Developer
Xtensive
Russian Federation Russian Federation

Member

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.



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 1 Feb 2010
Article Copyright 2010 by Alexandr Sergeevich Ilyin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid