log4net: Roll by date, enumerate file by number - log4net

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.

Related

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

Is there any logging function which allows you to create n number of log files programatically?

I'm working on a program which needs to write messages to multiple log files, depending on the number of threads it creates. The amount of threads is arbitrary. Is there a logging facility which allows me to create a separate log file for each thread? I'm currently using sl4j and logback, and it seems to require that the file names be configured in a properties file.
Logback's SiftingAppender separates (or sifts) logging according to a given runtime attributes. The general structure goes likes this:
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>clientPort</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>server-${clientPort}.log</file>
....
</appender>
</sift>
</appender>
By default, SiftingAppender assumes MDCBasedDiscriminator which relies heavily on MDC.
It is probably exactly what your are looking for. See also http://www.nurkiewicz.com/2013/04/siftingappender-logging-different.html

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.

How do I create a new file for each log4net logger instance?

I am trying to create a different log file for each thread (the threads are handling some site processing). Essentially just naming the log file with a name that is derived from the site that is being processed.
I know all about the GlobalContext.properties and also the TheadContext.properties and neither really seem to work for what I am trying to do. In pretty much either case any concurrently running threads just use whichever file is the current output file as their output. The only difference I have seen is that if I use the ThreadContext to set the property it makes all the files but only outputs to one, where using global it seems to only make the one file unless the processes start at different times.
What I would REALLY like to do is be able to tell the file to just use the name of the logger object (the name that is used in instantiating the object) in the file name instead of using these properties.
Instead of fighting the configuration, I recommend that you rethink your logging strategy. log4net was not built for a log file per thread/class. Loggers are created at startup or on first write (depends). Use a tailing log file reader, like Kiwi Log Viewer or Splunk, and filter on the thread id or logger name in your messages.
If you put them in your conversion pattern...
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline" />

How do I only keep the most recent n entries in a log4net sql table?

I am using log4net to log to a sql table. I'd like to be able to either only keep the most recent n days, or the most recent n entries in the table. Is this possible with log4net?
Log4net do not have this capability built-in. But such a task is probably best placed as a job ,e.g. in SSIS (if you're running MS SQL Server) or similar tools.
I figured it out, for the commandText for the AdoNetAppender I set the command text to:
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (#log_date, #thread, #log_level, #logger, #message, #exception); DELETE FROM [Log] WHERE [Date] < DATEADD(dd, -28, GETDATE())" />
It feels hacky, but it works. I'll post here if I find a neater solution.
I know I'm late to the party... but looking at ilivewithian's solution I would agree with Peter Lillevold's observation that having it cause additional load in the logging process is undesirable.
Wouldn't it also be possible to use a trigger in the database to auto-delete the older items? Sure, you would need a DB that supports triggers, but it seems like most modern ones (including open source ones like SQLite and PostgreSQL) do.

Resources