首先,我已经看到其他话题中的许多答案和提示(最相似的:Log4Net: Multiple loggers),但没有适用的答案。 我想要有两个具有不同文件附加器的记录器,并限制每个记录器都只能写入根记录器。 这是控制台应用程序。 全部代码如下:
using System;
using System.Diagnostics;
using System.Linq;
using log4net;
namespace Test_log4net
{
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger("Async");
logger.Info("started async");
Console.WriteLine("Logger: {0}", (logger as log4net.Core.LogImpl).Logger.Name);
Console.WriteLine("Appenders: {0}", string.Join(", ", (logger as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));
ILog logger2 = LogManager.GetLogger("Sync");
logger2.Info("started sync"); //changed: from logger -> to logger2 on 10/21/2014
Console.WriteLine("Logger: {0}", (logger2 as log4net.Core.LogImpl).Logger.Name);
Console.WriteLine("Appenders: {0}", string.Join(", ", (logger2 as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));
Console.ReadKey();
}
}
}
还有App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<root>
<level value="All"/>
</root>
<appender name="FileInfoAppenderA" type="log4net.Appender.RollingFileAppender">
<file value="D:\\temp\\AsyncTest.log"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="true"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d - %m%n"/>
</layout>
</appender>
<appender name="FileInfoAppenderS" type="log4net.Appender.RollingFileAppender">
<file value="D:\\temp\\SyncTest.log"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="true"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d - %m%n"/>
</layout>
</appender>
<logger Name="Sync" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoAppenderS"/>
</logger>
<logger Name="Async" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoAppenderA"/>
</logger>
</log4net>
</configuration>
并在控制台输出:
Logger: Async
Appenders: FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderA
文件已经被创建,但它们都是空的。当我在根目录中指定附加器时,如下所示:
<root>
<level value="All"/>
<appender-ref ref="FileInfoAppenderA"/>
<appender-ref ref="FileInfoAppenderS"/>
</root>
然后,控制台输出:
log4net:ERROR [RollingFileAppender] Attempted to append to closed appender named [FileInfoAppenderS]
Logger: Async
Appenders: FileInfoAppenderA, FileInfoAppenderS
Logger: Sync
Appenders: FileInfoAppenderA, FileInfoAppenderS
仅在 AsyncTest.log 中出现:
2014-04-11 17:26:58,142 - started async
2014-04-11 17:26:58,151 - started sync
我做错了什么?
更新(2014年10月21日):使用最新的Nuget可用的log4net,我得到了以下控制台输出:
Logger: Async
Appenders: FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderA
两个文件(AsyncTest.log、SyncTest.log)都是空的。
UPD (08/4/2015): 解决方案是在log4net部分中设置所有属性时使用小写。因此,我只需要更改app.config中的以下行:
......
<logger name="Sync" additivity="false">
......
<logger name="Async" additivity="false">
......
注意区别:属性'name'是小写的。