4

I'm trying to create a windows service and I need it to be able to write to the event logs. I've added an EventLog component to my Service project, and set the Log property to be ccs_wscln_log and the Source property to be ccs_wscln (same name as the service).

I have also created and installer for this project. My problem is that whenever I install the service, it creates the ccs_wscln registry key under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application when it SHOULD be HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\ccs_wscln_log.
The problem with this is that when I try to launch the service, I get an error that says

"The source 'ccs_wscln' is not registered in log 'ccs_wscln_log'. (It is registered in log 'Application'). The source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the source property'.

I've found that if I delete the ccs_wscln registry key under the Application folder, when I start the service it will run and generate the ccs_wscln_log entry under EventLog. So my question is, when I install the application, why is it creating an entry for me automatically under Application, and how do I prevent it from doing this?

I found another post on SO that said I need to restart my computer if I had installed it before under Application, so I tried that but when I reloaded the solution, I couldn't even bring up the designer because it was complaining that the registry entry was missing and it would still install under Application anyways.

alexD
  • 2,368
  • 2
  • 32
  • 43

2 Answers2

2

I created a tutorial for creating a Windows service from scratch using C#. I address the issue of writing to an application-specific log. See Step 9 here for details.

Community
  • 1
  • 1
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
  • What happens if I already have an EventLog component in my Service class with the same source/log names? I tried something similar to that a few minutes ago, and when I tried to install it I got a message complaining that the source already exists. Perhaps I need to restart my computer again – alexD Jul 19 '11 at 23:07
0

I think, you would need following in your ServiceInstaller class.

this.Installers.Clear();

Above code needs to be just before you are adding a range of installers.

That is because, EventLogInstaller is added by default. Calling clear will remove it. Alternatively, you can loop through the installers collection, select the specific type (EventLogInstaller) and assign it required LogName and EventSource name.

cdpnet
  • 580
  • 2
  • 7
  • 23