使用以下XML配置将日志配置为两个或多个文件,
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender2" type="log4net.Appender.RollingFileAppender">
<file value="logs\log1.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<logger additivity="false" name="RollingLogFileAppender2">
<level value="All"/>
<appender-ref ref="RollingLogFileAppender2" />
</logger>
</log4net>
上面的XML配置登录到两个不同的文件中。
要以编程方式获取特定的logger实例,
ILog logger = log4net.LogManager.GetLogger ("RollingLogFileAppender2");
您可以在log4net根元素内附加两个或多个appender元素,以登录到多个文件。
有关上述XML配置结构或最适合您的应用程序的附加程序的更多信息,请从下面的链接中阅读详细信息,
https://logging.apache.org/log4net/release/manual/configuration.html
https://logging.apache.org/log4net/release/sdk/index.html
希望这会有所帮助。