NLog not preserving whitespace - c#

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.

Related

NLog with two running programs

I´ve got a Windows-Service which runs in the same location as its Notify. They communicate with WebSocketSharp.
But only one program has a log file. I think the problem is, the notify consumes the nlog.config at first and for the service the nlog.config is locked. But I may confuse it. Also always the Notify only produces the log, never the service. May be in my nlog.config is an error? But then why does the notify produce one but the Service not, when the service and the notify should use the same?
I've tested and the service does not produce any log, even the notify is not running.
May there be a problem with Windows Service and NLog?
Here 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">
<time type="AccurateUtc" />
<targets>
<target name="debug-logfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/MyService/log/${date:format=yyyy-MM}.log" layout="${longdate} | ${level} | ${message}" />
<target name="trace-logfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/MyService/log/trace/${date:format=yyyy-MM-dd}.log" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fffffff} | ${level} | ${message}" />
<target name="errorfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/MyService/log/Error.log" layout="${longdate} | ${level} | ${message}" />
<target name="console" xsi:type="Console" layout="${longdate} | ${level} | ${callsite} | ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="trace-logfile" />
<logger name="*" minlevel="Debug" writeTo="debug-logfile" />
<logger name="*" minlevel="Error" writeTo="errorfile" />
</rules>
</nlog>
Here is how I use NLog:
protected static NLog.Logger Log = LogManager.GetCurrentClassLogger();
Log.Trace("Message");

CKFinder NLog Isn't Logging

I am using CKFinder in ASP.NET. It has been working but has recently stopped (uploads specifically). While trying to track down the issues, I noticed no logs are being written.
In my web.config, I have enabled verbose logging for ckfinder:
enableVerboseLogging="true"
Here is my NLog.config file (which I am not familiar with; we use log4net). Other traces are being written via NLog, however, so I think this is correct.
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target xsi:type="File" name="logFile" fileName="${basedir}/App_Data/logs/ckfinder.log"
archiveFileName="${basedir}/App_Data/logs/archives/ckfinder.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="5" concurrentWrites="true" keepFileOpen="false"
layout="${level} | ${logger} | ${longdate} | ${message}${onexception: | ${exception:format=ToString,StackTrace:maxInnerExceptionLevel=10}}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>
I was forgetting this line from Startup.Configuration:
LoggerManager.LoggerAdapterFactory = new NLogLoggerAdapterFactory();

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

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.

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