Nlog.config
<targets> <target type="Console" name="trace" layout="${message}${newline}${onexception:inner=${newline} *****Error***** ${newline} ${exception:format=toString}${exception:format=StackTrace}}"/> </targets>
主要Layout这段,当不是异常的时候显示Message,异常时候显示异常类型和StackTrace
也可加入${stacktrace},平时也可以显示Stacktrace。
private static void aa() { var logger = LogManager.GetCurrentClassLogger(); logger.Info("Hello1"); logger.Info("Hello2"); logger.Info("Hello3"); try{ throw new Exception("Test Exception",new Exception("Inner")); } catch(Exception error) { logger.InfoException("error1",error); } }
输出
Hello1
Hello2 Hello3 error1 *****Error***** System.Exception: Test Exception ---> System.Exception: Inner --- 内部异常堆栈跟踪的结尾 --- 在 testNlog.Program.aa() 在 testNlog.Program.aa()