2017-05-25 95 views
1

我有3個類的Lib項目(ClassLib1.dll,ClassLib2.dll,ClassLib3.dll)。 一個Windows服務項目。 WindowsService1。每個項目的log4net配置C#

日誌淨是在配置文件中配置Windows服務項目和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個庫項目。 如何爲這3個class lib配置log4 net,以便我可以在下面的單獨文件夾中有一個日誌文件?

對於主窗口服務 C:\ XX \日誌\ Win_Servive \ Win_Service_Log_File_.log 對於Class庫1 C:\ XX \日誌\ ClassLib1 \ ClassLib1_Log_File_.log 對於Class庫2 C:\ XX \日誌\ ClassLib2 \ ClassLib2_Log_File_.log 對於IIb類3 C:\ XX \日誌\ ClassLib3 \ ClassLib3_Log_File_.log

回答

3

您可以通過在web.config文件中配置三個不同的附加目的地,然後裝載三個不同的記錄器實現這一目標按名稱:

<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"); 
+0

謝謝娟,現在單獨的文件夾中創建,但在每個日誌文件中的日誌語句是相同的。 – Prashant

+0

這只是配置。更改每個節點的節點屬性,以便文件名是唯一的。 – JuanR

+0

謝謝瓊! – Prashant

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>