简体   繁体   English

在运行时禁用/启用nlog规则

[英]Disabling / Enabling nlog rules at runtime

I am using nlog with the nlog.config file. 我将nlog与nlog.config文件一起使用。

Now I have a target that logs unhandled exceptions to a webservice 现在,我有一个目标,将未处理的异常记录到Web服务

<target xsi:type="WebService" name="webservice"
    url="http://server.tld/services/errorreporting/"
    methodName=""
    namespace=""
    protocol="HttpPost"
>

....

</target>

And a rule: 和一条规则:

<logger name="*" minlevel="Fatal" writeTo="webservice" />

Now I wan't to allow the user to decide if he wants to use this feature. 现在,我将不允许用户决定是否要使用此功能。 The application is a winforms app and the user sees a dialog for unhandled exceptions. 该应用程序是一个winforms应用程序,用户会看到一个对话框,其中包含未处理的异常。 Now I want to add an errorreporting checkbox. 现在,我想添加一个errorreporting复选框。 Only if it is checked, I want to log to my webservice. 只有选中它,我才想登录到我的Web服务。

My idea is to use a filter for my rule with a custom condition. 我的想法是对具有自定义条件的规则使用过滤器。 The nlog docs claim: Nlog文档声称:

New condition functions are easy to add; just create an ordinary function and mark it with the [ConditionMethod] attribute.

So I could write a function 所以我可以写一个函数

public static bool ErrorReportingEnabled()
{
   return Properties.Settings.Default.ErrorReportingEnabled;
}

but I don't find any reference on how to define this function in my filter. 但我在过滤器中找不到有关如何定义此功能的任何参考。

Eventually I just changed my rule to 最终我将规则更改为

<logger name="WebServiceLogger" minlevel="Fatal" writeTo="webservice" />

and in my code I just create a logger with this name. 在我的代码中,我只是使用此名称创建了一个记录器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM