How to write to the event log?

I am trying to get my .Net Windows service to go to a custom event log. I use EventLogInstaller to create an event log and source when the application is installed. I read here that it takes some time to register a source, so they recommend restarting the application before trying to write it to the log.

Since this is a Windows service, I did not need to force a restart of the computer or force the user to manually start the service, so I use this code to wait for the log to exist and the service to start automatically.

 while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service"))) { Thread.Sleep(1000); } System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service"); controller.Start(); 

My problem is that the events from the service are still being written to the application log, and although I see my own log in the registry editor, it does not appear in the Windows 7 event viewer.

Any help would be greatly appreciated.

+4
source share
4 answers

Try this snippet:

edit - caveat: if the user executing the code does not have administrator privileges, this will throw an exception. Since this is so (and if the user does not have these rights), it is best to use the log, and simply write to it. see Source not found, but some or all event logs cannot be found

 if (!EventLog.SourceExists("MyApplicationEventLog")) { EventSourceCreationData eventSourceData = new EventSourceCreationData("MyApplicationEventLog", "MyApplicationEventLog"); EventLog.CreateEventSource(eventSourceData); } using (EventLog myLogger = new EventLog("MyApplicationEventLog", ".", "MyApplicationEventLog")) { myLogger.WriteEntry("Error message", EventLogEntryType.Error); myLogger.WriteEntry("Info message", EventLogEntryType.Information); } 
+3
source

By default, when the service is installed, the source receives a connection to the application log. If we change this connection at a later point, the system will need a reboot.

However, we can prevent the service from integrating with the application log by setting the autolog property to false in the constructor of the class of service (a class that inherits from the database). http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.autolog.aspx

+5
source

It looks like you are writing to the event log like this:

  EventLog.WriteEntry("Source", "Message"); 

This will be written to the application log.

If you use the code in a simons message with the creation of myLogger, you can specify the log name.

+1
source

I did something like this:

  var logName = EventLog.LogNameFromSourceName("MyApp", Environment.MachineName); //delete the source if it associated with the wrong Log if (!string.IsNullOrEmpty(logName) & logName != "MyLog") { EventLog.DeleteEventSource("MyApp", Environment.MachineName); } if (!EventLog.SourceExists("MyApp")) { EventLog.CreateEventSource("MyApp", "MyLog"); } 
0
source

Source: https://habr.com/ru/post/1310657/


All Articles