使用多个 log4net 文件记录器
我有文件附加器 FileA、FileB 和 FileC。我将 FileA 添加到根元素,因为我希望它成为一个包罗万象的元素(下面将详细介绍)。我将 FileB 和 FileC 用于特定消息,并为每个附加程序创建命名记录器。在代码中,我加载用于大多数消息的日志,如下所示:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
其他记录器,我像这样加载
private static readonly log4net.ILog commandLog = log4net.LogManager.GetLogger("LoggerFileB");
发生的情况是我在 LoggerFileB 中得到了我所期望的内容,即只有特殊消息。问题是这些消息也出现在 LoggerFileA 中,这是我添加到 root 中的所有内容。我可以为包罗万象创建一个特定的命名实例,而不是将其添加到根元素,但我希望调用类型作为输出中的记录器名称。创建命名记录器意味着 %logger 输出日志的名称而不是类型。有没有一种方法可以准确地获得我想要的内容(将记录器名称显示为类型,但不显示记录到其他命名记录器的消息)?希望我遗漏了一些东西并且有一个简单的解决方案。
下面是我的 log.config 在这种情况下的示例。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="FileA" type="log4net.Appender.RollingFileAppender">
<file value="FileA.txt" />
...snip...
</appender>
<appender name="FileB" type="log4net.Appender.RollingFileAppender">
<file value="FileB.txt" />
...snip...
</appender>
<appender name="FileC" type="log4net.Appender.RollingFileAppender">
<file value="FileC.txt" />
...snip...
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LoggerFileA" />
</root>
<logger name="LoggerFileB">
<level value="ALL" />
<appender-ref ref="FileB" />
</logger>
<logger name="LoggerFileC">
<level value="ALL" />
<appender-ref ref="FileC" />
</logger>
</log4net>
</configuration>
I have file appenders FileA, FileB, and FileC. FileA I add to the root element as I want it to be a catch all, (more on this below). FileB and FileC I use for specific messages and create named loggers for each of those appenders. In code, I load the log I'm using for most messages like so:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
The other loggers, I load like this
private static readonly log4net.ILog commandLog = log4net.LogManager.GetLogger("LoggerFileB");
What's happening is I'm getting what I expect in LoggerFileB, ie, ONLY the special messages. The problem is these messages also showing up in LoggerFileA, my catch-all I added to root. I could create a specific named instance for the catch-all, instead of adding it to the root element, but I want the calling type as the logger name in the output. Creating a named logger means that %logger outputs the name of the log instead of the type. Is there a way to get precisely what I want (the catchall to show the logger name as the type, but not show messages logged to other named loggers)? Hopefully I'm missing something and there is a simple solution.
Here's an example of what my log.config looks like for this situation.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="FileA" type="log4net.Appender.RollingFileAppender">
<file value="FileA.txt" />
...snip...
</appender>
<appender name="FileB" type="log4net.Appender.RollingFileAppender">
<file value="FileB.txt" />
...snip...
</appender>
<appender name="FileC" type="log4net.Appender.RollingFileAppender">
<file value="FileC.txt" />
...snip...
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LoggerFileA" />
</root>
<logger name="LoggerFileB">
<level value="ALL" />
<appender-ref ref="FileB" />
</logger>
<logger name="LoggerFileC">
<level value="ALL" />
<appender-ref ref="FileC" />
</logger>
</log4net>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
additivity
设置为 false:You can use set the
additivity
to false: