有没有办法用NLog创建一个命名记录器并按此名称过滤日志记录?(Is there a way to create a named logger with NLog and filter logging

编程入门 行业动态 更新时间:2024-10-27 00:28:58
有没有办法用NLog创建一个命名记录器并按此名称过滤日志记录?(Is there a way to create a named logger with NLog and filter logging by this name(s)?)

有时我不希望在开发期间将所有内容记录到Visual Sutdio Output窗口(目标 - >调试器)。 我想也许有一种方法可以命名一个特定的记录器(一个类)或几个记录器(来自多个类),这样在配置文件中我只能为我目前感兴趣的开发中的类启用日志记录。

目前我在所有课程中都有这个最常见的NLog系列:

private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

以及一个非常标准的配置,只需将日志呈现到输出窗口。

不幸的是,我没有找到如何按类启用/禁用记录器的运气。

Sometimes I don't want to log everything especially to the Visual Studio Output window (target --> debugger) during development. I thought maybe there is a way to name a particular logger (one class) or few loggers (from multiple classes) so that in the configuration file I can enable logging only for the classes in development that I am interested in at the moment.

Currently I have this most common NLog line in all my classes:

private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

and a very standard configuration that just renders logs to the output window.

Unfortunately I had no luck in finding how to enable/disable loggers by class(es).

最满意答案

正如我在评论中提到的,您可以使用NLog 条件来评估和确定您要对结果执行的操作。 正如文件所说:

条件是与过滤器一起使用的过滤器表达式。 它们由一个或多个测试组成。 在过滤器中使用它们来确定是否将采取措施。

还有一个非常有用的例子:

<rules> <logger name="*" writeTo="file"> <filters> <when condition="length('${message}') > 100" action="Ignore" /> <when condition="equals('${logger}','MyApps.SomeClass')" action="Ignore" /> <when condition="(level >= LogLevel.Debug and contains('${message}','PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" /> <when condition="not starts-with('${message}','PleaseLogThis')" action="Ignore" /> </filters> </logger> </rules>

As I've mentioned in the comments, you could use the NLog Conditions to evaluate and determine what you want to do with the results. As the documentation says:

Conditions are filter expressions used with the when filter. They consist of one or more tests. They >are used in the when filter to determine if an action will be taken.

Also there's a very useful example:

<rules> <logger name="*" writeTo="file"> <filters> <when condition="length('${message}') > 100" action="Ignore" /> <when condition="equals('${logger}','MyApps.SomeClass')" action="Ignore" /> <when condition="(level >= LogLevel.Debug and contains('${message}','PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" /> <when condition="not starts-with('${message}','PleaseLogThis')" action="Ignore" /> </filters> </logger> </rules>

更多推荐

NLog,classes,记录器,窗口,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-24 13:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1246297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记录器   没有办法   按此   创建一个   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!