gpt4 book ai didi

logging - Log4Net多个记录器

转载 作者:行者123 更新时间:2023-12-03 20:27:29 26 4
gpt4 key购买 nike

首先,我在其他主题(最相似的是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>
<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(10/21/2014):通过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">
......


注意区别:属性“名称”为小写。

最佳答案

您的示例中有几种错别字。首先是您不关闭配置标签,而为什么只进入一个文件,是因为您调用了:

logger.Info("started async");


然后您出乎意料地做到了:

logger.Info("started sync");


您实际上不会登录,因此不会写入第二个文件。

在控制台中,我从您的代码中得到:

Logger: Async
Appenders: FileInfoAppenderS, FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderS, FileInfoAppenderA


对于将来的信息,您通过将加性设置为false来正确完成操作,因为这意味着记录器将不会从根记录器继承。至于声明:


我想让2个记录器具有不同的文件附加器,并限制每个记录器写入根记录器


我不明白。例如,如果您希望记录器在拥有控制台附加程序的root记录器中写入这些文件,则只需删除可加性,它们就会写入控制台和自己的文件。还经过测试,效果很好。

我已阅读您的评论。现在,我添加我正在使用的代码并获得所需的代码:

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版本:带有.NET 4.0的1.2.13.0
请告诉我您是否想要的东西。

关于logging - Log4Net多个记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com