Write to Event Log – The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.”

event-log

I'am trying to write some messages to Windows Event log.

The (security) exception will be thrown when calling function "SourceExists()".

private bool CheckIfEventLogSourceExits()
    {
        try
        {
            if (!EventLog.SourceExists(this.BaseEventLog))
            {
                return false;
            }
            return true;
        }
        catch (System.Security.SecurityException)
        {
            return false;
        }
    }

All answers to this question are explaining how you can MANUALLY resolve issue.
Like here: Stackoverflow Thread. Solution would be changing some registry keys

But you can't expect that everyone who consumes your application is aware of these changes.

So my question is, how can we solve this issue programmatically?

Below my code:

 try
            {
                string sLog = "Application";
                if (CheckIfEventLogSourceExits())
                {
                    EventLog.CreateEventSource(this.BaseEventLog, sLog);
                }

                EventLog.WriteEntry(this.BaseEventLog, message, eventLogEntryType);
            }
            catch (Exception ex)
            {
                ex.Source = "WriteToEventLog";
                throw ex;
            }

Best Answer

I had a similar problem installing a webapp onto a new server. The webapp was using nettiers (v.1.1) and I god the Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.

Stack Trace: [SecurityException: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.] System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly, Boolean wantToCreate) +657 System.Diagnostics.EventLog.SourceExists(String source, String machineName, Boolean wantToCreate) +104 System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) +86 System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +201 System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type) +21 Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances.ReportCounterFailure(String message) +141 Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances..ctor(String categoryName, String counterName, Boolean createNewInstance) +439 Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.AddPerformanceCounter(String category, String[] counterNames, Boolean createNewInstance) +169 Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.Initialize(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds) +168 Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent..ctor(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds) +59 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceEvent..ctor(String[] counterNames, EventLogIdentifier[] eventLogIds) +140 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent..ctor(String[] counterNames, EventLogIdentifier[] eventLogIDs) +39 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent..cctor() +248

My solution was to manually enter the following two keys and strings into the registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\

New key: Enterprise Library Data Service Key entry: Name: EventMessageFile, Type: REG_EXPAND_SZ, Data: C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll

New key: Enterprise Library Instrumentation Key entry: Name: EventMessageFile, Type: REG_EXPAND_SZ, Data: C:\Windows\Microsoft.NET\Framework\v4.0.30319\EventLogMessages.dll