Skip to content

Latest commit

 

History

History
219 lines (174 loc) · 7.49 KB

README.md

File metadata and controls

219 lines (174 loc) · 7.49 KB

NLog.Mongo

Writes NLog messages to MongoDB.

Build status

NuGet Version

Coverage Status

Download

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

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

PM> Install-Package NLog.Mongo

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

Configuration Syntax

<extensions>
  <add assembly="NLog.Mongo"/>
</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.

includeEventProperties - Specifies if LogEventInfo Properties should be automatically included. Defaults to true.

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>

Default Output JSON

{
    "_id" : ObjectId("5184219b545eb455aca34390"),
    "Date" : ISODate("2013-05-03T20:44:11Z"),
    "Level" : "Error",
    "Logger" : "NLog.Mongo.ConsoleTest.Program",
    "Message" : "Error reading file 'blah.txt'.",
    "Exception" : {
        "Message" : "Could not find file 'C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\blah.txt'.",
        "Text" : "System.IO.FileNotFoundException: Could not find file 'C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\blah.txt' ...",
        "Type" : "System.IO.FileNotFoundException",
        "Source" : "mscorlib",
        "MethodName" : "WinIOError",
        "ModuleName" : "mscorlib",
        "ModuleVersion" : "4.0.0.0"
    },
    "Properties" : {
        "ThreadID" : 10,
        "ProcessID" : 21932,
        "ProcessName" : "C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\NLog.Mongo.ConsoleTest.exe",
        "UserName" : "pwelter",
        "Test" : "ErrorWrite",
        "CallerMemberName" : "Main",
        "CallerFilePath" : "c:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\Program.cs",
        "CallerLineNumber" : "43"
    }
}

Custom Document Fields

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 Document Fields JSON output

{
    "_id" : ObjectId("5187abc2545eb467ecce9184"),
    "Date" : ISODate("2015-02-02T17:31:20.728Z"),
    "Level" : "Debug",
    "Message" : "Sample debug message",
    "Logger" : "NLog.Mongo.ConsoleTest.Program",
    "ThreadID" : 9,
    "ProcessID" : 26604,
    "ProcessName" : "C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\v4.5\\NLog.Mongo.ConsoleTest.exe",
    "UserName" : "pwelter"
}

Custom Object Properties

NLog.config target

<target xsi:type="Mongo"
        name="mongoCustomJsonProperties"
        includeEventProperties="false"
        connectionString="mongodb://localhost"
        collectionName="CustomLog"
        databaseName="Logging"
        cappedCollectionSize="26214400">
    <field name="Properties" bsonType="Object">
        <layout type="JsonLayout" includeAllProperties="true" includeMdlc="true" maxRecursionLimit="10">
            <attribute name="ThreadID" layout="${threadid}" encode="false" />
            <attribute name="ProcessID" layout="${processid}" encode="false" />
            <attribute name="ProcessName" layout="${processname:fullName=false}" />
        </layout>
    </field>
</target>

Custom Object Properties JSON output

{
    "_id" : ObjectId("5184219b545eb455aca34390"),
    "Date" : ISODate("2013-05-03T20:44:11Z"),
    "Level" : "Error",
    "Logger" : "NLog.Mongo.ConsoleTest.Program",
    "Message" : "Error reading file 'blah.txt'.",
    "Exception" : {
        "Message" : "Could not find file 'C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\blah.txt'.",
        "Text" : "System.IO.FileNotFoundException: Could not find file 'C:\\Projects\\github\\NLog.Mongo\\Source\\NLog.Mongo.ConsoleTest\\bin\\Debug\\blah.txt' ...",
        "Type" : "System.IO.FileNotFoundException",
        "Source" : "mscorlib",
        "MethodName" : "WinIOError",
        "ModuleName" : "mscorlib",
        "ModuleVersion" : "4.0.0.0"
    },
    "Properties" : {
        "ThreadID" : 10,
        "ProcessID" : 21932,
        "ProcessName" : "NLog.Mongo.ConsoleTest.exe",
        "Product": { "Name": "Foo", "Id": 42 }
    }
}