I am following the instructions on:
http://msdn.microsoft.com/en-us/library/9k985bc9.aspx - How to create services
http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx - How to: Add Installers to Your Service Application
then I install the service, using an elevated command window and the command installutil.exe -i WindowsService1.exe
This builds, and installs the service and everything perfectly.
However, I would like to add a log to my service, leading me to wanting to create the event log source, and writing an initial message to it in the installer ( as the installer will run with elevated privileges, whereas the actual service might not )
So I add the following to the installer code (projectinstaller.cs
)
public ProjectInstaller()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("TestService"))
{
System.Diagnostics.EventLog.CreateEventSource(
"TestService Service", "TestService");
}
System.Diagnostics.EventLog TestEventLog = new System.Diagnostics.EventLog();
TestEventLog.Source = "TestService ServiceInstaller";
TestEventLog.Log = "TestService";
TestEventLog.WriteEntry("New log created");
}
This still builds, but now installutil.exe -i WindowsService1.exe
( still being run with elevated privileges ) throws the following error:
An exception occurred during the Install phase.
System.InvalidOperationException: Unable to create an instance of the WindowsService1.ProjectInstaller installer type.
The inner exception System.Reflection.TargetInvocationException was thrown with the following error message: Exception has been thrown by the target of an invocation..
The inner exception System.ArgumentException was thrown with the following error message: Log TestService has already been registered as a source on the local computer..
Google has lead me to believe that this is some form of permission error with windows RE creating logs, and has led me to a solution that involves manually writing stuff to the registry. I would however prefer to not muck around in the registry if there is a better way to do it ( e.g. a native c# way to get this to work )
How I can register the log and write an initial message to it when installing the service?
The exception is telling you exactly what the problem is. The last line reads:
The inner exception System.ArgumentException was thrown with the following error message: Log TestService has already been registered as a source on the local computer..
You're trying to create a source that already exists.
See my answer to a similar question for details.