RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Fast and Flexible Logging with Vista's Common Log File System : Page 4

Microsoft's Common Log File System is an OS-supported logging framework that provides all the logging capabilities you need—and it's available via managed code and your favorite .NET language.

Writing to a CLFS Log
CLFS does not define a format for log entries. You're free to define a format that fits the needs of your application. Fortunately, the .NET serialization API does most of the work for you. Start by defining a serializable class that encapsulates the fields in your log entries. As an example, consider this serializable LogEntry class with four properties (a log entry class for your application might have a completely different set of fields):

   public class LogEntry
       private string _subsystem;
       private int _severity;
       private string _text;
       private DateTime _timestamp;
       public string Subsystem
           get { return _subsystem; }
           set { _subsystem = value; }
       public int Severity
           get { return _severity; }
           set { _severity = value; }
       public string Text
           get { return _text; }
           set { _text = value; }
       public DateTime Timestamp
           get { return _timestamp; }
           set { _timestamp = value; }
This example demonstrates creating a log entry that consists of two strings, an integer, and a timestamp, but your log entry class might be considerably more complex. The point to remember is that as long as your class is .NET serializable, it's compatible with the CLFS.

CLFS does not define a format for log entries. You're free to define a format that fits the needs of your application.
You serialize an instance of this class to a stream of bytes using a BinaryFormatter and then write those bytes to the log using a LogRecordSequence. The LogEntry.Serializable attribute and the BinaryFormatter class handle all the serialization details:

   LogEntry entry = new LogEntry();
   entry.Subsystem = "Transactions";
   entry.Severity = 2;
   entry.Text = "An exception was thrown ...";
   entry.Timestamp = DateTime.Now;
   using (MemoryStream stream = new MemoryStream())
      // Serialize the entry
      BinaryFormatter formatter = new BinaryFormatter();
      formatter.Serialize(stream, entry);
      // Write it to the log
      ArraySegment<byte> bytes = new 
      sequence.Append(bytes, SequenceNumber.Invalid, 
        SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
As shown in the preceding code, the LogRecordSequence.Append method takes four parameters. The first parameter is the serialized log entry. The second and third parameters are used to create relationships between log records (covered later in this article). The fourth parameter controls buffering—RecordAppendOptions.ForceFlush causes CLFS to write the log record to the file immediately. Another option, RecordAppendOptions.None writes the log record to a buffer. Call LogRecordSequence.Flush to write all buffered log records to file. The CLFS guarantees flushed records even in the event of a system failure, so if you're writing multiple log entries, they aren't protected from system failures until you flush the buffer.

Reading from a CLFS Log
You use much the same approach to read log entries that you used to write them:

   List<LogEntry> entries = new List<LogEntry>();
   BinaryFormatter formatter = new BinaryFormatter();
   IEnumerable<LogRecord> records = 
   foreach (LogRecord record in records)
      LogEntry entry = (LogEntry) formatter.Deserialize(record.Data);
Passing the LogRecordSequence.BaseSequenceNumber and LogRecordEnumeratorType.Next parameters to LogRecordSequence.ReadLogRecords() returns log entries in the order in which they're stored in the extent files.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date