log4net

Aug 25, 2009 at 1:06 PM
Edited Aug 26, 2009 at 10:03 AM

Hello all,

I want to use log4net inside my addins. All addins are loaded into new AppDomains.

The problem is that I don't know how to setup log4net config inside each addin using configuration from host config (I use asp.net web site like a host)

Thanks


Oct 4, 2009 at 4:01 AM

There's http://logging.apache.org/log4net/release/sdk/log4net.Repository.Hierarchy.html

Method of understanding how to initialize loggers lies in looking at the web.config file and then just reversing that into code, or alternatively xcopying the web.config, then:

Log4NetConfigurationSection section =
(Log4NetConfigurationSection)ConfigurationManager.GetSection("log4net");
string fileName =
section.Appenders["FileAppender"].Properties["File"].ToString();
section.Appenders["FileAppender"].Properties["File"] =
fileName.Replace("%SERVICE_NAME%", "Foo");
Log4NetConfigurator.Configure(section);

- For the thread problem I would see something like that:
Log4NetConfigurationSection section =
(Log4NetConfigurationSection)ConfigurationManager.GetSection("log4net");
Thread logThread = new Thread();
logThread.Name = "Log thread";
logThread.Start();
foreach (Appender appender in section.Appenders)
{
    if (appender.Type == typeof(AsyncAppender))
    {
        appender.Properties["thread"] = logThread;
    }
}
Log4NetConfigurator.Configure(section);

From http://www.nabble.com/Configuration-system-enhancement-td19767525.html

This is from a config file of course. Here's how to do it in code:

         var h = (Hierarchy) LogManager.GetRepository();

            var root = h.Root;
            var pattern = new PatternLayout();
            pattern.ConversionPattern = "%logger %date [%thread] %message%newline";
            pattern.ActivateOptions();

            if (Config.Default.LiveMode == "RELEASE")
            {
                var appender = new EventLogAppender
                                   {
                                       ApplicationName = "Listener Service",
                                       LogName = "Application",
                                       Threshold = Level.Info,
                                       MachineName = Environment.MachineName,
                                       Layout = pattern
                                   };
                appender.ActivateOptions();
                root.AddAppender(appender);
                BasicConfigurator.Configure(h, appender);
            }

            // we might debug in release mode as well, or attach a debugger.
            var debugAppender = new DebugAppender();
            debugAppender.ActivateOptions();
            debugAppender.Layout = pattern;
            root.AddAppender(debugAppender);

            BasicConfigurator.Configure(h, debugAppender);
            _Logger = LogManager.GetLogger("BookingListenerAppService");

I'm running that code in a listener application which is very stable. Haven't had any problems with it.

Also, have a look at http://pipelinebuilder.codeplex.com/ for a new version of PB.

Cheers

 

            var h = (Hierarchy) LogManager.GetRepository();

            var root = h.Root;
            var pattern = new PatternLayout();
            pattern.ConversionPattern = "%logger %date [%thread] %message%newline";
            pattern.ActivateOptions();

            if (Config.Default.LiveMode == "RELEASE")
            {
                var appender = new EventLogAppender
                                   {
                                       ApplicationName = "Listener Service",
                                       LogName = "Application",
                                       Threshold = Level.Info,
                                       MachineName = Environment.MachineName,
                                       Layout = pattern
                                   };
                appender.ActivateOptions();
                root.AddAppender(appender);
                BasicConfigurator.Configure(h, appender);
            }

            // we might debug in release mode as well, or attach a debugger.
            var debugAppender = new DebugAppender();
            debugAppender.ActivateOptions();
            debugAppender.Layout = pattern;
            root.AddAppender(debugAppender);

            BasicConfigurator.Configure(h, debugAppender);
            _Logger = LogManager.GetLogger("BookingListenerAppService");
Oct 8, 2009 at 2:03 PM

try creating an app.config file in the add-in dir (preferably through the vs solution).

i've mine using common logging and log4net working through this.