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.
Related
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
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
Is there any event handler or similar that I can hook into, to run some code every time NLog starts a new Log File?
I have an NLog Config like this:
<target name="logfile" xsi:type="File"
fileName="[stuff]"
layout="[more Stuff]"
maxArchiveFiles="4"
archiveAboveSize="102400" />
And I'd kinda like to add some meta-data at the start of every log file (version number, Machine Name, etc. so that every log file comes complete with some contextual information)
I don't expect to be able to configure this text in NLog itself, but I'm hoping that I can tell NLog to run a certain method everytime it's going to create a new Log File.
Is this a thing?
Sounds like you are really looking for the Header-Layout on the FileTarget.
<target name="logfile" xsi:type="File">
<header>--- ${assembly-version} started on ${longdate} ---</header>
</target>
See also https://github.com/NLog/NLog/wiki/File-target
Some combine it with archiveOldFileOnStartup="true" because the Header is only printed when file is created (ensure application restarts will create new file).
You can use FileSystemWatcher.
FileSystemWatcher is watching specific (which you want it) directory, If new file created, handle this event with FileSystemWatcher
For more details : https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8
Another partial solution to the "What version?" aspect of this problem could come from:
https://github.com/NLog/NLog/wiki/AssemblyVersion-Layout-Renderer
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
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.