Skip to content

Callsite layout renderer

Rolf Kristensen edited this page Sep 17, 2023 · 35 revisions

CallSite outputs the source-file-name and source-line-number where a LogEvent occurred. NLog scans the callstack and output the method-stackframe just before the NLog Logger takes over.

Platforms Supported: Limited (Not supported on NetStandard 1.3)

Configuration Syntax

${callsite:className=Boolean:fileName=Boolean:includeSourcePath=Boolean:methodName=Boolean}

Parameters

Rendering Options

  • className - Indicates whether to render the class name. Boolean Default: True
  • includeNamespace - Include namespace in class name. Boolean Default: True

    Introduced with NLog v4.4

  • fileName - Indicates whether to render the source file name and line number.Boolean Default: False
  • includeSourcePath - Indicates whether to include source file path.Boolean Default: True
  • methodName - Indicates whether to render the method name.Boolean Default: True
  • cleanNamesOfAnonymousDelegates - Indicates whether the method name will be cleaned up if it is detected as an anonymous delegate. Boolean Default: True

    Introduced with NLog v4.3.9. Before NLog 5.0 the default value was False.

  • cleanNamesOfAsyncContinuations - Indicates whether the method and class names will be cleaned up if it is detected as an async continuation. Boolean Default: True

    Introduced with NLog v4.5. Before NLog 5.0 the default value was False.

  • skipFrames - The number of frames to skip. Integer Default: 0
  • captureStackTrace - Logger should capture StackTrace, if it was not provided manually. Default: true

    Introduced with NLog 4.7.1

Notes

The Callsite-LayoutRenderer infers a heavy performance hit when doing lots of logging, as it has to capture full StackTrace for every log message. NLog 5.0 makes it possible to skip the StackTrace-capture, when using LogEventInfo.SetCallerInfo(...) with input from Caller Information-attributes.

NLog 5.0 provides Fluent-Logger-API for easy capture of caller-information, and provides fast logging when using ${callsite:captureStackTrace=false}:

_logger.ForInfoEvent()
       .Message("This is a fluent message {0}", "test")
       .Property("PropertyName", "PropertyValue")
       .Log();

If using Custom wrapper for NLog Logger, then make sure to provide the typeof(OwnWrapper) as input, when calling NLog Logger:

Logger.Log(Type wrapperType, LogEventInfo logEvent)

Alternative if the custom Logger-wrapper comes from its own dedicated Assembly, then one can exclude the entire Assembly:

NLog.LogManager.AddHiddenAssembly(typeof(OwnWrapper).Assembly);

If symbol information is not available, then it will not be able to output source-code filename or line-number. 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.

If using Deterministic builds then it will generate symbol information with relative paths:

  <PropertyGroup>
    <Deterministic>true</Deterministic>
    <PathMap>$(SolutionDir)=./</PathMap>
    <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
  </PropertyGroup>
Clone this wiki locally