-1

I have a Windows Service. I'm struggling to get an EventLog working properly.

In the Windows Service's constructor I do:

    public MyService()
    {
        InitializeComponent();

        AutoLog = false;
        if (!EventLog.SourceExists(ServiceName))
        {
            EventSourceCreationData creationData = new EventSourceCreationData(ServiceName, ServiceName);
            EventLog.CreateEventSource(creationData);
        }
    }

After I run the service, I get no exceptions, but I can't see anything under Application and Services Logs in Event Viewer! (even after computer reset).

I checked my registry and my service appears in

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MyService

not here:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\MyService

I install Windows Service via Visual Studio's Developer Command Prompt for VS2015 opened as Administrator.

Why there? why not in eventlog? Why I can't see it in Event Viewer?

Thanks!

Ish Thomas
  • 2,270
  • 2
  • 27
  • 57

1 Answers1

-1

Use the following code, Check source exist before creating it. Check the naming convention here: https://msdn.microsoft.com/en-us/library/2awhba7a.aspx

string machineName = "MyRemoteServerName";
string source = "MyCustomApp";
string logName = "Application";//can be Application, System, or a custom log name.

if (EventLog.SourceExists(logName, machineName))
   return;

EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(source, logName);

eventSourceCreationData.MachineName = machineName;

EventLog.CreateEventSource(eventSourceCreationData);

Refer: https://msdn.microsoft.com/en-us/library/system.diagnostics.eventsourcecreationdata(v=vs.110).aspx

Vijai
  • 2,369
  • 3
  • 25
  • 32