2013-10-08 40 views
0

我正在使用nlog和nlog.config文件。在运行时禁用/启用nlog规则

现在我有一个记录未处理的异常的Web服务

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

.... 

</target> 

以及规则的目标:

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

现在我wan't以允许用户如果他想使用决定此功能。 该应用程序是一个winforms应用程序,用户可以看到未处理的异常的对话框。现在我想添加一个errorreporting复选框。只有当它被选中时,我才想登录到我的web服务。

我的想法是对自定义条件的规则使用过滤器。该nlog docs要求:

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

所以,我可以写一个函数

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

但我没有找到如何在我的过滤器来定义该功能的任何引用。

+0

https://github.com/Emudofus/BehaviorIsManaged/blob/master/Librairies/NLog/Conditions/ConditionMethods.cs – Vladimir

+0

而不是从您的nlog配置文件读取设置我会尝试在运行时更改记录器规则,我希望[这个问题](http://stackoverflow.com/questions/10302561/reconfiguring-nlog-programmatically)会给你一些见解 –

回答

0

最终我只是改变了我的规则来

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

,并在我的代码我只是创建具有此名称的记录。