Help me please to format exception logged with file logger
I would like to have exceptions to be logged with new line separator between inner exceptions (if any) with exception type surrounded with some special sort of symbols, like [ArgumentException] to make it different from error text, and exceptions to be intended with tab just to make it easy to read stack traces
I've checked the latest NLog 4.4.12 package but the problem is that it is not so easy to parameterize default layout to get something like
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
This is exception file log that I'd like to get. I've tried with layout like
layout="${longdate} ${logger} ${message}${onexception:${newline}${exception:format=tostring}}"
it uses standard tostring exception method but the result is not the same. I do not like that
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
Inner exception messages stack trace are detached in the log (so it is hard to read log later) in case if exception is re-thrown more then twice and there is no tab (space) before exception type name.
The best result I've got was with layout below
layout="${longdate} ${logger} ${message}${onexception:${newline}${exception:maxInnerExceptionLevel=10:format=shortType,message,stacktrace:separator=*:innerExceptionSeparator=
	}}"
and it is
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
However it is very hard to read. First stack trace line is on the same line as exception message. Inner error message is intended but inner stack trace - no.
Is there anything in layout that I've missed ? Should I create custom exception layout renderer ? Here is c# source code that I've used to raise exception