首先,我在其他主题中看到了很多答案和提示(最相似的是:Log4Net: Multiple loggers),但没有适用的答案。
我希望有2个记录器具有不同的文件追加器,并限制每个记录器写入根记录器。它是控制台应用程序。以下整个代码:
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中指定appender时,例如:
<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
我做错了什么?
UPD(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'是小写的。
答案 0 :(得分:18)
你的例子中有几个拼写错误。首先,你不要关闭配置标签,以及为什么你只得到一个文件,因为你打电话:
logger.Info("started async");
然后你出人意料地做了:
logger.Info("started sync");
您不会在第二个文件中写入,因为您实际上没有登录它。
在控制台中我从你的代码中得到:
Logger: Async
Appenders: FileInfoAppenderS, FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderS, FileInfoAppenderA
对于将来的信息,您通过将可加性设置为false来正确执行,因为这意味着记录器不会从根记录器继承。关于声明:
我希望有2个记录器具有不同的文件追加器,并限制每个记录器写入根记录器
我不明白。如果你想让你的记录器写入这些文件,而root logger例如有一个控制台appender,只需删除additivity,他们就会写入控制台和他们自己的文件。也经过测试,效果非常好。
我已阅读你的评论。现在我添加我正在使用的代码并获得您需要的代码:
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");
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>
<appender name="FileInfoAppenderA" type="log4net.Appender.RollingFileAppender">
<file value="C:\\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="C:\\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>
<root>
<level value="INFO"/>
</root>
<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>
Log4net版本:1.2.13.0 with .NET 4.0 如果你得到你想要的东西,请告诉我。
答案 1 :(得分:0)
此配置对我有用:
<log4net>
<root name="EventLog">
<level value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>
<logger name="FileLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
...appenders
</logger>
祝你好运!