找不到属性[rollingStyle]在[log4net.Appender.FileAppender]上设置对象

时间:2019-05-09 06:56:43

标签: c# .net log4net-configuration

我有一个配置了log4net的app.config文件。但是我的文件没有被创建。在记录器内部,它显示错误。

 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <logger name="ApplicationLogger">
      <appender-ref ref="XmlAppender" />
      <level value="ALL" />
    </logger>
    <appender name="XmlAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="C:\MyApplication\Logs\%property{LogName}.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value=".yyyyMMdd-HHmm" />
      <maxSizeRollBackups value="10000" />
      <maximumFileSize value="1MB" />
      <countDirection value="1"/>
      <immediateFlush value="true" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%n" />
      </layout>
    </appender>
</log4net>

我的C#代码如下:

public void LogMsg(string msg)
{
    try
    {
        strLogFileName = GetFileName();
        log4net.Repository.ILoggerRepository loggerRepository =         
        LogManager.CreateRepository(strLogFileName + "Repository");
        ThreadContext.Properties["LogName"] = strLogFileName;
        log4net.Config.XmlConfigurator.Configure(loggerRepository);
    }
    catch (Exception ex) { }
    logger = LogManager.GetLogger(strLogFileNameECU + "Repository", "ApplicationLogger");
    logger.Error(msg);
}

请告诉我我哪里出问题了。

1 个答案:

答案 0 :(得分:0)

对于滚动文件,您应该使用log4net.Appender.RollingFileAppender,这是一个配置演示

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log/log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="_yyyy-MM-dd.\t\x\t" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1MB" />
    <staticLogFileName value="false"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>