每个项目的log4net配置C#

时间:2017-05-25 17:41:19

标签: c# windows-services log4net

我有3个类Lib项目(ClassLib1.dll,ClassLib2.dll,ClassLib3.dll)。 一个Windows服务项目。 WindowsService1。

为Windows服务项目配置了net for net,而log4net在配置文件中为

    <log4net> 
    <root>
      <level value="all" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <file value="C:\\XX\\Logs\\{App_Name}\\{App_Name_Log_File_}" />
      <countDirection value="1" />
      <datePattern value="yyyyMMdd'.log'" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
      </layout>
    </appender>
  </log4net>

Windows服务指的是调用3个类Lib项目。 如何为这3个类lib配置log4 net,以便我可以在单独的文件夹中创建一个日志文件,如下所示?

对于主窗口服务 C:\ XX \日志\ Win_Servive \ Win_Service_Log_File_.log 对于类Lib 1 C:\ XX \日志\ ClassLib1 \ ClassLib1_Log_File_.log 对于类Lib 2 C:\ XX \日志\ ClassLib2 \ ClassLib2_Log_File_.log 对于类Lib 3 C:\ XX \日志\ ClassLib3 \ ClassLib3_Log_File_.log

2 个答案:

答案 0 :(得分:3)

您可以通过在web.config文件上配置三个不同的appender,然后按名称加载三个不同的记录器来实现此目的:

 <appender name="Class1Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
 <appender name="Class2Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
 <appender name="Class3Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>

然后使用适当的记录器:

ILog class1Log = LogManager.GetLogger("Class1Appender");
ILog class2Log = LogManager.GetLogger("Class2Appender");
ILog class3Log = LogManager.GetLogger("Class3Appender");

答案 1 :(得分:0)

最终的log4net配置如下

<log4net>
<root>
  <level value="all" />
</root>

<logger name="WinService">
  <appender-ref ref="WinService" />
</logger>

<logger name="ClassLibrary1">
  <appender-ref ref="ClassLib1" />
</logger>

<logger name="ClassLibrary2">
  <appender-ref ref="ClassLib2" />
</logger>

<logger name="ClassLibrary3">
  <appender-ref ref="ClassLib3" />
</logger>

<appender name="WinService" type="log4net.Appender.RollingFileAppender" additivity="false">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="C:\\XX\\Logs\\WinService\\WinService_" />
  <countDirection value="1" />
  <datePattern value="yyyyMMdd'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
  </layout>
</appender>
<appender name="ClassLib1" type="log4net.Appender.RollingFileAppender" additivity="false">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="C:\\XX\\Logs\\ClassLib1\\ClassLib1_" />
  <countDirection value="1" />
  <datePattern value="yyyyMMdd'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
  </layout>
</appender>
<appender name="ClassLib2" type="log4net.Appender.RollingFileAppender" additivity="false">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="C:\\XX\\Logs\\ClassLib2\\ClassLib2_" />
  <countDirection value="1" />
  <datePattern value="yyyyMMdd'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
  </layout>
</appender>
<appender name="ClassLib3" type="log4net.Appender.RollingFileAppender" additivity="false">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file value="C:\\XX\\Logs\\ClassLib3\\ClassLib3_" />
  <countDirection value="1" />
  <datePattern value="yyyyMMdd'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
  </layout>
</appender>