Windows应用程序无法在log4net中写入多个日志文件

时间:2019-05-17 10:10:17

标签: windows log4net log4net-configuration rollingfileappender log4net-appender

我在Windows Service中运行的应用程序中使用了7个模块。我正在使用log4net获取动态日志文件。一切正常。

如果我尝试使用2个log4net配置文件来一次获取2个日志文件。第一次正确创建日志文件,当我尝试第二次运行该应用程序时,只有一个日志文件正确滚动。另一个文件为空。

我的第一个log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
  <!-- Log4net Logging Setup -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] - [%property{Name}] - [%-5level] -- [%message]%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="%property{LogName}%date{_dd_MM_yyyy}.log.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maximumFileSize value="2KB" />
      <maxSizeRollBackups value="5" />
      <staticLogFileName value="false" />
      <preserveLogFileNameExtension value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] - [%property{Name}] - [%-5level] -- [%message]%newline" />
      </layout>
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="RollingFileAppender" />
      </filter>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingFileAppender" />    
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

我的第二个log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
  <!-- Log4net Logging Setup -->
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] - [%property{Name}] - [%-5level] -- [%message]%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="IotConnectivityAgent" />
        <acceptOnMatch value="true" />
    </filter>
      <file type="log4net.Util.PatternString" value="%property{LogName}%date{_dd_MM_yyyy}.log.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maximumFileSize value="2KB" />
      <maxSizeRollBackups value="5" />
      <staticLogFileName value="true" />
      <preserveLogFileNameExtension value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date] - [%property{Name}] - [%-5level] -- [%message]%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

在程序代码1st模块中:

 log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4net.config"));
            log4net.GlobalContext.Properties["LogName"] = Path.GetFullPath(_config.GetValue(IOT_DefineMacros.XMLTAG_LOG_PATH)) + "\\" + _processName;

在程序代码第二模块中:

 log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4net_IOT.config"));
log4net.GlobalContext.Properties["LogName"] = 
 Path.GetFullPath(_config.GetValue(IOT_DefineMacros.XMLTAG_LOG_PATH)) + "\\" + _processName;

第一个log4net配置文件的Assemly.cs文件:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"log4net.config", Watch = true)]

第一个log4net配置文件的Assemly.cs文件:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"log4net_IOT.config", Watch = true)]

我想从此解决方案中获取2个日志文件。应该同时创建两个log4net文件之间的内容。

0 个答案:

没有答案