以编程方式更改log4net日志记录级别 [英] Change log4net logging level programmatically

查看:106
本文介绍了以编程方式更改log4net日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于 650694 ,但那里没有答案,我根本无法获得所有这些建议,我怀疑我的处境可能略有不同.

This is similar to 650694 but no answer was accepted there, I can't get any of those suggestions to work at all, and I suspect I may be in a slightly different situation.

我正在调用log4net.Config.XmlConfigurator.Configure().但是在程序的这一点之后,我想将日志记录阈值更改为仅在运行时才知道的值.

I'm calling log4net.Config.XmlConfigurator.Configure(). But after that point in the program, I want to change the logging threshold to a value only known at runtime.

从另一个问题开始,我尝试了:

From the other question, I tried:

((log4net.Repository.Hierarchy.Logger)mylogger.Logger).Level = log4net.Core.Level.Error;

和:

var appender = new log4net.Appender.ColoredConsoleAppender();
appender.Layout = new log4net.Layout.PatternLayout(@"%date %-5level %message%newline");
appender.Threshold = log4net.Core.Level.Error;
appender.ActivateOptions();
log4net.Config.BasicConfigurator.Configure(appender);

但似乎没有一个效果:我仍然在控制台上看到DEBUG和INFO日志记录语句.

but neither one seems to have any effect: I'm still seeing DEBUG and INFO logging statements on the console.

我的直觉是我要添加一个新的附加程序,该附加程序对XML配置中声明的附加程序没有任何影响(告诉它打印DEBUG级别的消息),但是我尚无任何证据.

My hunch is that I'm adding a new appender, which has no effect on the appender declared in the XML config (which tells it to print DEBUG level messages), but I don't have any evidence for this yet.

一段时间以来,我一直在研究log4net API,但我只是没有看到它.有什么简单的我想念的吗?

I've been digging through the log4net API for a while now, and I'm just not seeing it. Is there something simple I'm missing?

推荐答案

此处提供的这些解决方案均不适合我.它没有在改变的运行时

None of these solutions present here worked for me. It wasn't changing at runtime

这对我有用:

((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);

您必须在对其配置进行更改后调用RaiseConfigurationChanged.

You have to call RaiseConfigurationChanged after making changes to its config.

这篇关于以编程方式更改log4net日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