Hi all,

I have come across this the other day and I thought I should share.

The problem:
From Windows Vista onwards, with all the UAC security stuff I have noticed that you can't just go ahead and add an entry in the EventLog if your event source does not already exist...
So the line of code below:
VB.NET Code:
  1. EventLog.WriteEntry("My Application", message, EventLogEntryType.Error)
might throw an error saying something along the line:
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
The solution:
What you can do in order to avoid this is to explicitly add the event source to your event log:
VB.NET Code:
  1. Dim eventSource As String = "My Application"
  2. If Not EventLog.SourceExists(eventSource) Then
  3.      EventLog.CreateEventSource(eventSource, "Application")
  4.              
  5. End If

To do this though you need to have admin rights and also if you are going to do this with an application you have to run the application as an administrator (right click, Run as Administrator)

Once the event source is there then you can resume adding messages to the event log as normal