Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging with Castle.Facilities.Logging and log4net

I'm trying to get log4net integration for Castle Windsor working. I wrote my class with an public property of type ILogger and took the configuration in my app.config like following.

<configuration>
  <configsections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configsections>

  <castle>
    <facilities>
      <facility id="logging" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" loggingApi="log4net" />
    </facilities>
    <components>
      <component id="form1" type="WinFormsActiveRecordSample.Form1, WinFormsActiveRecordSample" />
    </components>
  </castle>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
    </root>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="main.log" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd.MM.yy HH:mm:ss} %-5level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

In my eyes this should be working, but it doesn't. When I set loggingApi="console" it logs correctly. When I change it to log4net it does nothing. The log4net configuration was taken from another project where the block is working. What do I have to do that the log file is used? Must there be a special log4net configuration?

Thanks for any hint

Boris

like image 643
Booser Avatar asked Feb 19 '10 15:02

Booser


3 Answers

Move your log4net configuration to a separate file log4net.config, then refer that file from the facility configuration:

<facility id="loggingfacility" configfile="log4net.config" loggingapi="log4net" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>

If you want to have your log4net configuration in a section of your app.config:

public class MyLog4NetFactory: Log4netFactory {
    public MyLog4NetFactory() {
        XmlConfigurator.Configure();
    }

    public override ILogger Create(String name) {
        ILog log = LogManager.GetLogger(name);
        return new Log4netLogger(log.Logger, this);
    }

    public override ILogger Create(String name, LoggerLevel level) {
        throw new NotSupportedException("Logger levels cannot be set at runtime. Please review your configuration file.");
    }
}

then register the facility as:

<facility 
  id="loggingfacility" 
  loggingapi="custom" 
  customLoggerFactory="[fully qualified type name of MyLog4NetFactory]" 
  type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>
like image 89
Mauricio Scheffer Avatar answered Feb 13 '23 18:02

Mauricio Scheffer


You can use App.config for your log4net configuration without creating a custom logging factory. Simply provide the App.config file as an argument to the LoggingFacility constructor:

container
    .AddFacility("logging",
        new LoggingFacility(LoggerImplementation.Log4net,
            System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        )

Unlike Mauricio Scheffer's answer, the default factory does ConfigureAndWatch. This works just fine for me with the App.config file, though I'm not running on IIS or anything else that restricts permissions.

I'm doing this in code because you can't reliably use the Windsor Xml configuration to load log4net configuration from App.config. This is because the location of App.config can be modified when creating a new AppDomain.

Using my solution means the log file configuration will be compiled into your code. But you can mitigate this by using a Windsor Installer to configure logging, and specify the installer (or installer assembly) from the App.config file:

public class LoggingInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container
            .AddFacility("logging",
                new LoggingFacility(LoggerImplementation.Log4net,
                    System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
                );
    }
}

...

<castle>
    <installers>
        <install assembly="MyAssemblyName" />
    </installers>
</castle>

If in the future (maybe in your test cases) you have to load the logging configuration from a different file, and can't or don't want to recompile, simply change the Xml to point to Windsor Installers in a different assembly:

<castle>
    <installers>
        <install assembly="SomeOtherAssemblyName" />
    </installers>
</castle>
like image 26
Merlyn Morgan-Graham Avatar answered Feb 13 '23 17:02

Merlyn Morgan-Graham


Note that you can use the following in recent Castle versions:

container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithAppConfig());

This will use Log4net for logging and search for the log4net config section in the application configuration file.

like image 36
cguedel Avatar answered Feb 13 '23 17:02

cguedel