Giter Site home page Giter Site logo

nlog.mongo's Issues

关于this.AddProperties(bsonDocument, logEvent);内部方法问题

private BsonDocument CreateDocument(LogEventInfo logEvent) { BsonDocument bsonDocument = new BsonDocument(); if (this.IncludeDefaults || this.Fields.Count == 0) { this.AddDefaults(bsonDocument, logEvent); } foreach (MongoField current in this.Fields) { BsonValue value = this.GetValue(current, logEvent); if (value != null) { bsonDocument.set_Item(current.Name, value); } } this.AddProperties(bsonDocument, logEvent); return bsonDocument; }
这个方法能否设为虚方法,以便通过继承重写满足不同的插入到MONGO DB的字段格式,目前当我想把自定义字段加入到mongoDB中时,发现this.AddProperties(bsonDocument, logEvent);会把mongoTarget.Properties与eventInfo.Properties的字段都插入到mongoDB中,eventInfo.Properties存在于mongoDB的Properties键对面对应的字段,如果想输出自定义字段,那么则会造成mongoDB中的字段与Properties键对面对应的字段存在重复字段的情况,需要改善

Add strong name for assembly?

Adding strong name would enable this assembly to be used as dependency for others that have requirement to be strongly signed.

How to use code mode to add fields

var config = new LoggingConfiguration();
var mongoTarget = new MongoTarget();
 config.AddTarget("targetMongo", mongoTarget);
nlog.config
<field name="AppName" layout="${AppName}"/>

First call of logger.Info extremely slow

Hi!

I use NLog 4.5.11, NLog.Mongo 4.6.0.68 and MongoDB 4.0.6

My NLog.Config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="d:\temp\nlog-internal.log">

  <extensions>
    <add assembly="NLog.Mongo"/>
  </extensions>

  <targets>
    <target xsi:type="ColoredConsole" name="logconsole" />

    <target xsi:type="Mongo"
        name="mongoCustom"
        includeDefaults="false"
        connectionString="mongodb://localhost:27017/"
        collectionName="logs"
        databaseName="direct"
        cappedCollectionSize="26214400"
        cappedCollectionMaxItems="100000">
      <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}" />
    </target>

  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="logconsole" />
    <logger name="*" minlevel="Info" writeTo="mongoCustom" />
  </rules>
</nlog>

My code is:

NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
if (args.Length == 0)
{
    logger.Info("This process cannot be started by yourself");
    return;
}
else
{
    logger.Info($"Process Job: TokenId={args[0]}");
}

First call of logger.Info is very slow - 15-20 sec. Next calls are OK.

What I'm doing wrong?
Thanks.

Connection string from appsetting.json

How can I use a conn string from appsettings.json? NLog says that I can do that by using ${configsetting} like this:

<target xsi:type="Mongo"
        name="mongoDefault"
        connectionName=""
        connectionString="${configsetting:DatabaseSettings.ConnString}"
        databaseName="${configsetting:DatabaseSettings.DbName}"
        collectionName="log"
        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>

But when I run the app I got the following error:

Error Error has been raised. Exception: MongoDB.Driver.MongoConfigurationException: The connection string '${configsetting:DatabaseSettings.ConnString}' is not valid.
at MongoDB.Driver.Core.Configuration.ConnectionString.Parse()
at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
at MongoDB.Driver.MongoUrl..ctor(String url)
at NLog.Mongo.MongoTarget.b__46_0(String k)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at NLog.Mongo.MongoTarget.GetCollection()
at NLog.Mongo.MongoTarget.Write(LogEventInfo logEvent)
at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)

Remove try-catch from target single LogEventInfo Write override

Remove try-catch exception-handler from this method:

protected override void Write(LogEventInfo logEvent)

It currently breaks the NLog error-handling with possible retry or fallback, as the exception is silently ignored. The exception should flow to the NLog-engine, that will handle it.

The above rule does not apply for the Write(AsyncLogEventInfo[]), which is implemented perfectly (As you are correctly calling the Continuation-method)

How to add HostIp address

How to add Host IP address in the configuration?
Just like
<field name="Host" layout="${HostIP}" />

User Admin

Hi,
What permissions does the user need in order to write to the log?
It seems that in order to write to the log there is a need to use admin rights on the whole mongo instance. Anything less than that would not write to logs to the mongodb

"Invalid Token" using NLog.Mongo in .Net Core

Upon using the NLog.Mongo in .Net Core Class Library and use it as a reference in a console app it returns the "Invalid Token".
The solution is just the same as what you can see here in the Console test App and setup is just the same.
But when you use this in a .Net Framework Class Library it returns fine.
image

Push latest version to nuget.org

Hi, the version 4.4+ of NLog.Mongo is available in the myget repo but not in the central nuget.org repo. Using the 4.0 version is not an option for me because of the dependency on the legacy driver.

Is there any reason for not pushing NLog.Mongo 4.4+ to nuget.org?

Multi thread and async logging is start very late and missing same logs

I change a worker service log target to MongoDB from file. However, I observe some log lines are missing. Also, writing logs on collection start about a few seconds later. My nlog.config:

