C# Log4net处理与其他日志消息不同的异常

C# Log4net处理与其他日志消息不同的异常,c#,configuration,log4net,C#,Configuration,Log4net,我想为异常设置如下模式: <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%newlineException: %exception{message}%newlineStackTrace: %exception{stacktrace}%newline" />
  </layout>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%-5level] %date{HH:mm:ss}: %message%newline" />
  </layout>

虽然我想为非例外设置一个类似的模式:

  <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>

我怎样才能做到这一点


这与日志级别无关,因为它可以是
DEBUG
DEBUG-EXCEPTION
,也可以是
ERROR
ERROR-EXCEPTION
不完全确定您要找的是什么,但我认为它已经做到了。只要您像这样记录您的异常:

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)

不完全确定你在找什么,但我想它已经有点像了。只要您像这样记录您的异常:

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)