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

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>

Related

NLog settings not deleting MaxArchiveDays or MaxArchiveFiles?

I have the following NLog config file below. It's set to Archive every day and initially I had just the MaxArchiveFiles. Now I would like to keep only X amount of days of archived files and found the info that says MaxArchiveDays is available in v4.7 and up. So, I upgraded to v4.7 but now it doesn't seem to archive either on Days or File number.
Anyone see anything wrong with this config file with NLog v4.7?
<?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="D:/Logs/HRImport"/>
<targets async="true">
<target name="DefaultTarget"
xsi:type="File"
fileName="${LogDirectory}/LogFile.log"
encoding="utf-8"
layout="${longdate} | ${callsite} | ${message}"
archiveFileName="${LogDirectory}/Archive/${shortdate}_log.{#}.log"
archiveAboveSize="3145728"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="3"
maxArchiveDays="2"
/>
<!--1 meg: 1048576 -->
<target name="ConsoleTarget"
xsi:type="Console"
layout="${longdate} ${logger:shortName=True} ${message}${onexception:EXCEPTION OCCURRED\:${exception:format=type,message,StackTrace,method:maxInnerExceptionLevel=8:innerFormat=type,message,StackTrace,method}}"
/>
</targets>
<rules>
<logger name="defaultLogger" minlevel="Debug" writeTo="DefaultTarget,ConsoleTarget" />
</rules>
</nlog>
** UPDATE **
<target name="DefaultTarget"
xsi:type="File"
fileName="${LogDirectory}/LogFile.log"
encoding="utf-8"
layout="${longdate} | ${callsite} | ${message}"
archiveFileName="${LogDirectory}/Archive/{#}_log.log"
archiveNumbering="DateAndSequence"
archiveAboveSize="3145728"
archiveEvery="Day"
maxArchiveFiles="3"
maxArchiveDays="2"
/>
The problem is the use of ${shortdate}:
archiveFileName="${LogDirectory}/Archive/${shortdate}_log.{#}.log"
archiveNumbering="Rolling"
When using Layout in archiveFileName then it must be very static. Only {#} should contain the dynamic part.
Instead try this:
archiveFileName="${LogDirectory}/Archive/{#}_log.log"
archiveNumbering="DateAndSequence"
See also: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

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

Logging Persian messages with NLog

In my ASP.NET MVC project I have below config in Web.config as below:
<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">
<targets>
<target name="default" xsi:type="File" fileName="logs/app-log.txt" layout="
--------------------- ${level} (${longdate}) ----------------------$
IP: ${aspnet-request-ip}
Call Site: ${callsite}
${level} message: ${message}"/> archiveFileName="logs/archives/app-log.{#}.txt" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="7" />
</targets>
<rules>
<logger name="*" writeTo="default" />
</rules>
</nlog>
If my log message is in Persian text then in log file I have question mark as ????? instead of the Persian text.
How can I fix this problem? Any help will be appreciated!
I added encoding="UTF8" attribute in target element
for example:
<target name="FileLogger" xsi:type="File" encoding="UTF8" layout="....">
As #ramesh pointed out tru using Encoding="utf-16" and also try writeBom="true".
The nlog documentation of this file target is https://github.com/NLog/NLog/wiki/File-target and it might give you some more help.

NLog not sending to papertrail once deployed to an EC2

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

How to make Nlog archive a file with the date when the logging took place

We are using Nlog as our logging framework and I cannot find a way to archive files the way I want. I would like to have the date of when the logging took place in the logging file name.
Ex All logging that happend from 2009-10-01 00:00 -> 2009-10-01:23:59 should be placed in Log.2009-10-01.log. But all the logs for this day should be placed in Log.log for tailing and such.
The current NLog.config that I use looks like this.
<?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="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/Log.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
archiveEvery="Day"
archiveFileName="${basedir}/Logs/Log${shortdate}-{#}.log"
archiveNumbering="Sequence"
maxArchiveFiles="99999"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
This however sets the date in the logfile to the date when the new logfile is created. Which cause frustration when you want to read logs later.
It also seems like I have to have atleast one # in the archiveFileName, which I rather not. So if you got a solution for that also I would be twice as thankful =)
Probably too late to help you, but I believe all you need to do is include the date in the file name using the date layout renderer with the proper date format. By including the date, you don't need to specify the archive features. When the date changes, a new file will be automatically created.
<?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="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
Just in case if somebody still needs a solution -- requested feature has been added to NLog recently: https://github.com/NLog/NLog/pull/241, but it is still not available by Nuget
Maybe this is what you need,
Daily folder with the file Log.log in it
<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>

Categories