2

I am attempting to create my own custom event log using the below code:

 if (!System.Diagnostics.EventLog.SourceExists("DataStream"))
            {
                EventLog.CreateEventSource("DataStream", "SIRsDetectionDataStreamLog");
            }
            logger = new EventLog();
            logger.Source = "DataStream";

However, I keep getting a security error when checking if the source exists. The error I get is as follows:

SecurityException was unhandled

An unhandled exception of type 'System.Security.SecurityException' occurred in System.dll

Additional information: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • Read the documentation: http://msdn.microsoft.com/en-us/library/vstudio/6s7642se%28v=vs.90%29.aspx. `SecurityException: source was not found, but some or all of the event logs could not be searched`. i.e. you need to be administrator. – adrianm Jan 16 '15 at 21:30
  • 1
    Consider creating the event log when the service is installed. You have to be an administrator to install a service. You would either do this in a service installer using [EventLogInstaller](http://stackoverflow.com/a/1287334/517852) or in your MSI using something like WiX's [EventSource](http://stackoverflow.com/questions/58538/how-do-you-create-an-event-log-source-using-wix) component. – Mike Zboray Jan 16 '15 at 23:41

1 Answers1

3

To create an event source you must have administrative privileges.

All event logs, including the security log, must be searched to determine whether the event source is unique. Non administrative users do not have permission to access the security log therefore a SecurityException is thrown.

Spike
  • 46
  • 2