I want to configure Nlog.config to log into InfluxDB the same as logging to Seq. I couldn't find any InfluxDB extension for NLog. Do I need just to implement my own NLog.Targets.Targetor there is already something that works with InfluxDB?
Have you tried NLog.Targets.InfluxDB ?
<nlog>
<extensions>
<add assembly="NLog.Targets.InfluxDB"/>
</extensions>
<targets>
<target type="InfluxDB"
name="influx"
Host="https://yourinfluxhost.com"
Bucket="MyBucket"
Token="inFluXApi=K3y"
Org="Organisation">
<field name="data1" value="${event-properties:data1:format=#}" />
<tag name="data2" value="${event-properties:data2:format=#}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="influx" />
</rules>
</nlog>
Ex.
log.Info("This field {#data1} and tag {#data2} are sent to InfluxDB", data1, data2);
See also: https://github.com/tomcheney/nlog-influx-target
Related
I have a problem I have write a Nlog.target to log in database but I want nlog should log depending on a condition. I want to put a condition if dbvalue is true then made log entries in db.
I wrote the below code which is not working the dbvalue is false but log are still generated in db.
i have added this line in my startup.cs file
GlobalDiagnosticsContext.Set("dbvalue", "false");
here is my nlog.config
<rules>
<logger name="Microsoft.*" maxlevel="Info" final="true" writeTo="" />
<logger name="*" minlevel="Info" writeTo="database" >
<filters>
<when condition="equals('${gdc:dbvalue}', 'true')" action="Log" />
</filters>
</logger>
</rules>
can someone help?
I would probably do it like this:
<rules>
<logger name="Microsoft.*" maxlevel="Info" final="true" writeTo="" />
<logger name="*" minlevel="${gdc:dbLogMinLevel:whenEmpty=Off}" writeTo="database" />
</rules>
And then enable the database-logger like this:
NLog.GlobalDiagnosticsContext.Set("dbLogMinLevel","Info");
NLog.LogManager.ReconfigExistingLoggers(); // Explicit refresh active Logger-objects
See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
See also: https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration
I am using NLog to write logs to file, which is working fine. Now I want to write logs to Elasticsearch, so I added NLog.Targets.ElasticSearch in nugets package and I configured my Nlog.config file. Unfortunately, I can't see any log information while calling http://localhost:9200/_search
In NLog.config, I added extension and targets for Elasticsearch:
<extensions>
<add assembly="NLog.Extended" />
<add assembly="NLog.Targets.ElasticSearch"/>
</extensions>
<targets>
<target xsi:type="ElasticSearch"
name="elastic"
layout="${logger} | ${threadid} | ${message}"
includeAllProperties="true"
uri="http://localhost:9200"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
<logger name="*" minlevel="Trace" writeTo="elastic" />
</rules>
I expect Trace type NLog should be written in Elasticsearch. Am I missing something in the config file?
By the way, I checked this doc for parameters: https://github.com/ReactiveMarkets/NLog.Targets.ElasticSearch/wiki
I just added BufferingWrapper type and it worked. Some references to read:
https://github.com/markmcdowell/NLog.Targets.ElasticSearch/blob/master/src/NLog.Targets.ElasticSearch.Tests/App.config
https://github.com/nlog/NLog/wiki/BufferingWrapper-target
Additionally, please check ElasticSearch.Net latest version in NuGet package.
You can find NLog configuration for Elasticsearch below, which is working fine for me:
<extensions>
<add assembly="NLog.Extended" />
<add assembly="NLog.Targets.ElasticSearch"/>
</extensions>
<target xsi:type="BufferingWrapper" name="ElasticSearch"
flushTimeout="5000">
<target xsi:type="ElasticSearch"
uri="http://localhost:9200"
includeAllProperties ="true">
</target>
</target>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
<logger name="*" minlevel="Trace" writeTo="ElasticSearch" />
</rules>
When deploying to my EC2 NLog no longer seems to be working correctly. When deployed locally, I have no issues logging the chat. On the other hand, it's rapidly writing to it's internal log and ignoring the value of if it should write to it's internal log at all.
I've tried switching the protocol type to be used from TCP to UDP, which stops it from writing errors (but also does not allow it to connect to PaperTrail which was the point). I've also tried turning off internal logging, which also does not work. Recopying over my NLog.config, as well as going through the initial tutorial multiple times. I've also checked my firewall settings for ports, the EC2 security group settings for ports, and the application permissions on the firewall.
NLog version: "4.6.2"
Platform:.Net 4.5
Current NLog config (xml or C#, if relevant)
<?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="false" internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets>
<target name="syslog" type="Syslog">
<messageCreation>
<facility>Local7</facility>
<rfc5424 hostname="${machinename}-Ragnarok" appName="ChatServer-Ragnarok" />
</messageCreation>
<messageSend>
<protocol>TCP</protocol>
<tcp>
<server>logs.papertrailapp.com</server>
<port>REDACTED</port>
<tls>
<enabled>true</enabled>
</tls>
</tcp>
</messageSend>
</target>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="syslog" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
The actual error is as follows (It's repeated every few seconds in the logs):
2019-04-15 13:35:22.3986 Warn SendAsync failed Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at NLog.Targets.Syslog.MessageSend.SocketInitializationForWindows.KeepAliveConfigurationIsUpToDate(KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.SocketInitializationForWindows.ApplyKeepAliveValues(Socket socket, KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.SocketInitialization.SetKeepAlive(Socket socket, KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.Tcp.Init()
at NLog.Targets.Syslog.MessageSend.MessageTransmitter.b__21_0(Task _)
at NLog.Targets.Syslog.Extensions.TaskExtensions.<>c__DisplayClass0_01.<Then>b__0(Task t)
at System.Threading.Tasks.ContinuationResultTaskFromTask1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Does anyone have any ideas on how to get this to work / why it's not working when deployed?
Just a stopgap for now just to add the following to the config, which allows it to not crash on the "Keep Alive"
<?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="false" internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets>
<target name="syslog" type="Syslog">
<messageCreation>
<facility>Local7</facility>
<rfc5424 hostname="${machinename}-Ragnarok" appName="ChatServer-Ragnarok" />
</messageCreation>
<messageSend>
<protocol>TCP</protocol>
<tcp>
<server>logs.papertrailapp.com</server>
<port>REDACTED</port>
<tls>
<enabled>true</enabled>
</tls>
<keepAlive>
<enabled>false</enabled>
<time>15</time>
</keepAlive>
</tcp>
</messageSend>
</target>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="syslog" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
The code I've added in question is the following:
<keepAlive>
<enabled>false</enabled>
<time>15</time>
</keepAlive>
Edit:
A fix was just pushed to NLog.Targets.Syslog's git which fixed this issue for me entirely.
https://github.com/luigiberrettini/NLog.Targets.Syslog/pull/183
I'm trying to setup nlog to log into 3 separate log files. I want Entity Framework stuff to go to one log file (db.log). I want application-related logging only to go to another log file (own.log). And finally I want everything else (Microsoft stuff) to go to yet another file (all.log)
This is what I have below, but I can't get logging to work based on my requirements. What am I doing wrong?
Thanks in advance!
<?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"
autoReload="true"
internalLogFile="internal.log"
internalLogLevel="info" >
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile"
fileName="all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web"
fileName="own-${shortdate}.log"
layout="${longdate}|${activityid}|${level:uppercase=true}|${message} ${exception:format=ToString}|${logger}|${all-event-properties}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="File" name="sqllogfile"
fileName="db-${shortdate}.log"
layout="${date}|${activityid}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.EntityFrameworkCore.*" minlevel="Trace" writeTo="sqllogfile" final="true" />
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- Our log -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Moving database log was the key. Also, after moving catch-all log to the bottom, I now get correct logging results.
I would like to use Layouts in NLog to be able to change the minimum log level using an external variable :
<nlog>
<variable name="loglevel" value="Debug"/>
<targets>
<target ... />
</targets>
<rules>
<logger name="*" minlevel="${loglevel}" writeTo="LogFile" />
</rules>
</nlog>
After starting NLog, all log levels (eg : Tracing, Debug, Info, ...) are set to false which indicate NLog failed to parse minlevel attribute properly.
The NLog layout feature seems to works with target attributes only.
What I want to achieve : in my real app, loglevel is not a constant but rather a custom layout renderer.
I have also tried to replace value="Debug" by value="LogLevel.Debug" without success.
** Updated Answer **
NLog ver. 4.6 added support for using NLog-Config-Variables in minLevel. See https://github.com/NLog/NLog/pull/2709
NLog ver. 4.6.7 added support for adjusting minLevel at runtime, by modifying NLog-Config-Variables and calling ReconfigExistingLoggers(). See https://github.com/NLog/NLog/pull/3184
** Original Answer **
The minlevel, maxlevel and level attributes on <logger> should be fixed strings.
In this case you could use a <filter>, e.g.
<nlog>
<variable name="loglevel" value="Debug"/>
<targets>
<target ... />
</targets>
<rules>
<logger name="*" writeTo="LogFile" >
<filter condition="${level} >= ${loglevel}" action="Log">
</logger>
</rules>
</nlog>
See the docs