有没有办法可以从日志系统中排除自定义异常类?我知道我可以按类别过滤掉,但我只想保留一个自制的异常类。
问问题
481 次
1 回答
2
我看不到内置过滤器的方法。但是您可以将NLog与 ASP.NET Core 日志记录系统一起使用。您编写日志消息的方式与以前相同,但它为您提供了更广泛的配置,包括过滤特定异常类型的方法。
样本
让我们创建一个nlog.config
包含两个目标日志文件:一个包含所有消息,包括不需要的异常类型,另一个不包含此类型:
<targets>
<target xsi:type="File"
name="all"
fileName="all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<target xsi:type="File"
name="filtered"
fileName="filtered-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
</targets>
不需要的异常类型的排除是在日志记录规则中配置的。在这个例子中,我们过滤异常类型SampleApplication.Exceptions.SampleException
:
<rules>
<logger name="*" minlevel="Trace" writeTo="all" />
<logger name="*" minlevel="Trace" writeTo="filtered">
<filters>
<when condition="contains('${exception:format=Type}', 'SampleApplication.Exceptions.SampleException')"
action="Ignore" />
</filters>
</logger>
</rules>
于 2019-09-01T20:31:39.140 回答