Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

Exception layout renderer

Lixfeld edited this pageMay 24, 2023 ·30 revisions

Exception information provided together with the LogEvent. See alsoHow 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 justmessage.

    • Message - SerializeException.Message
    • Type - SerializeException.GetType().FullName
    • ShortType - SerializeException.GetType().Name
    • ToString - SerializeException.ToString()
    • Method - SerializeException.TargetSite
    • StackTrace - SerializeException.StackTrace
    • Data - Serialize the dictionaryException.Data
    • Source - SerializeException.Source.Introduced in NLog 4.6.7
    • TargetSite - SerializeException.TargetSite.Introduced in NLog 4.6.7
    • HResult - SerializeException.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 flattenAggregateException 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, thenException.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.

varex=newException();ex.Data.Add("data1","val2");throwex;
${exception:format=toString,Data}

Log exception as JSON

the@ format recursively serializes the entire exception hierarchy; combining it withmaxInnerExceptionLevel andinnerExceptionSeparator 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=@}

-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config

Configuration

Programmatic Configuration

Advanced

Extending NLog

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp