Dienstag, Februar 15, 2005

Endlich, lang erwartet (nun auch ein Rolling File Sink für EntLib)

Hier der orginal Post von Baz:

Enterprise Library Logging - Rolling File Sink

Overview
I spend my days working at Avanade and I've been using the beta of Enterprise Library for the past few months on various customer projects.  After using the FlatFileSink recently, I noticed a few problems with it.  The most glaring deficiency is the lack of rolling logs.  Log files will keep growing until an adminstrator manually fixes the problem.  On active systems, this could result in huge log files that can't even be opened using standard viewers.  The other problem was more subtle and turned out to be a bug with the FlatFileSink implementation.

This article describes the RollingFileSink extension of the Logging block.  This is a custom sink that can be plugged into the Enterprise Library Logging block like the standard sinks that are included.  The FlatFileSink shipped with EntLib 1.0 may not be adequate for enterprise systems since log files by default will continue to grow unchecked.  This new sink provides support for rolling over log files based on time or file size.

Even if you don’t need log rollover capabilities, the new sink fixes two bugs in the FlatFileSink.  Additional details about the specific fixes can be found at the end of this article.

Download the latest source code for the new RollingFileSink here.


Configuration
The sink creates a new log file when the current file exceeds defined thresholds.  The RollingFileSink provides file age and file size thresholds that can be configured using the Enterprise Library configuration tool like any other sink.

The RollingFileSink has the following properties• Header - optional•BR>• Footer - optional
• MaximumLogFilesBeforePurge - option•l
• Timestamp –ormat – yyyy-MM-dd [hh:mm t] - opti•nal
• Age Threshold - in•eger
• –Age Unit – Minutes, Hours, Days, Weeks,•Months
• Byt– Threshold • integer
•&n–sp;Byte Unit – KB, MB, GB


Design
The RollingFileSink uses a database-style log rollover process.  All new log entries are written to the file defined in the BaseFilename property.  Before a log is written, the sink checks to see if the file has grown too big or the time limit has expired.  If the current log file exceeds the size or age threshold, then the log file is renamed with the current timestamp or an incremental counter.  The new file is created with the same name defined in BaseFilename.

File Age Threshold
The age threshold allows you to limit the growth of your log files according to age.  The threshold is compared by evaluating the current date/time against the log file creation date/time.  For example, if the log file was created on 1/1/2000 at 3PM and the age threshold was set to 2 days, a new log file will be created on 1/3/2000 at 3PM.

There are two properties that control the age thre–hold behavior – AgeThreshold and AgeUnit.  Select the appropriate –nit size first – Minutes, Hours, Days, Weeks or Months - by setting the AgeUnit property.  Set the AgeThreshold property to zero to disable the age threshold check. 

File Size Threshold
In addition to age threshold, you can limit log file growth based on file size. 
There are two properties that control the age th–eshold behavior – ByteThreshold and ByteUnit.  Select the appropriate unit size first - Kilobytes, Megabytes or Gigabytes - by setting the ByteUnit property.  Set the ByteThreshold property to zero to disable the file size threshold check. 

Combo
Both the age and size thresholds can be applied at the same time.  For example, logs can be configured to rollover every week or earlier if the file grows over 10MB.  To accomplish this, set the AgeUnit to Weeks, AgeThreshold to 1, ByteUnit to Megabytes and ByteThreshold to 10.

Filename Formatting
When log file exceeds the defined thresholds, it is given a new unique name. 
The TimestampFormat property can be used to define a date-time format string used for generating new file names.  This allows overgrown log files to be renamed and stamped with the current timestamp.

The timestamp is appended to the end of the new filename, before the extension.  If the new filename is already exists, then an incremental counter is added until a unique combination is found.  If the TimestampFormat property is left blank, the counter will always be appended to the end of the BaseFilename.  The counter is formatted to 7 digits (mybase-0000123.log) and counting begins from 1.
 
Usage
In order to use the new sink, you must compile and copy the binaries into the same directory as the configuration tool.  There are two files that need to be copied - Avanade.Baz.Logging.Sinks.dll and Avanade.Baz.Logging.Sinks.Configuration.Design.dll.  When these files are in the same directory, they will be registered during the startup of the tool.  This will allow you to add a new Rolling File Sink from the right-click context menu on the Sinks collection node.


FlatFileSink Bug Fixes
There is a serious bug in the FlatFileSink that causes data loss in multithreaded applications.  The lock in the main WriteToFile method needs to be called earli–r in the process – before the file is opened.  This results in contention errors that get logged in the event log.  The RollingFileSink does not inherit from the FlatFileSink and has corrected this bug.

This new sink also fixes another bug in the FlatFileSink.  If you have tried using the FlatFileSink from ASP.NET you will notice that the sink tries to write to c:\windows\system32.  The workaround with the FlatFileSink is to specify a full path instead of just a filename.  The RollingFileSink correctly appends the base directory of the current AppDomain and resolves this issue.

Purging
Log files can be automatically deleted after a certain amount by setting the MaximumLogFilesBeforePurge property.  Set this property to 0 to disable automatic purging.  Purging occurs before the current log file is renamed.

Test Driven

The RollingFileSink includes a full set of unit tests and was built using test-driven development techniques.  Before extending this sink (or any Enterprise Library code for that matter), ensure that all of the unit tests are executing properly.

Unfinished Business
There are a few areas of the RollingFileSink that are incomplete.  The code has the various strings and messages hard-coded and does not use the string resource tool.  Code comments are also missing and FxCop has not been run.  There are also no unit tests for the design-time node.

Please download the source and take a look through.  Post your feedback and other improvements.

Keine Kommentare: