Link to home
Start Free TrialLog in
Avatar of Yibblo BV R.O. Carrilho
Yibblo BV R.O. CarrilhoFlag for Netherlands

asked on

Error writing to event log using Visual Basic

Hi,

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:
https://support.microsoft.com/nl-nl/help/301279/how-to-write-to-an-event-log-by-using-visual-basic-net-or-visual-basic

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

[/code]

Thanx in advance.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

could it be a permission issue? try to run as administrator
Avatar of Yibblo BV R.O. Carrilho

ASKER

https://www.experts-exchange.com/questions/29148306/Error-writing-to-event-log-using-Visual-Basic.html?anchorAnswerId=42879266#a42879266

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.

-saige-
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 https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog.createeventsource?redirectedfrom=MSDN&view=netframework-4.8#System_Diagnostics_EventLog_CreateEventSource_System_String_System_String_:
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:User generated imageClick the View Settings button, you should see an app.manifest appear in your editor window:User generated imageChange line 19 so that it requires administrator:User generated imageBuilt with the application manifest requiring administrator your application icon should have a shield over it; e.g. -User generated image-saige-
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.
Eric,

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.

@it_sage
Will the Manifest work for the Plug?

regards,
Rodney
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
ASKER CERTIFIED SOLUTION
Avatar of Yibblo BV R.O. Carrilho
Yibblo BV R.O. Carrilho
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.
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.

Regards,
Chinmay.
How did you fix it?