NLog 自定义布局渲染器异常
NLog custom layout renderer exception
我有以下自定义布局渲染器:
[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()
这些过滤器可以完美地进入自定义布局渲染器,但是
ExceptionLoggingRenderer
中的 属性 Exception
变为“${exception”,即 ${exception} 中的第二个大括号被视为我的自定义布局渲染器的结束大括号。
我无法通过 <when condition="..." action="Ignore">
.
逃脱或找到另一种让 ${exception} 布局渲染器在我的自定义布局渲染器中工作的方法
感谢任何帮助!
我找到了一种解决方案,可以避免将布局渲染器用于需要 ${exception}
的自定义布局渲染器参数。
我只是使用来自重写的 Append 方法的 LogEventInfo.Exception
:
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var whatever = logEvent.Exception;
}
我认为你的解决方案是最好的,使用LogEventInfo.Exception
但是嵌套的 ${exception}
也可以工作,如果您使用 Layout
类型作为 属性 异常,并使用 logevent 的上下文渲染它:
例如:
[LayoutRenderer("ignore-exception")]
public class ExceptionLoggingRenderer : LayoutRenderer
{
/// <summary>
/// What exception was thrown (used to check along with the Code)
/// </summary>
[RequiredParameter]
public Layout 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)
{
var exceptionString = Exception.Render(logEvent);
}
}
我有以下自定义布局渲染器:
[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()
这些过滤器可以完美地进入自定义布局渲染器,但是
ExceptionLoggingRenderer
中的 属性 Exception
变为“${exception”,即 ${exception} 中的第二个大括号被视为我的自定义布局渲染器的结束大括号。
我无法通过 <when condition="..." action="Ignore">
.
感谢任何帮助!
我找到了一种解决方案,可以避免将布局渲染器用于需要 ${exception}
的自定义布局渲染器参数。
我只是使用来自重写的 Append 方法的 LogEventInfo.Exception
:
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var whatever = logEvent.Exception;
}
我认为你的解决方案是最好的,使用LogEventInfo.Exception
但是嵌套的 ${exception}
也可以工作,如果您使用 Layout
类型作为 属性 异常,并使用 logevent 的上下文渲染它:
例如:
[LayoutRenderer("ignore-exception")]
public class ExceptionLoggingRenderer : LayoutRenderer
{
/// <summary>
/// What exception was thrown (used to check along with the Code)
/// </summary>
[RequiredParameter]
public Layout 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)
{
var exceptionString = Exception.Render(logEvent);
}
}