What the best rollover log file tracelistener for .NET

前端 未结 9 543
太阳男子
太阳男子 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:20

    I had the same issue. Secure environment, no open source allowed. Painful. Here's what worked for us.

    Derive from TextWriterTraceListener that adds members for max log size, max rolls to keep, and uses a FileStream with Share and Access set to ReadWrite and set OpenOrCreate. It should also create and hold a mutex with a name based on the filename.

    Override TraceEvent method, wait for the mutex, seek the stream to end, call Write, check the size and roll if necessary. Release the mutex.

    For rolling, rotate the previous rolls via file move and delete, copy the current file to the first level roll name, call SetLength(0) on the stream. All done within the mutex grab, but hopefully not often. This will work across processes and will avoid that crappy {GUID}mylog.log stuff.

提交回复
热议问题