Skip to content

saleslessa/NLog.LiteDB

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NLog.LiteDb

NLog target for the LiteDB database

Build status

Configuration Syntax

Examples below for the 4 connection string types:

  <targets>
    <!-- Legacy Target still supported-->
      <target name="legacy" xsi:type="liteDBTarget"
              connectionString="filename=NLog.db"
              collectionName="DefaultLog">
        <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>
    <target name="special" xsi:type="liteDBTarget"
            connectionString="special={MyDocuments}\testApp\NLog.db"
            collectionName="DefaultLog" IsJournaling="false">
      <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>
    <target name="path" xsi:type="liteDBTarget"
        connectionString="path=c:\temp\testApp\NLog.db"
        collectionName="DefaultLog" IsJournaling="false">
      <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>
    <target name="file" xsi:type="liteDBTarget"
    connectionString="file=NLog.db"
    collectionName="DefaultLog" IsJournaling="false">
      <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>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="special" />
    <logger name="*" minlevel="Info" maxlevel="Warn" writeTo="path" />
    <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="file" />
  </rules>

Parameters

General Options

name - Name of the target.

Connection Options

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

connectionString - 4 connection string types are permitted.

  • Special - this allows for the connection string to utilize special folders - Refer to Microsoft documentation on Environment.SpecialFolder Enum for a full list. They are case sensitive.
  • Path - as this suggests - a file path.
  • File - just a base filename.
  • Legacy - for backwards compatiblity.

Collection Options

collectionName - The name of the LiteDB collection to write logs to.
IsJournaling - Journaling is enabled by default. Specify IsJournaling="false" to disable LiteDB journaling.

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 name="liteDB" xsi:type="liteDBTarget"
            connectionString="file=NLog.db"
            collectionName="DefaultLog">        
      <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>

NLog.config target (LiteDB journal turned off)

    <target name="liteDB" xsi:type="liteDBTarget"
            connectionString="file=NLog.db"
            collectionName="DefaultLog" IsJournaling="false">        
      <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>

or

    <target name="legacy" xsi:type="liteDBTarget"
            connectionString="filename=NLog.db;journal=false"
            collectionName="DefaultLog">        
      <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":{"$oid":"58aa0e644a8392ac98bb4812"},
    "Date":{"$date":"2017-02-19T21:30:12.4760000Z"},
    "Level":"Error",
    "Logger":"NLog.LiteDB.Specs.IntegrationTests.IntegrationTests",
    "Message":"Test Log Message",
    "Exception":
    {
      "Message":"Test Exception",
      "BaseMessage":"Inner Exception",
      "Text":"System.Exception: Test Exception ---> System.Exception: Inner Exception\r\n   --- End of inner exception stack trace ---",
      "Type":"System.Exception",
      "Source":null
    },
      "Properties":
      {
        "ThreadID":"10",
        "ProcessID":"44184",
        "ProcessName":"-Information about process here ",
        "UserName":"MachineName\\user"
      }
    }
}

About

NLog target for the LiteDB database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%