Nlog target: DocumentDB (CosmosDB)
Extends NLog with a new target: Azure DocumentDB (CosmosDB). Easy to use with a nice default layout.
Install-Package Nlog.DocumentDBTarget
Just define connection string for DocumentDB and you are ready to go.
<nlog>
<extensions>
<add assembly="NLog.DocumentDBTarget" ></add>
</extensions>
<targets>
<target name="sample" type="DocumentDB" endPoint="https://myendpoint.documents.azure.com:443" authorizationKey="s0mes3cre7StuFf==" database="mydb" collection="mycollection"></target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="sample" ></logger>
</rules>
</nlog>
You can define your custom JSON document. Keep in mind you have to use JsonLayout. Otherwise logger falls back to the default layout.
<target name="custom document" type="DocumentDB" endPoint="https://myendpoint.documents.azure.com:443" authorizationKey="s0mes3cre7StuFf==" database="mydb" collection="mycollection"></target>
<layout type="JsonLayout">
<attribute name="time" layout="${longdate}" ></attribute>
<attribute name="timeEpoch" layout="${epoch}" ></attribute>
<attribute name="level" layout="${level:upperCase=true}"></attribute>
<attribute name="message" layout="${message}" ></attribute>
</layout>
</target>
Classic UNIX epoch. Useful for querying data from DocumentDB since datetime is not natively supported.
You can set application name in the target configuration:
<target name="custom" type="DocumentDB" application="Calculator" ...>
If leaved empty than logger try to set ${iis-site-name}, if empty (non web application then ${processname} is used.
You can set entity name in the target configuration:
<target name="custom" type="DocumentDB" entity="logger/log" ...>
It’s for our internal purposes in Goto10 when we add this field to all the documents. You can freely ignore it.
Default layout looks like that.
<layout type="JsonLayout">
<attribute name="application" layout="${application}" ></attribute>
<attribute name="logged" layout="${date}" ></attribute>
<attribute name="loggedEpoch" layout="${epoch}" ></attribute>
<attribute name="level" layout="${level}" ></attribute>
<attribute name="message" layout="${message}" ></attribute>
<attribute name="identity" layout="${identity}" ></attribute>
<attribute name="serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" ></attribute>
<attribute name="port" layout="${aspnet-request:serverVariable=SERVER_PORT}" ></attribute>
<attribute name="url" layout="${aspnet-request:serverVariable=HTTP_URL}" ></attribute>
<attribute name="serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" ></attribute>
<attribute name="remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" ></attribute>
<attribute name="logger" layout="${logger}" ></attribute>
<attribute name="machineName" layout="${machineName}" ></attribute>
<attribute name="exception" layout="${exception:tostring}" ></attribute>
<!-- if entity property is set -->
<!-- <attribute name="entity" layout="${entity}" ></attribute> -->
</layout>