Geeks With Blogs

@bsherwin

Brian Sherwin's Blog Moving at the Speed of .Net

I was working with log4net recently and had put a lot of the configuration information in the web.config file for an ASP.NET 2.0 web site.  Ideally, this was not what I wanted because I want to be able to change the configuration of the log4net without recycling the web application (a side effect of changing the web.config file).  After spending a fair amount of time reading out on the web--a lot of people said it can't be done. It took me piecing together multiple settings to finally get this to work.

However, I have the following dilemmas:

  • First, I didn't find all of the information in one place.
  • Second, I didn't bookmark the information I did find to give proper credit where it is due.

Therefore, if this has a resemblance to your work--I'm sorry--but I wanted to write this post to put it all in one place.

So my goal was to put it here in one place.

Step 1:

Create the log4net.config file.  That's right, break out the configuration into a file (call it whatever you want, but I call it log4net.config). Copy all of your log4net configuration settings out of your web.config file and put them here. (Don't just copy and paste--cut out everything related to log4net from the web.config file).

 

<?xml version="1.0"?>
<log4net>
  <appender name="UdpAppender" type="log4net.Appender.UdpAppender">
    <remoteAddress value="127.0.0.1" />
    <remotePort value="8080" />
    <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
      <locationInfo value="true" />
    </layout>
  </appender>
  <root>
    <!-- OFF, FATAL, ERROR, WARN, DEBUG, INFO, ALL -->
    <level value="ALL" />
    <appender-ref ref="UdpAppender" />
  </root>
</log4net>

Step 2:

Tell the application to configure log4net using this config file. There are really two spots for this. First, the global.asax and second the assemblyInfo.cs file. Note, that most of the time you will start out with a global.asax file with all of the code inline. For whatever reason, the only way I could get this to work was to break the global.asax up to use a code-behind and then ass the assemblyInfo.cs file.  So it ends up looking like this.

global.asax:

<%@ Application Language="C#" Inherits="GlobalAsax" %>

global.asax.cs (in your App_Code folder):

using System;
using System.Web;

public class GlobalAsax : HttpApplication
{
    // you may have lots of other code here
    void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}
Now that you have your application calling log4net's configuration, you can set an attribute in your assembly info so that log4net knows where to look for the configuration file.

AssemblyInfo.cs (in your App_Code folder):

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

The watch flag tells log4net to keep an eye on the configuration file for potential changes.  This is helpful if you want to change the configuration from logging everthing to errors only during the middle of your testing.

Step 3:

Log to your hearts content! Now that you have your configuration set up, start logging everything from Information to Debug to Fatal Errors. This sure beats doing "Response.WriteLine"

BTW, the reason I posted the UdpAppender, was that I have found this one to be incredibly useful when using Apache's "Chainsaw" log reader to view your logging without having to keep searching through log files.

Technorati Tags: ,
Posted on Friday, February 15, 2008 12:43 PM | Back to top


Comments on this post: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Brian,

Thanks for the tip.
It works like a charm.
Left by Kevin on Jun 28, 2008 3:17 PM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Brian,

Thanks for your help.
Left by Nikolay on Jul 09, 2008 7:19 AM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Cool, thanks for that.
Left by MatchMatix on Aug 21, 2008 5:09 AM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Just want to mention regarding step 2.

If you use the assembly-level attribute:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Then it shouldn’t be necessary to call the
log4net.Config.XmlConfigurator.Configure();

It will be done automatically by log4net on the first log call. If you want to check it yourself, set a breakpoint on the “static public void ConfigureAndWatch” in the log4net source (aprox line 720 in Xmlconfigurator.cs)


Also, instead of the assembly-level attribute, you can use:
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@"log4net.config"));

In an asp.net application you would need to write something like:
string appPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(appPath + "log4net.config"));

Best regards
Georg
http://www.l4ndash.com – Log4net dashboard
Left by Georg on Sep 16, 2008 4:46 PM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
do it in one line:
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@Server.MapPath("~/log4net.config")));

assuming your logging config is at the root - same as web.config
Left by Antek Group on May 18, 2009 5:04 PM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
We also need give folder with the write permission to ASPNET account, where the log file resides.
Left by shannon on Jul 08, 2009 3:55 AM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Georg, thanks for the tip, that's exactly the information I was looking for.

Joe
Left by Joe S on Aug 26, 2009 4:35 AM

# re: Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.
Requesting Gravatar...
Perfect - thanks!
Left by Keith on Aug 27, 2009 5:55 AM

Your comment:
 (will show your gravatar)
 


Copyright © Brian Sherwin | Powered by: GeeksWithBlogs.net | Join free