Writing to event log

T

Tlink

I am trying to write to the event log using the following code from a aspx
page benhind code.

Function WriteEventlog(ByVal LogName, ByVal MachineName, ByVal
Source, ByVal EventMessage, ByVal EventID, ByRef Category, ByRef
ErrorResults)
***values TLN
..(Dot) "" "Test 1"
0 0 "" ****
Dim sSource As String
Dim sLog As String
Dim sEvent As String
Dim sMachine As String
Try
If Source = "" Then sSource = "EProcessController" Else
sSource = Source
If LogName = "" Then
sLog = "Application"
Else
sLog = LogName
End If
If MachineName = "" Then
sMachine = "."
Else
sMachine = MachineName
End If

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

If EventID > 0 Then
EventLog.WriteEntry(sSource, EventMessage,
EventLogEntryType.Error, EventID, Category)
Else
EventLog.WriteEntry(sSource, EventMessage,
EventLogEntryType.Information, EventID, Category)
End If
Catch ex As Exception
ErrorResults = ex.ToString
End Try

End Function

Results in the following error:

System.Security.SecurityException: Requested registry access is not allowed.
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at System.Diagnostics.EventLog.FindSourceRegistration(String source,
String machineName, Boolean readOnly)
at System.Diagnostics.EventLog.SourceExists(String source, String
machineName)
at Inbizness.DefaultLibrary.WriteEventlog(Object LogName, Object
MachineName, Object Source, Object EventMessage, Object EventID, Object&
Category, Object& ErrorResults) in
D:\Dev\OrderingSystem\code\TClass\Class1.vb:line 381"

I have set the relevant registry key to allow the r/w of the event log yet
no go.
 
D

Dominick Baier [DevelopMentor]

Hello Tlink,

creating an event source requires special (admin) privileges -

you have two choices -

write a little console app that creates the event source for you (run it
under the admin account)
change the ACLs on some registry keys (not really recommended) :

If your application needs to create event sources, you need to ensure that
the application's identity has the relevant permissions on the following
registry key.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog

At minimum, your Web application process identity, which defaults to Network
Service on Windows Server 2003, must have the following permissions on this
registry key:

Query key value
Set key value
Create subkey
Enumerate subkeys
Notify
Read
These settings must be applied to the key shown above and subkeys. Alternatively,
you can create event sources at installation time when administrative privileges
are available.
 
P

Paul Glavich [MVP ASP.NET]

What we used to do was install our web application and then turn on
impersonation, and logon as the machine administrator.

Once accessed, we went back in and disabled impersonation, and all the event
sources were successfully created for us so we could then use the normal
user. On ly applies if your doing the install of course but it was an easy
way around it.

--
- Paul Glavich
MVP ASP.NET
http://weblogs.asp.net/pglavich
ASPInsiders member - http://www.aspinsiders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top