Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sikamikan opened this issue Feb 13, 2019 · 2 comments

Comments

@sikamikan
Copy link

sikamikan commented Feb 13, 2019

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

`

<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!

@falaque
Copy link

falaque commented Feb 19, 2019

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

@sikamikan
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants