Event Log Writer (Miscellaneous)

The Event Log Writer .NET component provides you with a simple and robust method to write to the event log. One of the major highlight of this component is writing to the event log at defined intervals on a background thread. This ensures that the performance of your applications remains at peak! This component also offers high performance writes using cached writers – creates the writers for you ensuring that the correct writer is always used without the overhead of creating a new one all the time.

This component is built using the .NET 2.0 Framework which means in can be used in all .NET applications that are using the 2.0 framework or newer. This includes .NET 2.0, 3.0, 3.5, 4.0, & 4.5. This component can be used all types of .NET applications including:

  • Console
  • Windows Forms
  • WPF
  • ASP.NET
  • ASP.NET MVC
  • Windows Services
  • Web Services (XML/WCF)

The component also ensures that many pitfalls are not encountered including:

  • Deadlocks – thread safety through locking avoids deadlocks
  • Memory leaks – clean up through the dispose method & consider problems such as the lapsed listener
  • Exceptions leaks – Exception handling through to ensure undesired termination of the application does not occur
  • ASP.NET ThreadPool & Service Unavailable – Give control of whether ThreadPool threads are used or not (by default no). This ensures that threads are not taken away from high availability ASP.NET applications causing the dreaded “Service Unavailable”
  • Task/Background work structure – Flexible mechanism for creating complex background operations
  • Strong name signed – Allows referencing in other application that are strong named signed & also installed into the Global Assembly Cache (GAC)

The example shows how to use the event writers.

Write to event log

Console.WriteLine("Writing two entries to the event log...");
var writerSetting = new EventLogWriterSetting(LogName, EventSource);
using (var writer = new EventLogWriter(writerSetting))
{
    for (int i = 0; i < 2; i++)
    {
        var message = string.Format("EventWriter message on {0}", DateTime.Now);
        Console.WriteLine(writer.WriteToLog(message, EventLogEntryType.Information)
                ? String.Format("Successfully wrote to the event log. Message: {0}", message)
                : "Unsuccessful attempt to write to the event log." 
            );
    }
}

Write to event log using cached writers

EventLogWriterCache.WriteToLog(DateTime.Now.ToString(), LogName, EventSource, EventLogEntryType.Information);

Write to the event log using the queue mechanism

Console.WriteLine("Configuring QueueWriter to write every 10 seconds starting after 10 seconds with a write delay between messages of 10 milliseconds.");
var writerQueueSetting = new EventLogWriterQueueSetting
    (TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(10), TimeSpan.FromSeconds(10));

EventLogWriterQueue.Setting = writerQueueSetting;
var writerQueue = new EventLogWriterQueue(LogError);
var message = new EventLogWriterQueueMessage
    (
        String.Format("Queue message: {0}", DateTime.Now.ToString(CultureInfo.InvariantCulture)),
        LogName,
        EventSource,
        EventLogEntryType.Information
    );
writerQueue.Log(message);

Console.WriteLine("Polling every four seconds for new entries in the event log...");
for (int i = 0; i < 4; i++)
{
    TestEventLogReader();
    Thread.Sleep(4000);
}

// Only dispose after your queue is empty
while (!EventLogWriterQueue.IsQueueEmpty)
{
    Thread.Sleep(1000);
}

writerQueue.Dispose();

You get a fully documented solution which includes the core file & test project. Also you get a comprehensive help file documenting full usage and you get compiled assemblies for immediate usage within your application.

Download Event Log Writer (Miscellaneous)

Leave a Reply

Your email address will not be published. Required fields are marked *