无论如何使用 log4net 记录一些信息
情况是 log4net 的级别配置为“错误”,但我需要在“无论如何”条件下写入一些信息 例如
“登录已开始” 如果仅启用“错误”或“致命”,我无法将其记录在“错误”或“致命”中,因为它只是信息,
所以除了将记录器的级别更改为信息、写入日志然后再改回来之外,还有什么方法可以做到这一点级别,因为这就像一种解决方法,而不是解决方案
,并且不使用标头,因为它们只出现在开头
编辑:在 Appender 中
StringMatchFilter stringFilter = new StringMatchFilter();
stringFilter.AcceptOnMatch = true;
stringFilter.StringToMatch = "successfully";
stringFilter.ActivateOptions();
appender.AddFilter(stringFilter);
DenyAllFilter deny = new DenyAllFilter();
deny.ActivateOptions();
appender.AddFilter(deny);
添加到 Appender 并将级别“All”设置为根并在 Appender 中管理级别,但仍然我我无法写入任何包含“成功”的消息 但请注意,当我将附加程序级别设置为信息时,过滤器开始工作
Situation is that level of log4net is configured as 'Error' but there is some information i need to write under a 'no matter what' condition
for example
"loggin has started"
if only 'Error' or 'Fatal' is enabled i cant log this in Error or Fatal since its just information
so is there any way i can do that other than change the level of the logger to info, write the log and then change back the level because that will act just like a workaround not a solution
and without using Headers since they only come at the beginning
EDIT: In an Appender
StringMatchFilter stringFilter = new StringMatchFilter();
stringFilter.AcceptOnMatch = true;
stringFilter.StringToMatch = "successfully";
stringFilter.ActivateOptions();
appender.AddFilter(stringFilter);
DenyAllFilter deny = new DenyAllFilter();
deny.ActivateOptions();
appender.AddFilter(deny);
adding to an appender and setting level 'All' to the root and managing levels in appenders but still i am unable to write any message containing 'successfully'
but please note when i set appender level to info the filter begins to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这都是因为您不喜欢使用 .FatalFormat 当它不是真正的错误时的想法。
我不确定您将如何以编程方式执行此操作,但如果您使用配置文件,您将添加一个部分,
这意味着您可以像这样记录消息
,或者您可以创建静态
并通过以下方式进行记录。
I assume this is all because you don't like the idea of using .FatalFormat when it is not really an error.
I'm not sure how you would do this programatically, but if you were using a config file you add a section like
which means you can log your messages like
or you could create a static
and log via.
我的建议是创建一个过滤器,首先查看字符串匹配,然后查看级别。这样,您就可以在消息中传递一个密钥字符串(例如“AppInfo”或一些唯一且在错误中找不到的字符串)。然后,即使您在 INFO 级别记录它,您的过滤器也会拾取并记录它,但过滤器会忽略所有其他不是 ERROR 或 FATAL 的消息。我在 CodeProject 上写了一篇文章,将向您展示如何进行复杂的过滤,如下所示:
http:// /www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
这里的一个关键是,您可能不需要在根目录中为这个特定的附加程序指定过滤,因为如果我没记错的话,根目录会取代附加程序。
My suggestion would be to create a filter that looks first at a string match and then at the level. That way you could have a key string that you pass in the message (say "AppInfo" or something that would be unique and not found in an error). Then, your filter would pick that up and log it even when you logged it at the INFO level but the filter would ignore all other messages that weren't ERROR or FATAL. I wrote an article on CodeProject that will show you how to do complex filtering like this:
http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
The one key here is that you would probably need to not specify the filtering in the root for this particular appender, since root supersedes the appender if I remember correctly.