I\'m looking for a good TraceListener for .Net that supports rolling over the log file based on size limits.
Constraints
I am using NLog and I am very satisfied. Source code is well written and it is easy to extend and modify. Documentation is good and it is very easy to configure.
Some Links:
You could use Microsoft.VisualBasic.Logging.FileLogTraceListener, which comes built-in with the .NET Framework. Don't let the VisualBasic in the namespace scare you, you'll just have to reference the microsoft.visualbasic.dll assembly and it should work fine with C#.
I'm a big fan of log4net (http://logging.apache.org/log4net/index.html), it's very easy to configure and supports just about any log type you want, but can have custom ones written as well.
It can also do different actions depending on the log level. We log all messages to a text file and Error -> Fatal send emails
Consider Enterprise Library Logging Application Block
Make sure to install only the Logging block, since EntLib installer checks all blocks by default.
As given in one of the comments:
Writes to a rolling text file.
Remarks
A new file is used when the maxFileSize is reached, as well as a daily or weekly basis as specified by logFileCreationSchedule.
Each file has a name in the format "\(-)(-).log", with the local date included for daily and weekly rotation, and a sequence number appended if the file already exists.
FileLogTraceListener is a common suggestion but it throws away events or throws an exception when the file exceeds the given max size.
We created a class that extends it, and overrides the Write/WriteLine methods.
There's a try/catch (InvalidOperationException)
, and if that happens, we call base.Close
and rename the file (FullLogFileName
) as follows (we need the base.Close
or else we'll get a 'file in use' error):
In a loop, we add a number to the end, and see if that file exists; if not, use File.Move(FullLogFileName, newFileWithNumber)
, otherwise we keep incrementing the number until we find a file name that works. There's also a lock to ensure that the given instance is thread safe.