6

我有这个 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);

问题:现在我想lockingModelMinimalLock. 以编程方式,而不是在 XML 中。
怎么做?

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

4

2 回答 2

9
// 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();
}
于 2013-10-09T11:27:14.300 回答
-2
    <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>
于 2014-12-03T17:43:09.830 回答