项目作者: goto10hq

项目描述 :
Nlog target: DocumentDB (CosmosDB)
高级语言: C#
项目地址: git://github.com/goto10hq/NLog.DocumentDB.git
创建时间: 2016-05-27T13:40:45Z
项目社区:https://github.com/goto10hq/NLog.DocumentDB

开源协议:MIT License

下载


NLog.DocumentDB

NLog.DocumentDB target

Software License
Latest Version on NuGet
NuGet
.NETStandard 2.0
Build status

Extends NLog with a new target: Azure DocumentDB (CosmosDB). Easy to use with a nice default layout.

Install using Nuget Package Manager

Install-Package Nlog.DocumentDBTarget

Basic configuration

Just define connection string for DocumentDB and you are ready to go.

  1. <nlog>
  2. <extensions>
  3. <add assembly="NLog.DocumentDBTarget" ></add>
  4. </extensions>
  5. <targets>
  6. <target name="sample" type="DocumentDB" endPoint="https://myendpoint.documents.azure.com:443" authorizationKey="s0mes3cre7StuFf==" database="mydb" collection="mycollection"></target>
  7. </targets>
  8. <rules>
  9. <logger name="*" minlevel="Trace" writeTo="sample" ></logger>
  10. </rules>
  11. </nlog>

Custom layout configuration

You can define your custom JSON document. Keep in mind you have to use JsonLayout. Otherwise logger falls back to the default layout.

  1. <target name="custom document" type="DocumentDB" endPoint="https://myendpoint.documents.azure.com:443" authorizationKey="s0mes3cre7StuFf==" database="mydb" collection="mycollection"></target>
  2. <layout type="JsonLayout">
  3. <attribute name="time" layout="${longdate}" ></attribute>
  4. <attribute name="timeEpoch" layout="${epoch}" ></attribute>
  5. <attribute name="level" layout="${level:upperCase=true}"></attribute>
  6. <attribute name="message" layout="${message}" ></attribute>
  7. </layout>
  8. </target>

Own renderers

{epoch}

Classic UNIX epoch. Useful for querying data from DocumentDB since datetime is not natively supported.

{application}

You can set application name in the target configuration:

  1. <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.

{entity}

You can set entity name in the target configuration:

  1. <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.

Layouts

Default layout looks like that.

  1. <layout type="JsonLayout">
  2. <attribute name="application" layout="${application}" ></attribute>
  3. <attribute name="logged" layout="${date}" ></attribute>
  4. <attribute name="loggedEpoch" layout="${epoch}" ></attribute>
  5. <attribute name="level" layout="${level}" ></attribute>
  6. <attribute name="message" layout="${message}" ></attribute>
  7. <attribute name="identity" layout="${identity}" ></attribute>
  8. <attribute name="serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" ></attribute>
  9. <attribute name="port" layout="${aspnet-request:serverVariable=SERVER_PORT}" ></attribute>
  10. <attribute name="url" layout="${aspnet-request:serverVariable=HTTP_URL}" ></attribute>
  11. <attribute name="serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" ></attribute>
  12. <attribute name="remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" ></attribute>
  13. <attribute name="logger" layout="${logger}" ></attribute>
  14. <attribute name="machineName" layout="${machineName}" ></attribute>
  15. <attribute name="exception" layout="${exception:tostring}" ></attribute>
  16. <!-- if entity property is set -->
  17. <!-- <attribute name="entity" layout="${entity}" ></attribute> -->
  18. </layout>