Modify Overwrite Policy for an EventLog created by an EventLogInstaller

Developing a Windows Service is a common task. Creating an EventLog for this service also is a common practice. The EventLog can be created with a ServiceInstaller (a class which inherits from System.Configuration.Install.Installer).

This is how an Eventlog can be added during the installation with a custom Installer class:

public Installer()
      Installers.Add(new EventLogInstaller
                  Source = "EventLogSource",
                  Log = "EventLogName"

The EventLogInstaller class does not allow to set the overwrite mode.

You can change the overwrite mode after the log has been created, if you use the Committed Event.

public Installer()
   Committed += OnCommitted;

/// <summary>
/// modify the eventlog
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void OnCommitted(object sender, InstallEventArgs e)
   EventLog log = EventLog.GetEventLogs().SingleOrDefault(l => l.Log == "EventLogName");
   if (log == null) throw new InvalidOperationException("The log does not exist");
   log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 30);