Mehwish
Mehwish

Reputation: 11

Custom Event Logging in a Windows Service

In the project installer i am creating a custom event log. But when my service starts my all logs are going to the "Application" instead of my cuustom log. Below is the code which i have added to installer.

// Create Event Source and Event Log     
EventLogInstaller logInstaller = new EventLogInstaller();
logInstaller.Source = "MyServices";
logInstaller.Log = "MyService Events";

Installers.Add(logInstaller);

Furthurmore the name of service is MyService.exe.

When I uninstall and re-install the service, i installation fail with the following install log;

Running a transacted installation.

Beginning the Install phase of the installation. See the contents of the log file for the D:\MyService\MyService\bin\Release\MyService.exe assembly's progress. The file is located at D:\MyService\MyService\bin\Release\MyService.InstallLog.

An exception occurred during the Install phase. System.ArgumentException: Source MyServices already exists on the local computer.

The Rollback phase of the installation is beginning. See the contents of the log file for the D:\MyService\MyService\bin\Release\MyService.exe assembly's progress. The file is located at D:\MyService\MyService\bin\Release\MyService.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.

And this is how i write log entry;

EventLog.WriteEntry("MyServices", logMessage, logType);

Can someone please help me what wrong i am doing.

Upvotes: 0

Views: 2687

Answers (2)

hzh
hzh

Reputation: 351

I had a similar problem and solved it by:

  1. Stop current service.
  2. Close task manager, service manage tool and windows event viewer.
  3. Use intallutil.exe /u to uninstall current service.
  4. delete event log registry key of current service.(This is important!) (\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\yourCurrentLogName) You can find your current log and you can confirm your deletion under windows event viewer.
  5. Use installutil.exe to install service again.
  6. Restart Windows.(This is important, too!) I'm not sure whether it is necessary or not. But I kept getting errors even I logged out. Everything got fine since I restart windows. (Maybe AWS cached something?)

Upvotes: 1

SoaperPlus
SoaperPlus

Reputation: 159

Need to create a EventSource in this way:

if (!EventLog.SourceExists(source))
{
  var eventSourceData = new EventSourceCreationData(source, logName);
  EventLog.CreateEventSource(eventSourceData);
}

source - "MyServices"

logName - some of your program name.

Insert the data need in this way:

log = new EventLog();
log.Source = "MyServices";
log.WriteEntry(message, entryType);

Upvotes: 0

Related Questions