如何以编程方式添加日志过滤器?
How do I programmatically add logging filters?
我想在我的 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>
现在我想用C#代码实现它。网上没找到示例代码。
我会看一下 NLog 的 Configuration API 文档。虽然它没有具体引用过滤器,但似乎以编程方式完成此操作的方法是使用 LoggingRules
来控制日志消息的处理方式。
编辑
正如我所说,配置 API 是实现此目的的方法。此示例将以编程方式复制以下配置:
配置
<targets>
<target name="console" type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console">
<filters>
<when condition="not starts-with('${message})', 'PleaseLogThis')" action="Ignore" />
</filters>
</logger>
</rules>
代码
// Create the filter
var filter = new ConditionBasedFilter();
filter.Condition = "not starts-with('${message}','PleaseLogThis')";
filter.Action = FilterResult.Ignore;
// Create the rule to apply the filter to
var consoleTarget = new ColoredConsoleTarget();
var rule = new LoggingRule("*", LogLevel.Debug, consoleTarget);
rule.Filters.Add(filter);
// Create the config to apply the rule to
var config = new LoggingConfiguration();
config.LoggingRules.Add(rule);
// Update the log manager with the programmatic config
LogManager.Configuration = config;
// Test Your Logging
var log = LogManager.GetCurrentClassLogger();
log.Debug("Test");
log.Debug("Filter");
log.Debug("PleaseLogThis");
Console.ReadLine();
我想在我的 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>
现在我想用C#代码实现它。网上没找到示例代码。
我会看一下 NLog 的 Configuration API 文档。虽然它没有具体引用过滤器,但似乎以编程方式完成此操作的方法是使用 LoggingRules
来控制日志消息的处理方式。
编辑
正如我所说,配置 API 是实现此目的的方法。此示例将以编程方式复制以下配置:
配置
<targets>
<target name="console" type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console">
<filters>
<when condition="not starts-with('${message})', 'PleaseLogThis')" action="Ignore" />
</filters>
</logger>
</rules>
代码
// Create the filter
var filter = new ConditionBasedFilter();
filter.Condition = "not starts-with('${message}','PleaseLogThis')";
filter.Action = FilterResult.Ignore;
// Create the rule to apply the filter to
var consoleTarget = new ColoredConsoleTarget();
var rule = new LoggingRule("*", LogLevel.Debug, consoleTarget);
rule.Filters.Add(filter);
// Create the config to apply the rule to
var config = new LoggingConfiguration();
config.LoggingRules.Add(rule);
// Update the log manager with the programmatic config
LogManager.Configuration = config;
// Test Your Logging
var log = LogManager.GetCurrentClassLogger();
log.Debug("Test");
log.Debug("Filter");
log.Debug("PleaseLogThis");
Console.ReadLine();