Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set log4net LockingModel programmatically after loading with XmlConfigurator?

I have this XML log4net configuration:

  <log4net>
    <appender name="myAppender" type="log4net.Appender.RollingFileAppender">
      <file value="mylog.txt" />
    </appender>
    <root>
      <level value="DEBUG" />
        <appender-ref ref="myAppender" />
    </root>
  </log4net>

I load this configuration with the C# line below, and it works well:

log4net.Config.XmlConfigurator.Configure(path);

PROBLEM: Now I want to set the lockingModel to MinimalLock. Programmatically, not in XML.
How to do it?

That would be the equivalent of adding <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> in the XML configuration.

like image 632
Nicolas Raoul Avatar asked Oct 09 '13 06:10

Nicolas Raoul


1 Answers

// assumes there are not multiple file appenders defined
var appender = log4net.LogManager.GetRepository()
                                 .GetAppenders()
                                 .OfType<FileAppender>()
                                 .SingleOrDefault();

if (appender != null)
{
     appender.LockingModel = new FileAppender.MinimalLock();
}
like image 176
stuartd Avatar answered Nov 07 '22 23:11

stuartd