Writing to event log in C# - do I need to use EventLog.CreateEventSource when writing to Application log?

StackOverflow https://stackoverflow.com/questions/7960930

  •  18-02-2021
  •  | 
  •  

Question

When I use the following code to write to Application Event log, everything works fine:

EventLog log = new EventLog();
log.Source = "Application";
log.WriteEntry("test message", EventLogEntryType.Error);

When I use the code that is from MSDN and all other blogs, I get the security error (I am guessing because CreateEventSource raises it).

string sSource = "MyWebService";
string sLog = "myApplication";
string sMsg = errorMessage;

if (!EventLog.SourceExists(sSource))
   EventLog.CreateEventSource(sSource, sLog);

 EventLog.WriteEntry(sSource, sMsg, EventLogEntryType.Error);  

So, do I need to check whether the source exists if all I need is to write to Application log which is there by default?

What is the proper way to write to EventViewer?

Was it helpful?

Solution

The CreateEventSource method create a new source in the event log, this allow you to write log of your application in the application own group instead of writing in the generic Application group.

Maybe you get an error because the user that you using to create the event source doesn't have the permission to create it, try to run your program as administrator if you are under Vista/7 OS.

The proper way to log in the event viewer depends on your needs, if your application generates a lot of logging message and you want to group this log in an application specific container, maybe it is better to create an application specific log event source and write the log in it, instead if your application generates few log messages and there is no need to group them together you can use the the generic Application log event source ...

OTHER TIPS

I suggest you try log4net, in case you want to write to different sources as well (smtp, file, etc.)

http://logging.apache.org/log4net/release/config-examples.html#eventlogappender

For web apps:

General use:

Similar solution for winforms/windows service.

You need to have admin rights to create an event source. In the first one you are not using a custom source.

A straight WriteEntry will go to the default Application source. The SourceExists and CreateEventSource is if you want to create your own custom source which will be easier to locate any log entries in the Event Viewer.

And yes you need to have rights to create a customer event source as others have mentioned.

You need admin rights to run your application.

Either you can run your application by Going into the debug folder of your application and right click on your .exe file and run as admin

or

You run Visual studio as admin

You don't need to create an event source. It can be a big advantage when generating events that are language-independent or that have substitutions, but it's optional, at least for .NET programs (the BCL provides a default event source).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top