Exception information provided together with the LogEvent. See also How to properly log exceptions.
Platforms Supported: All
${exception:format=String:innerFormat=String:maxInnerExceptionLevel=Integer:innerExceptionSeparator=String
:separator=String:exceptionDataSeparator=string}
format - Format of the output. Default: ToString,Data
. Must be a comma-separated list of exception properties:
Notice before NLog 5.0, then default was just
message
.
Exception.Message
Exception.GetType().FullName
Exception.GetType().Name
Exception.ToString()
Exception.TargetSite
Exception.StackTrace
Exception.Data
Exception.Source
. Introduced in NLog 4.6.7Exception.TargetSite
. Introduced in NLog 4.6.7Exception.HResult
. Introduced in NLog 4.6.8innerFormat - Format of the output of inner exceptions. Must be a comma-separated list of exception properties (As shown above). This parameter value is case-insensitive.
maxInnerExceptionLevel - Maximum number of inner exceptions to include in the output. By default inner exceptions are not enabled for compatibility with NLog 1.0. Note that the format @ serializes InnerException like any other property. Integer. Default: 0
separator - Separator used to concatenate parts specified in the Format. Default: single space
innerExceptionSeparator - Separator between inner exceptions. Default: new line
exceptionDataSeparator - Separator used to concatenate the exception data parts. Default: ;
Introduced in NLog 4.3.9
baseException - Render innermost Exception from Exception.GetBaseException(). Boolean. Default: false
Introduced in NLog 4.7.1
flattenException - Automatically flatten AggregateException from async operations to extract its inner exceptions. Boolean. Default: true
Introduced in NLog 4.7.6
If debug symbol information is not available, then Exception.ToString()
will not output source-code filenames or line-numbers. Symbol information from PDB files must be deployed with the application to retrieve and output source-code line-number. For applications compiled in release mode, then inlining can occur and cause "unexpected" line-numbers.
${exception:format=message}
ToString
will also print the innerException:
${exception:format=toString}
Log full (including Data)
Also print exception data, e.g.
var ex = new Exception(); ex.Data.Add("data1", "val2"); throw ex;
${exception:format=toString,Data}
the @ format recursively serializes the entire exception hierarchy; combining it with maxInnerExceptionLevel and innerExceptionSeparator won't do what you want. To get the entire exception hierarchy including innerexceptions as nested objects, just specify :format=@
.
NLog 4.7 allows you to override the object reflection for certain object-types. This can be useful for objects with dangerous properties or complex properties.
LogManager.Setup().SetupSerialization(s => s.RegisterObjectTransformation<System.Net.WebException>(ex => new { Type = ex.GetType().ToString(), Message = ex.Message, StackTrace = ex.StackTrace, Source = ex.Source, InnerException = ex.InnerException, Status = ex.Status, Response = ex.Response.ToString(), // Call your custom method to render stream as string }) );
This can then be used together with:
${exception:format=ToString,Properties,Data}
Or like this for generating JSON:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4