Installing an Event Log Source in .NET

Event logs have been a feature of Windows NT since its original release. Applications and operating system components can make use of this centralized log service to report events that have taken place, such as a failure to start a component or complete an action.

The .NET API for writing events to a log are simple and easy to use. There’s just one little issue. In order to write to the event log, your program needs to register an event log source. Registering an event log source requires administrator privileges. This can be a problem in Standard User Accounts in Windows XP and Vista since these accounts do not have administrator privileges.

Because of the administrator privilege, the best time to register the event source is when the program is installed. Again, .NET provides the framework to cooperate with the Windows Installer Service making it a relatively easy and bullet proof task. To begin, you’ll need add a class to your program decorated with the correct attribute. Here’s an example.

using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
public class InstallEventLog : Installer
    public const string EventSource = "Desk Drive";
    public InstallEventLog()
        var eventLogInstaller = new EventLogInstaller();
        eventLogInstaller.Source = EventSource;

This class can reside in a separate assembly (.dll) or be embedded in your main program.

The [RunInstaller(true)] attributes specifies whether the Visual Studio Custom Action Installer or the Installer Tool (Installutil.exe) should be invoked when the assembly is installed.

The EventLogInstaller class installs and configures an event log that your application reads from or writes to when running. When the Installer Tool (Installutil.exe) is called, it looks at the RunInstallerAttribute. If it is true, the tool installs all the items in the Installers collection that are associated with your project installer.

Next, you’ll need to add a custom action to the installer itself. If you’re using a Visual Studio setup project, right-click on the setup project, select View and the Custom Actions. On the Custom Actions page, right-click the “Install” node and then “Add Custom Action”


Select a folder and an item. Since I added my custom installer action to my main executable, I selected “Primary output”. Double click and its added.


That’s all there is to it. When you run your installer, the event source is registered. If the event source is already registered then nothing happens. I would recommend not uninstalling the event source once it is installed.

Writing events to the log is even easier. I usually write a couple of static methods similar to the ones below.

public static void LogError(string message)
    Log(message, EventLogEntryType.Error);
public static void LogInformation(string message)
    Log(message, EventLogEntryType.Information);
static void Log(string message, EventLogEntryType logEntryType)
        EventLog.WriteEntry(InstallEventLog.EventSource, message, logEntryType);
        // in case the event source is not registered

I usually add these to the Program.cs class that gets generated by the Visual Studio C# project wizard.

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
Tweetz Desktop 1.0
Tweetz 0.11 Released
More... (1073)

Donate with PayPal