NLog file archive behavior not as expected - nlog

NLog archive behavior is not working as expected. NLog version used: 4.7.9
<target name="traceLog" xsi:type="file" fileName="log-${shortdate}.1.log"
archiveFileName="log-${shortdate}.{#}.log"
archiveAboveSize="1280" // 10kb
archiveNumbering="Sequence"
concurrentWrites="false"
keepFileOpen="true"
openFileCacheTimeout="30"
maxArchiveFiles="5">
Output with inconsistent size though achieve expected after every 10kb size
Also,
want reuse the old achieve file but it delete those old archives(like 0, 1) instead and
also wanted to number archive starts with 1 instead of 0.
How to achieve these requirements with NLog?

Maybe just do this:
<target name="traceLog" xsi:type="file" fileName="log-${shortdate}.log"
archiveAboveSize="1280"
maxArchiveFiles="5"
keepFileOpen="true"
concurrentWrites="false"
openFileCacheTimeout="30">
See also: https://github.com/NLog/NLog/wiki/File-target#archive-old-log-files
Notice if you mix "Dynamic FileName Archive Logic" (log-${shortdate}.1.log) with "Static FileName Archive Logic" (archiveFileName="..."), then it will not work well. See also: https://github.com/NLog/NLog/wiki/File-target#dynamic-vs-static-archive-logic

Related

Use sequence number in active log file

I have the following log target defined.
<target xsi:type="File"
name="file"
layout="${defLayout}"
fileName="${logNamePrefix}.txt"
archiveFileName="${logNamePrefix}.{######}.txt"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
keepFileOpen="true"
openFileCacheTimeout="30"
concurrentWrites="false"
archiveOldFileOnStartup="true"/>
This will give me files like 2021-07-09_13.08.30-Log.txt that are then renamed to 2021-07-09_13.08.30-Log.000000.txt once the rollover occurs. The active file is always the one without the sequence number.
I was wondering if there was some way to push the sequence number into the active filename so the active file would always be numbered. The logger would write to 2021-07-09_13.08.30-Log.000000.txt and then 2021-07-09_13.08.30-Log.000001.txt and so on.

Is it possible to archive logs based on size and on time using NLog

I am using NLog.
I would like to have a size-based file archival and a time-based file archival.
Meaning, every time the log file exceeds 10 MB a new log file is created. Also, every day a new log file is created.
It is clear how to do each of the above separately (https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples) but not how to use them in tandem.
Without any details of the expected fileName-Layout, then this will work just fine in NLog 4.5 (and newer):
<target type="file" name="logfile" fileName="App-${shortdate}.log" archiveAboveSize="1000000" maxArchiveFiles="30" />
It will produce the following filenames (newest first)
App-20200216.log
App-20200216.2.log
App-20200216.1.log
App-20200215.log
App-20200214.log
App-20200214.1.log
See also: https://github.com/NLog/NLog/wiki/File-target#archive-old-log-files

How to use NLog to write to different file according to parameter

I want to be able to write to logs to a different log file according to one of my log parameters.
Lets say, bankId. Each day and bank will write to their own file like
2019-11-11-bank11.log
2019-11-12-bank11.log
and so on.
How can i achieve that programmatic with some kind of a pattern for the log file.
I don't want to create a new version of my app every time I have a new bankid.
You could set the context in one of the context classes and render it with a layout renderer.
e.g. use ${mdlc} and MappedDiagnosticsLogicalContext:
Config:
<target name="file" xsi:type="File"
fileName="${basedir}/${mdlc:bankid}.log" .. />
Code:
using (NLog.MappedDiagnosticsLogicalContext.SetScoped("bankid", "bank11"))
{
logger.Info("myLogEvent");
}
See also all context layout renderers
Sounds like you have an application-wide setting. For that you should either use:
NLog Config Variables - ${var:bankid}
Global Diagnostic Context - ${gdc:bankid}
I prefer to use the GDC has it has a minimum number of surprises, when modifying at runtime.
You can do this in NLog.config:
<target name="file" xsi:type="File"
fileName="${basedir}/${shortdate}-bank${gdc:bankid:whenEmpty=0}.log" .. />
Then at application startup then you can do this:
NLog.GlobalDiagnosticsContext.Set("bankid","11");
See also: https://github.com/nlog/nlog/wiki/Gdc-Layout-Renderer
You can also extract a setting from config-file:
app.config - https://github.com/NLog/NLog/wiki/AppSetting-Layout-Renderer
appsettings.json - https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

Xamarin.iOS versioning during build

I've been trying to get an automatic versioning system going for builds (mainly due to external crash analytics picking up each build as the same until I change the version manually). The format is simple, I take the CFBundleShortVersionString from the Info.plist, and append the current date and time (in yyyyMMddmmss format) as subversion.
The task I've put together for this:
<Project>
<Target Name="BeforeBuild">
<XmlPeek XmlInputPath="$(ProjectDir)Info.plist" Query="//dict/key[. = 'CFBundleShortVersionString']/following-sibling::string[1]">
<Output TaskParameter="Result" ItemName="VersionNumber" />
</XmlPeek>
<PropertyGroup>
<BuildNumber>$([System.DateTime]::Now.ToString(yyyyMMddmmss))</BuildNumber>
</PropertyGroup>
<XmlPoke XmlInputPath="$(ProjectDir)Info.plist" Query="//dict/key[. = 'CFBundleVersion']/following-sibling::string[1]" Value="$(VersionNumber).$(BuildNumber)" />
</Target>
</Project>
However it fails with the following error:
Target BeforeBuild:
[...]/[...].csproj(1069,5): error MSB3733: Input file "[...]/Info.plist" cannot be opened. For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.
Done building target "BeforeBuild" in project "[...].csproj" -- FAILED.
What am I doing wrong? There's not much info about this error, at least not much that I could find and would help fixing it.

NLog - combining day and size archiving

Using NLog, I want to achieve the following:
One log file per day.
Archive on size, unlimited archives.
Keep max x days of logs, automatically deleting older logs.
How to configure this?
One log file per day.
Archive on size, unlimited archives.
This is possible with NLog
Config:
<target name="file" xsi:type="File"
layout="${longdate} ${message} ${exception}"
fileName="${basedir}/logs/log-${shortdate}.txt"
archiveFileName="${basedir}/archives/log.{#####}.txt"
archiveAboveSize="10240"
archiveNumbering="Sequence" />
Keep max x days of logs, automatically deleting older logs
This isn't. You could limit the maximal archives, but not the log files.

Resources