【问题标题】:Disabling / Enabling nlog rules at runtime在运行时禁用/启用 nlog 规则
【发布时间】:2013-10-15 00:51:01
【问题描述】:

我正在使用带有 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" />

现在我不允许用户决定是否要使用此功能。 该应用程序是一个 winforms 应用程序,用户会看到一个未处理异常的对话框。现在我想添加一个errorreporting 复选框。只有选中它,我才想登录我的网络服务。

我的想法是为我的规则使用自定义条件的过滤器。 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;
}

但我没有找到任何关于如何在我的过滤器中定义此函数的参考。

【问题讨论】:

标签: c# .net nlog


【解决方案1】:

最终我只是将规则更改为

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

在我的代码中,我只是创建了一个具有此名称的记录器。

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多