loresoft / nlog.mongo Goto Github PK
View Code? Open in Web Editor NEWMongoDB Target for NLog
License: MIT License
MongoDB Target for NLog
License: MIT License
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键对面对应的字段存在重复字段的情况,需要改善
Adding strong name would enable this assembly to be used as dependency for others that have requirement to be strongly signed.
Log properties Event.Id is cause problem, I had to change line 255 to this string key = Convert.ToString(property.Key, CultureInfo.InvariantCulture).Replace('.', '_');
var config = new LoggingConfiguration();
var mongoTarget = new MongoTarget();
config.AddTarget("targetMongo", mongoTarget);
nlog.config
<field name="AppName" layout="${AppName}"/>
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.
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, Func
2 valueFactory)
at NLog.Mongo.MongoTarget.GetCollection()
at NLog.Mongo.MongoTarget.Write(LogEventInfo logEvent)
at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
Hi loresoft team.
This target working fine on my pc, but not work on server. How I can get error message from lib?
https://github.com/NLog/NLog/wiki/Internal-Logging
Internal logging not helping :(
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 Host IP address in the configuration?
Just like
<field name="Host" layout="${HostIP}" />
An BsonString exception is thrown when Source is null from this line:
NLog.Mongo/src/NLog.Mongo/MongoTarget.cs
Line 345 in 19a7f0b
Example:
logger.Error(new Exception("Test"), "Test Exception")
Will result in an exception.
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
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.
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?
“field - Specifies a root level document field. There can be multiple fields specified.”
"${var}" In mongodb, not a root field, but a property of a field。
nlog.config Example:
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;
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)
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:
Is there a way to remove the last properties object from the json in Mongo?
Thanks!
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">
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
Add support for specifying an SSL cert to use when connecting to Mongo.
Perhaps more broadly allow configuration of MongoClientSettings or allow the application to create the MongoClient instance via a callback method.
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>
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
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.
<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>
Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.