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:
- Add a class to your application named InstallActions.cs
- The file content should resemble this:
using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; [RunInstaller(true)] public class InstallEventLog : Installer { public const string EventSource = "MyEventSource"; public InstallEventLog() { var eventLogInstaller = new EventLogInstaller(); eventLogInstaller.Source = EventSource; Installers.Add(eventLogInstaller); } }
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. - Add a Visual Studio Setup project to your solution, if one doesn't already exist.
- Right-click the Application Folder node, in the left window pane
(The context menu appears) - In the context menu, select Add > Project Output...
(The Add Project Output Group dialog appears)
- Select the relevant output required for installation (typically Primary Output and Active configuration)
- Click the OK button
- Right-click the setup project node in the Solution Explorer
(The context menu appears) - In the context menu, select View > Custom Actions
- Right-click the Install node
(The context menu appears) - In the context menu, select Add Custom Action...
(The Select Item in Project dialog appears)
- In the dialog window, double-click the Application Folder node, or select it from the drop-down list
- 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.
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.
nice one
ReplyDeleteWow, 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
ReplyDelete