有时我不希望在开发期间将所有内容记录到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
发布评论