Monday, October 31, 2005

Enabling log4net in .NET 2.0

I am just getting into using log4net, and for the most part I like what I see.  It works with .NET 2.0, but it has a couple of gotchas that do not seem to be documented anywhere.  Here they are:

For ASP.NET 2.0 web sites, since there is no longer an AssemblyInfo.cs file, enable log4net by telling it to search the Web.config file for the appender configuration inside Global.asax:

protected void Application_Start(Object sender, EventArgs e) {
	log4net.Config.DOMConfigurator.Configure();
}

For .NET 2.0 class libraries, test projects  and applications, call this same method where your application starts, or in some static method for your test project or class library.

5 comments:

  1. hi, do u know how to use log4net on a c# program? I'm trying to make a GUI application to analyze log files...and I think lo4net would really help me in this case

    ReplyDelete
  2. Just put the Configure method call in your C# program somewhere before you start logging. If that doesn't do it for you, if you can give more detail about what you're doing perhaps we can figure something out.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I have been using log4net.Config.XmlConfigurator.Configure(); for a while. Not sure which one is the right one. Good post BTW :)

    ReplyDelete
  5. thanks. it worked for me. Great post

    ReplyDelete