NLog Layout that formats log events in accordance with Elastic Common Schema (ECS).
$ dotnet add package Elastic.CommonSchema.NLogThis Layout implementation formats an NLog event into a JSON representation that adheres to the Elastic Common Schema specification.
The .NET assemblies are published to NuGet under the package name Elastic.CommonSchema.NLog
var config = new LoggingConfiguration();
var consoleTarget = new ConsoleTarget("console") { Layout = new EcsLayout() }; // Use the ECS layout.
config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget);
LogManager.Configuration = config;
var logger = LogManager.GetCurrentClassLogger();
The sample above uses EcsLayout with the NLog console target, but you are free to use any target of your choice, perhaps consider
the NLog FileTarget and Elastic Filebeat for durable and reliable ingestion.
<nlog>
<extensions>
<add assembly="Elastic.CommonSchema.NLog"/>
</extensions>
<targets>
<target name="console" xsi:type="console">
<layout xsi:type="EcsLayout">
<metadata name="MyProperty" layout="MyPropertyValue" /> <!-- repeated, optional -->
<label name="MyLabel" layout="MyLabelValue" /> <!-- repeated, optional -->
<tag layout="MyTagValue" /> <!-- repeated, optional -->
</layout>
</target>
</targets>
<rules>
<logger name="*" minLevel="Debug" writeTo="Console" />
</rules>
</nlog>
Metadata Options
truefalseEvent Options
Agent Options
Process Options
${processname:FullName=true}${processid}${processname:FullName=false}${processinfo:MainWindowTitle}${threadid}Server Options
${environment-user}Host Options
${machinename}Log Origin Options
${exception:format=method}${exception:format=source}Http Options
${aspnet-trace-identifier}${aspnet-request-method}${aspnet-request-contentlength}${aspnet-request-referrer}${aspnet-response-statuscode}Url Options
${aspnet-request-url:IncludeScheme=true:IncludeHost=false:IncludePath=false}${aspnet-request-url:IncludeScheme=false:IncludeHost=true:IncludePath=false}${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=true}${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=false:IncludePort=true}${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=false:IncludeQueryString=true}${aspnet-user-identity}Trace Options
System.Diagnostics.Activity.Current.TraceIdSystem.Diagnostics.Activity.Current.SpanIdTransaction Options
${ElasticApmTransactionId}Additionally any valid ECS log template properties that is available under LogTemplateProperties.* e.g LogTemplateProperties.TraceId
is supported and will directly set the appropriate ECS fields.
logger.Info("The time is {TraceId}", "my-trace-id");
Will override trace.id on the resulting ECS json document.
An example of the output is given below:
{
"@timestamp": "2020-02-20T16:07:06.7109766+11:00",
"log.level": "Info",
"message": "Info \"X\" 2.2",
"ecs.version": "9.0.0",
"log": {
"logger": "Elastic.CommonSchema.NLog.Tests.LogTestsBase"
},
"labels": {
"ValueX": "X"
},
"agent": {
"type": "Elastic.CommonSchema.NLog",
"version": "1.6.0"
},
"event": {
"created": "2020-02-20T16:07:06.7109766+11:00",
"severity": 6,
"timezone": "Romance Standard Time"
},
"host": {
"ip": [ "127.0.0.1" ],
"name": "LOCALHOST"
},
"process": {
"executable": "C:\\Program Files\\dotnet\\dotnet.exe",
"name": "dotnet",
"pid": 17592,
"thread.id": 17592,
"title": "15.0.0.0"
},
"server": { "user": { "name": "MyUser" } },
"service": {
"name": "Elastic.CommonSchema",
"type": "dotnet",
"version": "1.6.0"
},
"metadata": {
"SomeY": 2.2
},
"MessageTemplate": "Info {ValueX} {SomeY} {NotX}"
}
This software is Copyright (c) 2014-2020 by Elasticsearch BV.
This is free software, licensed under: The Apache License Version 2.0.