gpt4 book ai didi

c# - Log4net 对待异常的方式不同于其他日志消息

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:16 25 4
gpt4 key购买 nike

我想为异常设置这样的模式:

  <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%-5level] %date{HH:mm:ss}: %message%newlineException: %exception{message}%newlineStackTrace: %exception{stacktrace}%newline" />
</layout>

虽然我想为非异常设置这样的模式:

  <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%-5level] %date{HH:mm:ss}: %message%newline" />
</layout>

我怎样才能做到这一点?

这与日志级别无关,因为它可能是DEBUGDEBUG-EXCEPTION,以及ERROR错误异常

最佳答案

不完全确定您在寻找什么,但我认为它已经可以做到这一点。只要您像这样记录异常:

try 
{
throw new Exception();
}
catch (Exception ex)
{
Log.Error("There was an exception!", ex)
}

这是 PowerGUI VSX 的示例:

2012-01-25 23:04:21,539 [1] FATAL PowerGui.AddOn.PowerGuiComposer [(null)] - There was an unhandled exception! OMG WTF!
System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'QuickConsole.MainWindow' that matches the specified binding constraints threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Engine, Version=3.1.0.2058, Culture=neutral, PublicKeyToken=a69c32b63191909f' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)

关于c# - Log4net 对待异常的方式不同于其他日志消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com