<?xml version="1.0" encoding="utf-8"?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="NLog.Mongo"/>
  </extensions>

  <targets>
    <target name="mongoTest" xsi:type="AsyncWrapper">
      <target xsi:type="Mongo"
          includeDefaults="false"
          connectionString="mongodb://localhost:27017/Logging"
          collectionName="CustomLogAsyncSecond"
          cappedCollectionSize="2621440000">
        <field name="Logger" layout="${logger}"/>
        <field name="ProcessName" layout="${processname:fullName=true}" />
        <field name="Date" layout="${longdate}" bsonType="DateTime" />
        <field name="ThreadID" layout="${threadid}" bsonType="Int32" />
        <field name="ThreadName" layout="${threadname}" />
        <field name="Level" layout="${level}"/>
        <field name="Message" layout="${message}" />
        <field name="Exception" layout="${exception:format=tostring}" />
      </target>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="mongoTest"/>
  </rules>
</nlog>

And I use:

Microsoft.Extensions.Configuration;
Microsoft.Extensions.DependencyInjection;
Microsoft.Extensions.Logging;
NLog;
NLog.Extensions.Logging;
System.Threading;

Latest mongoDB Driver 2.0

nLog does not work when I upgrade my project to the latest mongoDB Driver:

---> System.TypeInitializationException: The type initializer for 'NLog.Mongo.MongoTarget' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'MongoDB.Driver, Version=1.9.2.235, Culture=neutral, PublicKeyToken=f686731cfb9cc103' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at NLog.Mongo.MongoTarget..cctor()
--- End of inner exception stack trace ---
at NLog.Mongo.MongoTarget..ctor()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at NLog.Internal.FactoryHelper.CreateInstance(Type t)

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

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!

Target does not update on LogManager.ReconfigExistingLoggers();

We store our connection strings and credentials outside of our app in a secrets vault, but we like to keep the rest of our config in config files. We marry the two just before starting up the app. The following code works great with other targets such as the DatabaseTarget, but doesn't work with MongoTarget.

    static void Main(string[] args)
    {
        Logger logger = NLog.LogManager.GetCurrentClassLogger();

        // do startup activities like building the host and log to files
        try
        {
            // build host
        }
        catch (Exception ex)
        {
            // logger.LogException()
            // the logger never writes anything to mongo and never throws an error
        }

        // set connection string after retrieving username/password from secrets store
        var mongo = LogManager.Configuration.FindTargetByName("mongoCustom") as MongoTarget;
        mongo.ConnectionString = "mongodb://localhost/Logging";
        LogManager.ReconfigExistingLoggers();

        // run the host
        try
        {
            // host.Run()
        }
        catch (Exception ex)
        {
            // logger.LogException()
        }
    }

<target xsi:type="Mongo" name="mongoCustom" includeDefaults="false" collectionName="CustomLog" cappedCollectionSize="26214400">

connect to non-local db

i am not sure if i am making any mistakes or is that a connection string of this format:

mongodb://<dbuser>:<dbpassword>@ds215388.mlab.com:15388/speckle which is a typical MongoLab connection string will not work with this project?

I guess i am wondering if remote databases are supported? All of the examples I have seen are for localhost

Date stored as string

I have the following NLog.config and I am seeing Date field stored as string instead of Date inside MongoDB. Why do you think this is happening?

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <variable name="logDirectory" value="${basedir}/Logs" />
  <variable name="VerboseLayout" value="${longdate} ${level:upperCase=true} ${message} (${callsite:includSourcePath=true})" />

  <extensions>
    <add assembly="NLog.Mongo"/>
  </extensions>

  <targets>
    <target xsi:type="Mongo"
            name="mongoCustom"
            includeDefaults="false"
            connectionString="mongodb://localhost:27017/NteLogging"
            collectionName="workerConsoleLogs">

      <field name="Date" layout="${longdate:universalTime=true}" />
      <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}" />
      <field name="ThreadName" layout="${threadname}" />
      <field name="ProcessID" layout="${processid}" />
      <field name="ProcessName" layout="${processname:fullName=true}" />
      <field name="UserName" layout="${windows-identity}" />
    </target>

    <target xsi:type="ColoredConsole" name="console" layout="${VerboseLayout}"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="mongoCustom" />
    <!--<logger name="*" minlevel="Trace" writeTo="console" />-->
  </rules>
</nlog>

appsettings.json with Mongo

Hello,
I have tried successfully using NLog.Mongo with the XML configuration file.
Now I try to use the appsettings.json file but I can't find any real example, especially to add additional fields.

How can I convert this part from the XML file...

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

... to appsettings.json format?

Thanks for your help

Missing a Basic NLog / NLog.Mongo Introduction

Just want to say thank you for the NLog.Mongo target. Works as expected now.

I forgot to add:

<extensions> <add assembly="NLog.Mongo"/> </extensions>

To NLog.config in the first place.

  1. So maybe it would be worth to have a short step-by-step tutorial. It was hard for me to find the error since I am also new to NLog.
  2. Is a way to get rid of the schema warning for xsi-type”Mongo”?

fail to connet to mongo db

myconfig:

<target xsi:type="Mongo" name="mongo" databaseName="GrapefruitVuCore" collectionName="Logs" connectionString="mongodb://username:pwd@ip/GrapefruitVuCore" cappedCollectionSize="26214400"> <property name="LongDate" layout="${longdate}" bsonType="DateTime" /> <property name="Level" layout="${level}" /> <property name="Logger" layout="${logger}"/> <property name="Message" layout="${message}" /> <property name="Exception" layout="${exception:format=tostring}" /> <property name="Url" layout="${aspnet-request-url}" /> <property name="Action" layout="${aspnet-mvc-action}" /> <property name="UserName" layout="${windows-identity}" /> </target>

error message

Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.