NLog not sending to papertrail once deployed to an EC2 - c#

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

Related

NLog date won't run in Linux Mono. (System.NotSupportedException)

I'm new on using NLog and C#. The configuration works correctly under Windows system. However, when I tried running it under VPS server. It shows this error message saying that the Date layout isn't supported in my system. I've searched around Google to see if there's anything that I could do but nothing helps.
Running in: Mono JIT compiler version 6.12.0.122
Output: Unhandled Exception: NLog.NLogConfigurationException: Error when setting property 'Format' on Layout Renderer: ${date} ---> System.NotSupportedException: Cannot invoke method with stack pointers via reflection
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="/tmp/nlog-internal.log">
<variable name="VerboseLayout" value="${longdate:padding=4} ${level:upperCase=true:padding=-6} [${logger:shortName=false} -> ${threadname}] ${message} (${callsite:includSourcePath=false}) ${exception:format=ToString}" />
<variable name="ConsoleLayout" value="${longdate:padding=4} ${level:upperCase=true:padding=-6} [${logger:shortName=true:padding=-12}] ${message}" />
<variable name="TextBoxLayout" value="${date:format=HH\:MM\:ss.ffff} [${level:upperCase=true}] [${logger:shortName=true}] ${message}" />
<variable name="MinLogLevel" value="Info" />
<targets>
<!-- 10 MiB files -->
<target xsi:type="File" encoding="utf-8" name="file" maxArchiveFiles="10" archiveNumbering="DateAndSequence" keepFileOpen="true" openFileCacheTimeout="30" fileName="log.txt"
layout="${VerboseLayout}" archiveAboveSize="10485760" archiveFileName="/logs/{#####}.log"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>

NLog - It doesn't delete old log files

ASP.NET CORE Using NLog
I have some trouble , it doesn't delete old logs file.
"MaxArchiveFiles" and "maxArchiveDays" did not execute the action of deleting logs properly.
What I do wrong?
Thanks!
<?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"
internalLogLevel="info"
internalLogFile="C:\Logs\testProject\nlog-internal.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="ALL"
fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}"
maxArchiveFiles="20"
archiveFileName="C:\Logs\testProject\testProject{#}.log"
archiveDateFormat="yyyy-MM-dd"
archiveAboveSize="104857600"
archiveNumbering="DateAndSequence"
maxArchiveDays="3"
archiveEvery="Day"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="ALL" />
</rules>
</nlog>
Current files (files from today) are not part of archive when using MaxArchiveDays. So maxArchiveDays="1" will only clean files older than yesterday.
You could consider removing archiveFileName and archiveDateFormat and archiveNumbering, so it becomes this:
<target xsi:type="File" name="ALL"
fileName="C:\Logs\testProject\testProject.${cached:${date:format=yyyy-MM-dd}}.log"
layout="${longdate}|${uppercase:${level}}|${threadid}|${logger}|${message} ${exception:format=ToString}"
maxArchiveFiles="100"
archiveAboveSize="10485760"
maxArchiveDays="1"
/>

NLog config inside web.config or app.config ignored in version 4.5.11

Recently, we've updated some web and console application, to use a newer version of NLog, the latest 4.5.11.
Before, we've used a 4.4 version.
With this 4.4 version, we were able to set the NLog config inside the web.config or app.config file.
According to the documentation, this is still possible. However, with the newer version this does not work.
I only get some logging, if I copy/paste the same rules, into an nlog.config file.
Of course, I could do this for all my applications, but there is a chance we miss something.
What is the cause it isn't read from the web/app.config file anymore?
I also tried to set the throwExceptions="true" attribute, but it gave me no exceptions.
Also if I set the internal log file, on the attribute inside my web.config, it's still not working. The internal log, is also empty.
Tested with internalLogLevel="Info" and internalLogLevel="Trace"
If I set it in the Application_Start, I only have
2019-02-27 10:23:33.1899 Info Shutting down logging...
2019-02-27 10:23:33.1899 Info Logger has been shut down.
This is my Web.Config (partly)
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
...
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogFile="c:\temp\internal-nlog.txt" internalLogLevel="Info">
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/Log/${shortdate}.log" layout="${date:format=dd-MM-yyyy HH\:mm\:ss.fff} [${level}] ${callsite} ${message}${onexception:${newline}EXCEPTION OCCURRED\:${exception:format=tostring}}" />
<target name="warnfile" xsi:type="File" fileName="${basedir}/Log/warn/${shortdate}.log" layout="${date:format=dd-MM-yyyy HH\:mm\:ss.fff} [${level}] ${callsite} ${message}${onexception:${newline}EXCEPTION OCCURRED\:${exception:format=tostring}}" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="logfile">
<filters>
<when condition="contains('${message}', 'ThreadAbortException')" action="Ignore" />
</filters>
</logger>
<logger name="*" level="Debug" writeTo="warnfile">
<filters>
<when condition="contains('${message}', 'ThreadAbortException')" action="Ignore" />
</filters>
</logger>
</rules>
</nlog>

nlog settings for ASP .NET Core 2.1 site

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.

Send ALL messages to one log file, and RavenDB logs to a different log file

This sounds trivial, but I somehow cannot manage to do it. I have the below 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" autoReload="true" throwExceptions="true">
<variable name="logDirectory" value="${basedir}/App_Data/logs"/>
<targets>
<target name="file" xsi:type="AsyncWrapper">
<target xsi:type="File" name="f1" fileName="${logDirectory}\log1.txt" layout="${longdate} ${callsite} ${level} ${message} (File 1)"/>
</target>
<target xsi:type="File" name="fileGeneral" fileName="${logDirectory}\log_${shortdate}.txt" >
<layout xsi:type="Log4JXmlEventLayout"/>
</target>
<target xsi:type="File" name="fileRaven" fileName="${logDirectory}\raven_${shortdate}.txt" >
<layout xsi:type="Log4JXmlEventLayout"/>
</target>
</targets>
<rules>
<logger name="Raven.*" minlevel="Trace" writeTo="fileRaven"></logger>
<logger name="*" minlevel="Trace" writeTo="fileGeneral"></logger>
</rules>
</nlog>
This is ending up with Raven + ALL logs to 'log_[date].txt', and another copy of just RavenDB logs in 'raven_[date].txt'. How should this be done?
<logger name="Raven.*" minlevel="Trace" writeTo="fileRaven" final="true"></logger>
Where final="true" means that no more rules for Raven.* will be executed will do what you are asking (if I understood you correctly).
Unfortunately this is not possible.
The reason is that the loggers are evaluated from top to bottom, the first one matching will be used, and does below will not be evaluated.
In you case this means that when logging anything Raven related the first logger will be used and stops the flow.

Categories