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