17 June 2010

Installing an Event Source

Writing events to the event log is easy, but you must register an event source with the system.  Because these are simply registry keys, the application must be executing under administrative privileges, to create the keys.  This is a problem frequently encountered by client-server, service, and middleware .NET programmers.

The one time you are guaranteed to have administrative control over the registry is during installation of the application.  Adding a custom action to a Visual Studio Setup project will create your EventSource, during installation, without encountering permissions issues.

The Installui.exe utility that we run from the Visual Studio Command Line, especially when testing services, executes during the setup process.  We can tell it to look for RunInstaler attribute decorated classes, whose input parameter value is true, and call the method.  (Cool!)

Configuration Steps:
  1. Add a class to your application named InstallActions.cs
  2. The file content should resemble this:

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

    Notice the method is decorated with the RunInstaller attribute. This tells the compiler the method is to be accessible to the setup project. Of course, this class may be included in any project in the solution.
  3. Add a Visual Studio Setup project to your solution, if one doesn't already exist.

  4. Right-click the Application Folder node, in the left window pane
    (The context menu appears)
  5. In the context menu, select Add > Project Output...
    (The Add Project Output Group dialog appears)

  6. Select the relevant output required for installation (typically Primary Output and Active configuration)
  7. Click the OK button
  8. Right-click the setup project node in the Solution Explorer
    (The context menu appears)
  9. In the context menu, select View > Custom Actions

  10. Right-click the Install node
    (The context menu appears)
  11. In the context menu, select Add Custom Action...
    (The Select Item in Project dialog appears)
  12. In the dialog window, double-click the Application Folder node, or select it from the drop-down list
  13. In the dialog window, double-click select the Primary output from PROJECT_NAME (Active) node
    (The dialog closes, and a new child node appears under the Install node, entitled, Primary output from WindowsService1 (Active))
You are done!  The setup project is now configured to execute all classes inheriting Installer, and have the RunInstaller(true) attribute.

I generally keep the EventSource name stored in an application setting (app.config) file.  Retrieving this value for use in the source code provided above takes a little more effort; because, the InstallEventLog class is not in a namespace.


  1. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. event registration software


Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.