Skip to content

Exception layout renderer

Lixfeld edited this page May 24, 2023 · 30 revisions

Exception information provided together with the LogEvent. See also How to properly log exceptions.

Platforms Supported: All

Configuration Syntax

${exception:format=String:innerFormat=String:maxInnerExceptionLevel=Integer:innerExceptionSeparator=String
           :separator=String:exceptionDataSeparator=string}

Parameters

Rendering Options

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

    • Message - Serialize Exception.Message
    • Type - Serialize Exception.GetType().FullName
    • ShortType - Serialize Exception.GetType().Name
    • ToString - Serialize Exception.ToString()
    • Method - Serialize Exception.TargetSite
    • StackTrace - Serialize Exception.StackTrace
    • Data - Serialize the dictionary Exception.Data
    • Source - Serialize Exception.Source. Introduced in NLog 4.6.7
    • TargetSite - Serialize Exception.TargetSite. Introduced in NLog 4.6.7
    • HResult - Serialize Exception.HResult. Introduced in NLog 4.6.8
    • Properties - Appends any additional properties that specific type of Exception might have. Introduced in NLog 4.6.8
    • @ - Serialize all Exception-properties into Json-format. Introduced in NLog 4.5
  • innerFormat - 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

Notes

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.

Examples

Log only message

${exception:format=message}

Log full (without Data)

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}

Log exception as JSON

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=@.

${exception:format=@}

Custom Exception Serialization

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:

${exception:format=@}
Clone this wiki locally