4

I'm having a weird issue with log4net.

In an ASP.NET application, I want to configure log4net externally, so I have a separate log4net.config file which I hook up to my web app with this line in the AssemblyInfo.cs file belonging to my web app:

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

Now, if I instantiate the log4net logger class the normal way like this:

public class MyClass
{
    private static readonly ILog _logger = log4net.LogManager.GetLogger(typeof(MyClass));
    ....

Then this works, and the logging works as normal. However, I've wrapped my logging code in a LogManager class, which is part of a separate assembly (Infrastructure), and is reused across a number of projects. It has a GetLogger that looks like this:

public static class LogManager
{
    public static ILog GetLogger()
    {
        var stack = new StackTrace();
        var frame = stack.GetFrame(1);
        return new log4net.LogManager.GetLogger(frame.GetMethod().DeclaringType);
    }
}

So I can use this in my asp.net code:

public class MyClass
{
    private static readonly ILog _logger = LogManager.GetLogger();
    ....

But... This doesn't work! No logging is produced It doesn't seem to hook up the config file correctly. If I put my log4net config directly into the web.config, then this LogManager works fine.

2
  • 1
    Is Log4Net scanning for the assembly attribute when it first gets prodded in the currently executing assembly, therefore, if the attribute is not defined in that assembly, it's not get set up as expected?
    – Tim Lloyd
    Jun 22, 2011 at 11:02
  • Yeah could be - that could be related to Eben's response below Jun 22, 2011 at 11:29

2 Answers 2

3

To sum up what Bibhu has said. It has all to do with log4net.Config.XmlConfigurator.

You have to configure log4net before logging will start. So whenever you use your LogManager class the hosting application (windows, web) must configure the logging.

Either that or you need to do it in your LogManager.

3
  • So if use a LogManager class AND that class is in another assembly, then I need to manually call XmlConfigurator.Configure() in my global.asax.cs. That does fix it, but I don't get it - why would it work if I set my _logger the normal way ? Jun 22, 2011 at 11:29
  • ah! as per chibacity's comment I may have missed that --- where is the [assembly: log4net.Config.XmlConfigurator...] bit defined? Point is that the hosting application has to configure log4net (however that happens)
    – Eben Roux
    Jun 22, 2011 at 11:43
  • Right - so that looks like the issue! Because I moved LogManager into another assembly which doesn't have the assembly attribute, it doesn't work! It all makes sense now. Jun 22, 2011 at 11:48
2

Tell the application to configure log4net using external 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.

Then, start logging.

5
  • Not quite sure why you've given me a Udp config to use, but I think your main point is the .Configure() method being called.. But I was under the impression that the assebly attribute removes the need to do this? Jun 22, 2011 at 11:17
  • @Matt Roberts - Have look on this question, it may help you. stackoverflow.com/questions/1731513/…
    – Bibhu
    Jun 22, 2011 at 11:26
  • Thanks, but that's more of a best practice. What I do was working for me and I was happy with it, but moving the code into LogManager borked it :) Jun 22, 2011 at 11:32
  • @Matt Roberts - Your code should work out. Can you recheck the log4net.config file. logging.apache.org/log4net
    – Bibhu
    Jun 22, 2011 at 11:34
  • Thanks for your efforts, but your latest answer is a bit wrong. If you use the assembly attribute to hook up the config file, you don't also need to hook it up in the Global.asax.cs. Instead, you can just call XmlConfigurator.Configure(); Jun 22, 2011 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.