每个项目 C# 的 log4net 配置
log4net configuration for each project C#
我有 3 个 Class Lib 项目(ClassLib1.dll、ClassLib2.dll、ClassLib3.dll)。
一个 Windows 服务项目。 Windows服务 1.
网络日志是为 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 Class Lib 项目。
我如何为这 3 个 class 库配置 log4 net,以便我可以在单独的文件夹中有一个日志文件,如下所示?
主要 windows 服务
C:\XX\Logs\Win_Servive\Win_Service_Log_File_.log
对于 Class 库 1
C:\XX\Logs\ClassLib1\ClassLib1_Log_File_.log
对于 Class 库 2
C:\XX\Logs\ClassLib2\ClassLib2_Log_File_.log
对于 Class 库 3
C:\XX\Logs\ClassLib3\ClassLib3_Log_File_.log
您可以通过在 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");
最终的 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>
我有 3 个 Class Lib 项目(ClassLib1.dll、ClassLib2.dll、ClassLib3.dll)。 一个 Windows 服务项目。 Windows服务 1.
网络日志是为 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 Class Lib 项目。 我如何为这 3 个 class 库配置 log4 net,以便我可以在单独的文件夹中有一个日志文件,如下所示?
主要 windows 服务 C:\XX\Logs\Win_Servive\Win_Service_Log_File_.log 对于 Class 库 1 C:\XX\Logs\ClassLib1\ClassLib1_Log_File_.log 对于 Class 库 2 C:\XX\Logs\ClassLib2\ClassLib2_Log_File_.log 对于 Class 库 3 C:\XX\Logs\ClassLib3\ClassLib3_Log_File_.log
您可以通过在 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");
最终的 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>