NLog - combining day and size archiving - nlog

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.

Related

NLog file archive behavior not as expected

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

Azure create filename from blob name in nLog.config

I am newbie to Azure and just digging out to my first task. we are creating logs file for error logs.
I want to create 4 diff. which has logs of 6 hours from starting of the day. Please find below my nlog.config code:
<target type="AzureBlobStorage"
name="Trace-BlobStorageLogger"
layout=""
connectionString=""
container=""
blobName="nlog-storage-trace-test-${date:format=dd-MM-yyyy}.txt"/>
Right now, it generating one file for whole day, but due to storage capacity once it is full then after logs are not logging.
We want to divide it into 6 hours each into 4 files. We want file to be created something like:
nlog-storage-trace-test-10-06-2020-0000-0600.txt
nlog-storage-trace-test-10-06-2020-0600-1200.txt
and so on.
What change is needed in blobName in target tag in nlog.config file or nay other change that fulfills my requirement.
Thanks
The "new" NLog.Extensions.AzureBlobStorage will reduce the number of writes, so it stay below 50000 file-operations per day:
https://www.nuget.org/packages/NLog.Extensions.AzureBlobStorage/
But if you want the filename to "expire" every 6 hours, then I guess you can use cachedSeconds do this:
<target type="AzureBlobStorage"
name="Trace-BlobStorageLogger"
blobName="nlog-storage-trace-test-${date:format=dd-MM-yyyy_hhmm:cachedSeconds=21600}.txt"/>
Alternative you can write/register your own custom NLog LayoutRenderer:

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

Creating dynamic log files via Log4Net by process ID & date without rename policy

I have been scratching around Stack and the net for a solution to my particular use case but has yet have not found one.
Log4Net 1.2.10
IIS 6.1
.NET application using CastleCore
My requirement is to create a unique file name based on process id per hour with process id, date and hour in the filename. Although if the process Id were to change within that hour then create a new file name based on that process id and date/hour.
I have a service running on a web farm in IIS, this has 4 worker processes running simultaneously, 2 app pools 2 process per pool. So 4 log files being written too simultaneously.
My current config (below) appears to work upto a point. The files get created and are logged too simultaneously, great! The logging is quite heavy in that over the course of an hour it will log around
200MB per process id.
During the course of the hour one of the files out of the two simply stops writing. I have set Minimal lock and different process id's which should prevent any deadlocks, race conditions or collisions.
My current config is below
<appender name="WSG_file_appender" type="WSG.Logger.LogAppender,WSG.Logger">
<file type="log4net.Util.PatternString" value="../../WSG/IWSGServices-[%processid]" />
<datePattern value="-dd.MM.yyyy-HH'.log'" />
<staticLogFileName value="false"/>
<rollingStyle value="Date" />
<appendToFile value="true" />
<maximumFileSize value="500MB" />
<maxSizeRollBackups value="50" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%d] [%t] %-5p [%m]%n" />
</layout>
The format of the log that is generated is:
IWSGServices[8977]-23.03.15.14.log
This is what I want, in the above example that is the log for process ID 8977 for 1400 hours. At 1500 new logs gets generated for each process id in use and the new hour stamp.
When the logging stops in log4net debug mode I get this error many times:
log4net:ERROR [LogAppender] Failed to write [].
System.ArgumentOutOfRangeException: Count cannot be less than zero.
Parameter name: count
at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)
at System.IO.StreamWriter.Write(String value)
at log4net.Util.QuietTextWriter.Write(String value)
I have not found any similar cases of this error within log4net so use cases are limited. I suspect there is some I/O issue or log4net (at least this older version) is not capable of fulfilling my use case. However I am not convinced latest versions will be either. I have not upgraded log4net yet due to reported issues with later versions and Castle Core implementations.
Setting StaticLogFileName=true results in filenames being created without the date stamp i.e WSGServices[8699], and then on the hour gets renamed to IWSGServices[8977]-23.03.15.14.log for example. This is not what I want as for this use case I do not want files to get renamed as I have a log reader which sees this as a new file and parses it again.
I have tried a large number of combinations including with and without locking models, Rolling styles, datepatterns and the config above seems to be the closest I have come. It's possible this config will work if I roll per minute but this will result in over 3000 log files per day which is not practical.
I'd like to achieve this under the current version of log4net I am using, however if I patch to 2.11 there is a setting of PreserveLogFileNameExtension parameter although the file will still get renamed but just not after the extension.
One suggestion I looked at was setting the IIS pool setting Disable Overlap Recycle to false but this didn't make any difference as would only come into play on recycles of the app pool.
Another alternative is too log both processes form each app pool to a single log file although due to the amount of writes I think this solution would not work without further process collisions.
Any help appreciated in case I have missed something or if anyone has used this in current or later log4net versions.

log4net: Roll by date, enumerate file by number

Previously, my team had been using log4net to roll by maximum file size, and it was generating files like:
MyLog.log
MyLog.log.1
MyLog.log.2
etc.
Recently, we've switched to
<rollingStyle value="Date"/>
<MaxSizeRollBackups value="14"/>
<datePattern value="yyyyMMdd"/>
Which now produces filenames like:
MyLog.log
MyLog.log20130324
MyLog.log20130323
etc.
Is it possible to roll by date and yet still have the log files enumerated like before? If so, how would I do this? I looked all over Log4net's website, but couldn't find a good reference on how to do this.
I don't think you can do this without creating a custom appender. For the RollingFileAppender,
if the rollingStyle is set to Date or Composite (the default), then the next output file name is generated from the base file name + the current time formatted by datePattern.

Resources