What the best rollover log file tracelistener for .NET

前端 未结 9 547
太阳男子
太阳男子 2020-12-28 13:43

I\'m looking for a good TraceListener for .Net that supports rolling over the log file based on size limits.

Constraints

  • Uses .Net b
相关标签:
9条回答
  • 2020-12-28 14:06

    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:

    • Using NLog To Track Events
    • Introduction to NLog
    0 讨论(0)
  • 2020-12-28 14:09

    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#.

    0 讨论(0)
  • 2020-12-28 14:15

    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

    0 讨论(0)
  • 2020-12-28 14:15

    Consider Enterprise Library Logging Application Block

    Make sure to install only the Logging block, since EntLib installer checks all blocks by default.

    0 讨论(0)
  • 2020-12-28 14:17

    As given in one of the comments:

    FileLogTraceListener Class

    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.

    0 讨论(0)
  • 2020-12-28 14:18

    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.

    0 讨论(0)
提交回复
热议问题