简体   繁体   中英

Where should be custom EventLog created?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM