Skip to content

An NLog target for MongoDB persistance on the .NET Core platform.

License

Notifications You must be signed in to change notification settings

lukpep/NLog.MongoDB.NetCore

 
 

Repository files navigation

NLog.MongoDB.NetCore

NuGet Build status Build Status Join the chat at https://gitter.im/NLogMongoDBNetCore/NLogMongoDBNetCore

An NLog target for MongoDB persistance on the .NET Core platform.

Download

The NLog.MongoDB.NetCore library is available on nuget.org via package name NLog.MongoDB.NetCore.

To install NLog.MongoDB.NetCore, run the following command in the Package Manager Console

PM> Install-Package NLog.MongoDB.NetCore

More information about NuGet package avaliable at https://nuget.org/packages/NLog.MongoDB.NetCore

Configuration Syntax

<extensions>
  <add assembly="NLog.MongoDB.NetCore"/>
</extensions>

<targets>
  <target xsi:type="Mongo"
          name="String"
          connectionName="String"
          connectionString="String"
          collectionName="String"
          cappedCollectionSize="Long"
          cappedCollectionMaxItems="Long"
          databaseName="String"
          includeDefaults="Boolean">
    
    <!-- repeated --> 
    <field name="String" layout="Layout" bsonType="Boolean|DateTime|Double|Int32|Int64|String"  />
    
    <!-- repeated --> 
    <property name="String" layout="Layout" bsonType="Boolean|DateTime|Double|Int32|Int64|String"  />
  </target>
</targets>

Parameters

General Options

name - Name of the target.

Connection Options

connectionName - The name of the connection string to get from the config file.

connectionString - Connection string. When provided, it overrides the values specified in connectionName.

databaseName - The name of the database, overrides connection string database.

Collection Options

collectionName - The name of the MongoDB collection to write logs to.

cappedCollectionSize - If the collection doesn't exist, it will be create as a capped collection with this max size.

cappedCollectionMaxItems - If the collection doesn't exist, it will be create as a capped collection with this max number of items. cappedCollectionSize must also be set when using this setting.

Document Options

includeDefaults - Specifies if the default document is created when writing to the collection. Defaults to true.

field - Specifies a root level document field. There can be multiple fields specified.

property - Specifies a dictionary property on the Properties field. There can be multiple properties specified.

Examples

Default Configuration with Extra Properties

NLog.config target

<target xsi:type="Mongo"
        name="mongoDefault"
        connectionString="mongodb://localhost/Logging"
        collectionName="DefaultLog"
        cappedCollectionSize="26214400">
  <property name="ThreadID" layout="${threadid}" bsonType="Int32" />
  <property name="ThreadName" layout="${threadname}" />
  <property name="ProcessID" layout="${processid}" bsonType="Int32" />
  <property name="ProcessName" layout="${processname:fullName=true}" />
  <property name="UserName" layout="${windows-identity}" />
</target>

Complete Custom Document

NLog.config target

<target xsi:type="Mongo"
        name="mongoCustom"
        includeDefaults="false"
        connectionString="mongodb://localhost"
        collectionName="CustomLog"
        databaseName="Logging"
        cappedCollectionSize="26214400">
  <field name="Date" layout="${date}" bsonType="DateTime" />
  <field name="Level" layout="${level}"/>
  <field name="Message" layout="${message}" />
  <field name="Logger" layout="${logger}"/>
  <field name="Exception" layout="${exception:format=tostring}" />
  <field name="ThreadID" layout="${threadid}" bsonType="Int32" />
  <field name="ThreadName" layout="${threadname}" />
  <field name="ProcessID" layout="${processid}" bsonType="Int32" />
  <field name="ProcessName" layout="${processname:fullName=true}" />
  <field name="UserName" layout="${windows-identity}" />
</target>

Custom Output JSON

{
    "_id": {
        "$oid": "594b8db3b65a8d0db4204123"
    },
    "Date": {
        "$date": "2017-06-22T09:28:19.863Z"
    },
    "Level": "Info",
    "Message": "LogLevel.Info: , k=1071, l=1299",
    "Logger": "NLog.MongoDB.NetCore.ConsoleTest.Program",
    "ThreadID": 1,
    "ProcessID": 3508,
    "ProcessName": "D:\\SPECIALS\\PROJECTS\\NLog.MongoDB.NetCore\\tests\\NLog.MongoDB.NetCore.ConsoleTest\\bin\\Debug\\netcoreapp1.0\\win10-x64\\NLog.MongoDB.NetCore.ConsoleTest.exe",
    "UserName": "kaan.has"
}

About

An NLog target for MongoDB persistance on the .NET Core platform.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 95.1%
  • PowerShell 4.9%