tags:

views:

1304

answers:

4

Background

I am writing a class library assembly in C# .NET 3.5 which is used for integration with other applications including third-party Commercial-Off-The-Shelf (COTS) tools. Therefore, sometimes this class library will be called by applications (EXEs) that I control while other times it will be called by other DLLs or applications that I do not control.

Assumptions

  • I am using C# 3.0, .NET 3.5 SP1, and Visual Studio 2008 SP1
  • I am using log4net 1.2.10.0 or greater

Constraints

Any solution must:

  • Allow for the class library to enable and configure logging via it's own configuration file, if the calling application does not configure log4net.
  • Allow for the class library to enable and configuring logging via the calling applications configuration, if it specifies log4net information

OR

  • Allow for the class library to enable and configuring logging using it's own configuration file at all times.

Problem

When my stand-alone class library is called by a DLL or application that I do not control (such as a third-party COTS tool) and which doesn't specify log4net configuration information, my class library is unable to do any of it's logging.


Question

How do you configure and enable log4net for a stand-alone class library assembly so that it will log regardless if the calling application provides log4net configuration?

A: 

In your standalone class library, have a singleton which loads the log4net configuration file using the log4net.Config.XmlConfigurator.

Specifically, you can define all of your code to use your own custom logging class; this class can just be a simple wrapper of the log4net logging calls, with one addition; make a static member which contains the log information you want to log to; initialize that with a call to the XmlConfigurator in the static constructor for that class. That's all you have to do.

McWafflestix
How would I be sure that my singleton would be called since they could be calling any number of methods or classes within my library? Wouldn't I have to wrap all of my logging calls into the singleton class so that I could enforcing loading, if necessary?Thanks!
Burly
Added information on just how to do this in the answer.
McWafflestix
+3  A: 

You can probably code something around the XmlConfigurator class:

public static class MyLogManager
{
    // for illustration, you should configure this somewhere else...
    private static string configFile = @"path\to\log4net.config";

    public static void GetLogger(Type type)
    {
        if(log4net.LogManager.GetCurrentLoggers().Length == 0)
        {
            // load logger config with XmlConfigurator
            log4net.Config.XmlConfigurator.Configure(configFile);
        }
        return LogManager.GetLogger(type);
    }
}

Then in your classes, instead of:

private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));

Use:

private static readonly ILog log = MyLogManager.GetLogger(typeof(MyApp));

Of course, it would be preferable to make this class a service and dynamically configure it with the IoC container of your choice, but you get the idea?

EDIT: Fixed Count() problem pointed out in comments.

Jacob
Ohhh, nice. This answers the question I was asking McWafflestix and Jeroen Huinink (I would imagine they had thought about this as well). Let me give this a whirl quickly.
Burly
@Jacob: it seems to me that the condition you are checking Count() > 0 is wrong. Shouldn't it read Count() = 0?
Jeroen Huinink
If you have multiple classes in your library then Jacob's solution makes sense. If you have only one you could implement my solution. This avoids the need for an extra class.
Jeroen Huinink
I believe Jeroen is correct, the condition should be == 0. Also, it's .Length not .Count. I'm working out this idea in my current solution. I'll get back shortly with more details. Thanks!
Burly
D'Oh, of course. This is a bit of a hack though. I'm sure log4net has some sort of configuration mechanism that would allow you to override GetLogger(type) without the need for a custom class, something like a LoggerResolver...
Jacob
You might also want to change to return type of GetLogger to ILogger instead of void.
tkeE2036
+4  A: 

In your code you can check if there are any loggers via

log4net.LogManager.GetCurrentLoggers().Count()

You could then for example use an XmlConfigurator to load a default configuration from a file:

log4net.Config.XmlConfigurator.Configure(configFile)

You could do the initialization in a static or regular constructor.

class Sample
{
    private static readonly log4net.ILog LOG;

    static Sample()
    {
        if (log4net.LogManager.GetCurrentLoggers().Count() == 0)
        {
            loadConfig();
        }
        LOG = log4net.LogManager.GetLogger(typeof(Sample));

    }

    private static void loadConfig()
    {
        /* Load your config file here */
    }

    public void YourMethod()
    {
       LOG.Info("Your messages");
    }
}
Jeroen Huinink
This sounds promising. Similar to my comment to McWafflestix, how would I perform this check and call to Configure in such a manner that it is guaranteed to execute before any logging calls are made in the class library without wrapping all logging calls into a separate class? In otherwords, there is no "main" in my class assembly where I can perform this check - they can call into my assembly where ever they want. I would like to avoid wrapping log4net if possible.
Burly
+1  A: 
Burly