如何在使用XmlConfigurator加载后以编程方式设置log4net LockingModel?

时间:2013-10-09 06:02:27

标签: c# log4net xmlconfigurator

我有这个XML log4net配置:

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

我使用下面的C#行加载此配置,它运行良好:

log4net.Config.XmlConfigurator.Configure(path);

问题:现在我想将lockingModel设置为MinimalLock以编程方式,而不是XML格式 怎么做?

这相当于在XML配置中添加<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

2 个答案:

答案 0 :(得分:7)

// 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();
}

答案 1 :(得分:-4)

    <appender name="text" type="log4net.Appender.RollingFileAppender,log4net">
        <param name="File" value="c:\system-name.log"/>
        <param name="AppendToFile" value="true"/>
        <rollingStyle value="Date"/>
        <staticLogFileName value="true"/>
        <datePattern value="'.'yyyy-MM-dd"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %5p %c{1}:%L - %m%n"/>
        </layout>
enter code here
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    </appender>