Using Nlog to log from my asp.net Core application, I would like to review the logs in Datadog.
Datadog allows me to visualize the log data, and slice, search, select and sort logs in a convenient way to provide support to my customers.
I was looking for a way to use NLog to directly post to the Datadog API, so I do not need to use the Windows Agent to collect the logs.
Below how to do this, as I could not find the answer anywhere.
In Datadog, create an API key in Integrations, APIs. Give the API key a name.
In NLog.config create a target. The url is either datadoghq.com or datadoghq.eu (for europe).
<target xsi:type="WebService"
name="datadog"
url="https://http-intake.logs.datadoghq.com/v1/input"
encoding="utf-8"
protocol="JsonPost"
preAuthenticate="false" >
<parameter name='date' type='System.String' layout='${longdate}'/>
<parameter name='ipaddress' type='System.String' layout='${aspnet-request-ip}'/>
<parameter name="userid" type='System.String' layout="${aspnet-User-Identity}" />
<parameter name="level" type='System.String' layout="${level:upperCase=true}"/>
<parameter name="version" type='System.String' layout="${configsetting:name=VersionSettings.Version:default=?}" />
<parameter name="threadid" type='System.String' layout="${threadid}" />
<parameter name="controller" type='System.String' layout="${aspnet-mvc-controller}" />
<parameter name="class" type='System.String' layout="${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}" />
<parameter name="message" type='System.String' layout="${message}" />
<parameter name="elapsed" type='System.String' layout ="${event-properties:item=elapsed}" />
<parameter name="service" type='System.String' layout="${configsetting:name=Nlog.Component}" />
<parameter name="hostname" type='System.String' layout="${configsetting:name=Nlog.HostName}" />
<parameter name="exception" type='System.String' layout="${exception:format=ToString}" />
<parameter name="ddsource" type='System.String' layout="csharp" />
<header name="DD-API-KEY" layout="${configsetting:name=Nlog.datadog}"/>
<header name="Content-Type" layout="application/json"/>
</target>
Now create a rule to write to the target and you are done!
All of the parameters can be configured to become columns in Datadog, and/or facets to select on. I am using a date parameter so that the date matches other logs, rather than displaying the built in date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With