简体   繁体   English

如何以编程方式添加日志记录筛选器?

[英]How do I programmatically add logging filters?

I want to add the rule in my NLog. 我想在我的NLog中添加规则。 The rule is: 规则是:

 <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>

Now I want to implement it in C# code. 现在我想用C#代码实现它。 I haven't found the sample code online. 我还没有在网上找到示例代码。

I would take a look a look at the Configuration API documentation for NLog. 我将看一下NLog的Configuration API文档。 While it doesn't specifically reference filters, it seems like the way to accomplish this programmatically is by using LoggingRules to control how log messages are processed. 虽然它没有专门引用过滤器,但似乎以编程方式完成此操作的方法是使用LoggingRules来控制日志消息的处理方式。

Edit 编辑

Like I said, the Configuration API is the way to achieve this. 就像我说的,Configuration API是实现这一目标的方法。 This example will replicate the following config programmatically: 此示例将以编程方式复制以下配置:

Config 配置

<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>

Code

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式创建kmltreeview文件夹并在其中添加地标? - How do I programmatically create kmltreeview folders and add placemarks in them? 如何在运行时以编程方式将项目添加到“水平列表”? - How do I programmatically add items to a Horizontal List at runtime? 如何以编程方式向数据网格中添加列和单元格? - How do I add columns and cells in a row to a datagrid programmatically? 如何以编程方式将音频添加到Toast通知中? - How do I add audio programmatically to a Toast notification? 如何以编程方式将NewLines添加到TFS工作项文本框? - How do I programmatically add NewLines to a TFS work item textbox? 如何以编程方式将事件处理程序添加到.NET按钮? - How Do I Add Event Handlers to .NET Buttons Programmatically? 如何以编程方式将项目引用添加到wix项目? - How do I add project references to a wix project programmatically? 如何以编程方式将XML Map添加到Excel 2010电子表格中? - How do I add an XML Map programmatically to an Excel 2010 Spreadsheet? 如何以编程方式向AJAX标签添加新标签? - How do I add a new tab programmatically to AJAX tabs? 如何以编程方式将程序集引用添加到项目? - How do I programmatically add an assembly reference to a project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM