Configure the .Net Application Loggers

Before you begin, review the Supported Log Frameworks.

For log4net, the following metadata is added to the relevant global or the current scope context and written to the log file.

  • appd_node_id
  • appd_bt_id
  • appd_request_guid

This is an example of log4net log enrichment using an appender.

log4net

<log4net>
<appender name="RollingFile" type="log4net.Appender.FileAppender">
<file value="app.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] appd_node_id=%property{appd_node_id} appd_bt_id=%property{appd_bt_id} appd_request_guid=%property{appd_request_guid} %-5level %logger{1} %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>

For NLogs, the following metadata is injected into the log files.

  • Global scope context
    • appd_node_id
  • Current scope context
    • appd_bt_id
    • appd_request_guid

This is an example of NLog configuration settings that utilize targets to inject metadata into log files.

NLog

<targets>
<!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="allfile" fileName=".\nlog-AspNetCore-all-${shortdate}.log" layout="${longdate}|appd_node_id=${gdc:appd_node_id}|appd_bt_id=${scopeproperty:appd_bt_id}|appd_request_guid=${scopeproperty:appd_request_guid}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" />
</targets>

Serilog

For Serilog, the log enrichment involves augmenting each log message with specific string properties that provide contextual information. These properties include:

  • appd_node_id
  • appd_bt_id
  • appd_request_guid

To capture and display this enriched data in the logs, the output template must incorporate these properties explicitly. For example, the output template must contain:

appd_node_id={appd_node_id} appd_bt_id={appd_bt_id} appd_request_guid={appd_request_guid}

When you configure Serilog by using the Serilog.Settings.Configuration NuGet package with an appsettings.json file, use the example template:

"Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
        {
            "Name": "File",
            "Args": {
                "path": "log.txt",
                "outputTemplate": "[{Timestamp:HH:mm:ss} appd_node_id={appd_node_id} appd_bt_id={appd_bt_id} appd_request_guid={appd_request_guid} {ThreadId} {Level:u3}] {Message:lj}{NewLine}{Exception}"
            }
        }
    ],
    "Enrich": [ "WithThreadId" ]
}