NLog.Mongo icon indicating copy to clipboard operation
NLog.Mongo copied to clipboard

Logging properties into Mongodb but with extra "properties" object in json

Open sikamikan opened this issue 6 years ago • 2 comments

Hi, I had been trying to save a error trace to mongodb using NLog.Mongo using:

` <nlog internalLogFile="C:/TMC Applications/Logs/Security/nloginternal.log" internalLogLevel="Warn" xmlns="http://www.nlog-project.org/schemas/NLog.netfx40.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<extensions>
  <add assembly="NLog.Mongo"/>
</extensions>
<targets>
  <target name="file"
          xsi:type="File"
          layout="${longdate}|${level}|${processid}|${threadid}|${message} ${exception:format=tostring}"
          fileName="C:/TMC Applications/Logs/Traces/TraceLog.txt"
          archiveFileName="C:/TMC Applications/Logs/Traces/archives/TraceLog.{#####}.txt"
          archiveAboveSize="5242880"
          archiveNumbering="Sequence"
          concurrentWrites="true"
          keepFileOpen="true"
          encoding="iso-8859-2" />
  
  <target name="mongo"
          xsi:type="Mongo"
          includeDefaults="false"
          connectionString="mongodb://localhost:27017/local"
          collectionName="traces" >
    <field name="date" layout="${event-properties:date}"/>
    <field name="applicationId" layout="${event-properties:applicationId}"  />
    <field name="moduleId" layout="${event-properties:moduleId}"  />
    <field name="userId" layout="${event-properties:item=userId}"   />
    <field name="logLevelId" layout="${event-properties:item=logLevelId}"  />
    <field name="browserInfo" layout="${event-properties:item=browserInfo}"  />
    <field name="ip" layout="${event-properties:item=ip}"  />
    <field name="hostName" layout="${event-properties:item=hostName}"  />
    <field name="screenInfo" layout="${event-properties:item=screenInfo}"  />
    <field name="errorCode" layout="${event-properties:item=errorCode}"  />
    <field name="callStack" layout="${event-properties:item=callStack}"  />
    <field name="description" layout="${event-properties:item=description}"  />        
  </target>     
</targets>

<rules>
  <logger name="*" minlevel="Info" writeTo="file" />
  <logger name="*" minlevel="Info" writeTo="mongo"/>
</rules>

`

adding properties with

`public void Save(Trace trace) { var logger = NLog.LogManager.GetCurrentClassLogger(); var traceJson = JsonConvert.SerializeObject(trace);

        LogEventInfo info = new LogEventInfo(LogLevel.Warn, "NLogger", traceJson);
        info.Properties.Add("date", trace.Date);
        info.Properties.Add("applicationId", trace.ApplicationId);
        info.Properties.Add("moduleId", trace.ModuleId);
        info.Properties.Add("userId", trace.UserId);
        info.Properties.Add("logLevelId", trace.LogLevelId);
        info.Properties.Add("browserInfo", trace.BrowserInfo);
        info.Properties.Add("ip", trace.Ip);
        info.Properties.Add("hostName", trace.HostName);
        info.Properties.Add("screenInfo", trace.ScreenInfo);
        info.Properties.Add("errorCode", trace.ErrorCode);
        info.Properties.Add("callStack", trace.CallStack);
        info.Properties.Add("description", trace.Description);
        
        logger.Log(info);
    }`

But receiving this in mongodb database: image Is there a way to remove the last properties object from the json in Mongo?

Thanks!

sikamikan avatar Feb 13 '19 14:02 sikamikan

Hi Use IncludeEventProperties = false in the config. [Like: <target IncludeEventProperties = false ]

falaque avatar Feb 19 '19 13:02 falaque

Hi, thanks for the reply. I already tried with IncludeEventProperties = false but it didnt work. I ended up making a customTarget. Thanks again

sikamikan avatar Feb 19 '19 13:02 sikamikan