【发布时间】:2017-10-04 13:04:06
【问题描述】:
请帮助我格式化使用文件记录器记录的异常
我希望在内部异常(如果有的话)之间使用新行分隔符记录异常,异常类型用一些特殊类型的符号包围,例如[ArgumentException],以使其与错误文本不同,以及使用制表符设计的异常只是为了便于阅读堆栈跟踪
我检查了最新的 NLog 4.4.12 包,但问题是参数化默认布局并不是那么容易得到类似的东西
2017-10-04 15:13:22.5823 NLogTest.Program starting
2017-10-04 15:13:22.5823 NLogTest.Program failed to start NLogTest
[ArgumentException] bad try
at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 62
at NLogTest.Program.Main(String[] args) in d:\projects\NLogTest\Program.cs:line 19
[ArgumentException] outer exception
at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 49
at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 57
[KeyNotFoundException] innerException
at NLogTest.Classes.UnitOfWork.innerException() in d:\projects\NLogTest\Program.cs:line 38
at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 45
2017-10-04 15:13:22.5823 NLogTest.Program the end
这是我想要获取的异常文件日志。 我试过像
这样的布局layout="${longdate} ${logger} ${message}${onexception:${newline}${exception:format=tostring}}"
它使用标准的tostring异常方法但结果不一样。我不喜欢这样
2017-10-04 15:28:52.6881 NLogTest.Program failed to start NLogTest
System.ArgumentException: bad try ---> System.ArgumentException: outer exception ---> System.Collections.Generic.KeyNotFoundException: innerException
at NLogTest.Classes.UnitOfWork.innerException() in d:\projects\NLogTest\Program.cs:line 40
at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 47
--- End of inner exception stack trace ---
at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 51
at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 59
--- End of inner exception stack trace ---
at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 64
at NLogTest.Program.Main(String[] args) in d:\projects\NLogTest\Program.cs:line 20
内部异常消息堆栈跟踪在日志中被分离(因此以后很难读取日志)以防异常被重新抛出两次以上 并且异常类型名称前没有制表符(空格)。
我得到的最好结果是下面的布局
layout="${longdate} ${logger} ${message}${onexception:${newline}${exception:maxInnerExceptionLevel=10:format=shortType,message,stacktrace:separator=*:innerExceptionSeparator=
	}}"
它是
2017-10-04 15:49:02.6188 NLogTest.Program failed to start NLogTest
ArgumentException*bad try* at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 64
at NLogTest.Program.Main(String[] args) in d:\projects\NLogTest\Program.cs:line 20
ArgumentException*outer exception* at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 51
at NLogTest.Classes.UnitOfWork.tryException() in d:\projects\NLogTest\Program.cs:line 59
KeyNotFoundException*innerException* at NLogTest.Classes.UnitOfWork.innerException() in d:\projects\NLogTest\Program.cs:line 40
at NLogTest.Classes.UnitOfWork.outerException() in d:\projects\NLogTest\Program.cs:line 47
但是它很难阅读。第一个堆栈跟踪行与异常消息在同一行。内部错误消息是有意的,但内部堆栈跟踪 - 没有。
布局中有什么我错过的吗? 我应该create custom exception layout renderer 吗? Here is我用来引发异常的c#源代码
【问题讨论】: