NLog settings not deleting MaxArchiveDays or MaxArchiveFiles? - c#

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

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

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

I'm trying to configure NLog for my C# application. I've got the logging working but it seems to have a mind of it's own in regards to whitespace. Here is 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="c:\temp\nlog-internal.log" >
<variable name="brief"
value="${longdate} | ${level} | ${logger} |: ${message}" />
<variable name="verbose"
value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} |: ${message}" />
<targets>
<target name="myLog" xsi:type="File" fileName="myLog.txt" layout="${verbose}" />
<target name="console" xsi:type="Console" layout="${brief}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="myLog" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
The log's output currently looks like this:
2016-05-19 12:08:00.0302|DEBUG|Namespace.ClassName|:Some log message here...
For some reason, NLog is getting rid of the spaces I defined in the target's layout attribute. What I would like the log entry to look like is:
2016-05-19 12:08:00.0302 | DEBUG | Namespace.ClassName |: Some log message here...
How do I get NLog to preserve the spaces in the layout attribute?
I've also tried to set the level to a fixed length with padding like this: ${padding:padding=5,fixedlength=true:${level:uppercase=true}} as mentioned in this post, but still no luck.
I discovered that NLog doesn't play nice when there is a # character in the path for the solution. Renamed the path and it started working again.

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