c# - NLog 自定义布局渲染器异常
问题描述
我有以下自定义布局渲染器:
[LayoutRenderer("ignore-exception")]
public class ExceptionLoggingRenderer : LayoutRenderer
{
/// <summary>
/// What exception was thrown (used to check along with the Code)
/// </summary>
[RequiredParameter]
public string Exception { get; set; }
/// <summary>
/// What error code was thrown (used to check along with the Exception)
/// </summary>
[RequiredParameter]
public string Code { get; set; }
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(IgnoreLoggingToDb(Exception, Code));
}
private bool IgnoreLoggingToDb(string exception, string code)
{
if (exception == "whatever" && code == "123")
return true;
else return false;
}
}
此外,以下过滤器:
<filters>
<whenEqual ignoreCase="true" layout="${ignore-exception:Code=82:Exception=${exception}}"
action="Ignore" compareTo="true"/>
<when condition="'${ignore-exception:Code=82:Exception=${exception}}'"
action="Ignore"/>
</filters>
调用记录器:
Log.Error().Exception(e).Property("Code", (int)e.Code).Write()
这些过滤器完美地进入了自定义布局渲染器,但是其中的属性变成Exception
了ExceptionLoggingRenderer
“${exception”,即${exception} 中的第二个大括号被视为我的自定义布局渲染器的结束符。
我无法逃脱或找到另一种方法让 ${exception} 布局渲染器在我的自定义布局渲染器中工作<when condition="..." action="Ignore">
。
任何帮助表示赞赏!
解决方案
我找到了一种解决方案,可以避免将布局渲染器用于自定义布局渲染器的参数,该参数采用${exception}
. 我只是使用一个LogEventInfo.Exception
覆盖的 Append 方法:
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var whatever = logEvent.Exception;
}
推荐阅读
- html - css:多种效果导致翻转无效
- linux - 插入系统时自动启动驻留在 USB 上的 bash 脚本以获取 USB 信息
- c++ - 是否存在非托管结构可以调用包含它的托管类的析构函数的情况?
- sql - 存储单圈时间 SQL Server
- c++ - 为什么 std::atomic_fetch 将指针作为其输入参数
- c# - UTF8 中的 WebClient DownloadString 不返回法语字符
- amazon-web-services - 如何使用 Node.js 从 AWS Lambda 中的 /tmp 文件夹将 WAV 文件上传到 S3
- html - 我怎么把
内容 在最小高度的 div 底部? - javascript - a11y 可作为按钮访问的表单输入
- haskell - 在 Haskell 中建模异构图