简体   繁体   中英

c# Use NLog with Mongo

i would log my data with NLog and MongoDB. I configured my NLog.config like this:

<extensions>
  <add assembly="NLog.Mongo"/>
</extensions>
...
 <target xsi:type="Mongo"
        name="mongo"
        includeDefaults="false"
        connectionString="mongodb://localhost"
        collectionName="myCollection"
        databaseName="logs"
        cappedCollectionSize="26214400">
  <field name="_id" layout="${Id}"/>
  <field name="ts" layout="${Timestamp}" bsonType="DateTime"/>
  <field name="cap" layout="${ApplicationCaller}" />
</target>

But how can i pass value ID, Timestamp, ApplicationCaller in my log?? I try this code:

public void LogExceptionOnMongo(string callIdentifier, string applicationCaller)
{
     var _myLogger = LogManager.GetLogger("mongo");
     var logEventInfo = new LogEventInfo(LogLevel.Fatal, "", "Exception");
     logEventInfo.Properties["CallIdentifier"] = callIdentifier;
     logEventInfo.Properties["TimeStamp"] = DateTime.UtcNow;
     logEventInfo.Properties["ApplicationCaller"] = applicationCaller;

     _myLogger.Log(logEventInfo);
}

and this configuration:

<target xsi:type="Mongo"
        name="mongo"
        includeDefaults="false"
        connectionString="mongodb://localhost"
        collectionName="myCollection"
        databaseName="logs"
        cappedCollectionSize="26214400">
  <field name="_id" layout="${event-properties:item=CallIdentifier}"/>
  <field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
  <field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>

But i obtain this log in mongo (I don't wont circled data):

在此处输入图片说明

Please can anyone help me?? Thank you and sorry for my English

There would have to be a code change to accommodate this. See Issue for more information.

New property includeEventProperties can now be configured:

<target xsi:type="Mongo"
        name="mongo"
        includeDefaults="false"
        includeEventProperties="false"
        connectionString="mongodb://localhost"
        collectionName="myCollection"
        databaseName="logs"
        cappedCollectionSize="26214400">
  <field name="_id" layout="${event-properties:item=CallIdentifier}"/>
  <field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
  <field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>

Just use ver. 4.6.0.68 from nuget: https://www.nuget.org/packages/NLog.Mongo

This morning i have tried use the new version of library, this is the new configuration:

   <target xsi:type="Mongo"
        name="mongo"
        includeDefaults="false"
        connectionString="mongodb://localhost"
        collectionName="myCollection"
        databaseName="logs"
        cappedCollectionSize="26214400"
        includeEventProperties="false">
  <field name="_id" layout="${event-properties:item=CallIdentifier}"/>
  <field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTime"/>
  <field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>

with new tag includeEventProperties=false and with its new property in the class MongoTarget and it works fine. This is the result: 在此处输入图片说明 Thank you so much, great job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM