Nlog变量是否存储相同的值直到会话结束
Does Nlog variables store the same value until the session end
我正在通过变量传递异常消息
LogManager.Configuration.Variables["Exception"] = exception.ToString();
nlog.config
<variable name="Exception" />
<column name="EXCEPTION MESSAGE" layout="${var: Exception}" />
在日志文件中,我创建了一个自定义 nlog class,如上所述,我在其中通过变量传递了一些值。如果我第一次遇到异常,那么它会在下一行一次又一次地写入日志文件,尽管在同一会话的下一行中没有异常,直到我们得到第二个异常。然后第二个例外是在所有下一行中重写。
我该如何解决这个问题?有什么方法可以杀死之前的变量值吗?
您不应在 NLog-Config-Variables 中存储异常(或其他上下文信息)。
您应该这样做:
logger.Error(exception, "Something bad happened");
并将您的配置修改为:
<column name="EXCEPTION MESSAGE" layout="${exception:format=tostring}" />
<column name="EXCEPTION TYPE" layout="${exception:format=type}" />
<column name="EXCEPTION TARGETSITE" layout="${exception:format=Method}" />
另见 https://github.com/NLog/NLog/wiki/Context and https://github.com/NLog/NLog/wiki/Exception-layout-renderer
我正在通过变量传递异常消息
LogManager.Configuration.Variables["Exception"] = exception.ToString();
nlog.config
<variable name="Exception" />
<column name="EXCEPTION MESSAGE" layout="${var: Exception}" />
在日志文件中,我创建了一个自定义 nlog class,如上所述,我在其中通过变量传递了一些值。如果我第一次遇到异常,那么它会在下一行一次又一次地写入日志文件,尽管在同一会话的下一行中没有异常,直到我们得到第二个异常。然后第二个例外是在所有下一行中重写。
我该如何解决这个问题?有什么方法可以杀死之前的变量值吗?
您不应在 NLog-Config-Variables 中存储异常(或其他上下文信息)。
您应该这样做:
logger.Error(exception, "Something bad happened");
并将您的配置修改为:
<column name="EXCEPTION MESSAGE" layout="${exception:format=tostring}" />
<column name="EXCEPTION TYPE" layout="${exception:format=type}" />
<column name="EXCEPTION TARGETSITE" layout="${exception:format=Method}" />
另见 https://github.com/NLog/NLog/wiki/Context and https://github.com/NLog/NLog/wiki/Exception-layout-renderer