Error writing to event log using Visual Basic


I am programming in Visual Studio 2015 a Visual Basic project.

Goal is to write errors to event log. I have tried several examples like mentioned in:

It is not working like I expect it to work allready at the start.

Dim sSource As String
Dim sLog As String
Dim sEvent As String
Dim sMachine as String

sSource = "My Application"
sLog = "Application"
sEvent = "Error description"
sMachine = environment.machinename.tostring

If Not EventLog.SourceExists(sSource, sMachine) Then
            EventLog.CreateEventSource(sSource, sLog, sMachine)
End If

Open in new window

When firing this code I get the error:

Unkonown error: 5
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. The inaccessable logs: Security.

This happens immidiately when code reaches:
If Not EventLog.SourceExists(sSource, sMachine) Then

Open in new window

Anybody any idea how to fix this? Lots of articles on the internet but all regarding older versions of Visual Studio.

The exact code now is:
[code]Sub log_event(ByRef sEvent As String)

        Dim aLog As EventLog
        Dim myLog As New EventLog
        Dim aEventLogList() As EventLog
        Dim aLogEntry As EventLogEntry
        Dim aLogEntries As EventLogEntryCollection

        Dim sSource As String
        Dim sLog As String
        Dim sMachine As String

        sSource = "Werkvergunning"
        sLog = "Application"
        sMachine = Environment.MachineName.ToString

        If Not EventLog.SourceExists(sSource) Then

            EventLog.CreateEventSource(sSource, sLog)

        End If

        With myLog

            .Source = sSource
            .Log = sLog
            .EnableRaisingEvents = True

            .WriteEntry("Writing Error entry to event log", EventLogEntryType.Error)
‘            .WriteEntry("Writing Information entry to event", EventLogEntryType.Information)
‘            .WriteEntry("Writing Warning entry to event", EventLogEntryType.Warning)

        End With

    End Sub

Open in new window


Thanx in advance.
Rodney CarrilhoIT EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
could it be a permission issue? try to run as administrator
Rodney CarrilhoIT EngineerAuthor Commented:

Hi Eric, I am administrator on the machine so It should not be a permission issue. When users will run the application the will not allways have administrator rights so it should be possible to write to the event log with user rights I suppose.

Thanx for your comment :)
You still need to run your application *AS* administrator.  When you launch an application it starts in the context of the user that is running the application and administrative rights (even if you are an administrator) have to be requested.

You can resolve this by adding an application manifest which will require administrative rights and automatically request such rights.

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Éric MoreauSenior .Net ConsultantCommented:
I just made the test. It is definitely a permission issue.

I copied the code in a new project, executed it and ran into the same issue you experienced.

I ran the .exe from the bin/debug folder, same issue.

right-clicked the .exe and selected "run as administrator" and it ran correctly. I saw the result in the event viewer.

I ran the .exe (with regular privileges) again, now that the EventSource exist, and results were added to the event viewer again.

look at
To create an event source in Windows Vista and later or Windows Server 2003, you must have administrative privileges.

The reason for this requirement is that all event logs, including security, must be searched to determine whether the event source is unique. Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.
To add an appropriate application manifest, open your project properties:Capture.PNGClick the View Settings button, you should see an app.manifest appear in your editor window:Capture.PNGChange line 19 so that it requires administrator:Capture.PNGBuilt with the application manifest requiring administrator your application icon should have a shield over it; e.g. -Capture.PNG-saige-
Rodney CarrilhoIT EngineerAuthor Commented:
Hi Eric,

I am building a Microsoft Excel Plugin. So I create a setup and edit a registry key to Install the PlugIn (with the Visual Basic code) and add a registry key to make the plugin available for all users (Citrix environment).

So how much I understand what you have tried it will not help in this case but thanx for checking the permission part for now I am sure I have to find an answer for that part. Possibly in the security of the eventlog on the Citrix machine.

But thanx for your response.
Rodney CarrilhoIT EngineerAuthor Commented:

I created an executable with a button and the same code. When starting the executable as Administrator and pressing the button the source was created in the Event log. After that I had no issues with the Sourcecode in the Plugin. So I will probably have to run the executable as administrator once before installing the Plugin or use an existing one.

Even after cleared the event log the plugin was able to create the event log items :) (before creating the source again with the executable). So it looks like a one-time create thing I can do for example when running the setup with an event log with the message "Application installed". Thanx for your comment, thoudh it is not the solution I was hoping for it's real close to a solution.

Will the Manifest work for the Plug?

Éric MoreauSenior .Net ConsultantCommented:
depending on what you are using to install your plugin, maybe you can create the event source from that package because normally the setup runs with administrator privileges
Rodney CarrilhoIT EngineerAuthor Commented:
Hi all,

Like mentioned I created an executable and in the setup I run that excutable. That does the trick. I found my own solution with help from you guys.

Thanx all.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FYI: administrative rights are not needed to write events unless you want to write to the security event log. Don't confuse this with creating event sources.

It could be that windows has a bug here and requests high permissions if you do it that way. Or, your code really tried to create an event source, although you just want to write sn event. You can confirm what I am saying by using a different method: Powershell new-winevent works as non-admin.
Chinmay PatelChief Technology NinjaCommented:
Hi Rodney,

On the risk of being labeled captain obvious, your finding/assumption was right. To create the event log source you need the admin permission, so during the setup OR initialization (whatever you want to call it), you must have Admin privilege. Afterwards, writing to that log does not require admin privilege.

How did you fix it?
Rodney CarrilhoIT EngineerAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.