Writing errors to the Event Log

M

MattB

I'm writing some code to log errors in a web app (vb/asp.net). I found a
straightforward-looking example here:
http://www.c-sharpcorner.com/Code/2002/May/CustomErrorHandlingASPNEt.asp

But mine doesn't seem to be working.

If I step through the code I put in global.asax.vb it seems to get to
the line where it checks for the existence of the log and then returns
without writing anything to the event log. I don;t know why or how to
troubleshoot it. Any suggestions? Here's the code I'm using:

----------------
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim ErrorDescription As String = Server.GetLastError.ToString

'Creation of event log if it does not exist
Dim EventLogName As String = "Test"
If (Not EventLog.SourceExists(EventLogName)) Then ' <--
stepping through code seems to end here
EventLog.CreateEventSource(EventLogName, EventLogName)
End If

' Inserting into event log
Dim Log As New EventLog
Log.Source = EventLogName
Log.WriteEntry(ErrorDescription, EventLogEntryType.Error)

End Sub
 
M

Marina

Is it throwing an exception?
Asp.net does not by default have rights to create new event logs. Try
playing around with security settings.
 
B

bruce barker

asp.net usually does not have permission to create the event source which is
actually a registry entry. create the source yourself, or give the asp.net
account write permission to the register using regedt32. the path is:

hkey_local_machine\system\currentcontrolset\services\eventlog\application

-- bruce (sqlwork.com)


| I'm writing some code to log errors in a web app (vb/asp.net). I found a
| straightforward-looking example here:
| http://www.c-sharpcorner.com/Code/2002/May/CustomErrorHandlingASPNEt.asp
|
| But mine doesn't seem to be working.
|
| If I step through the code I put in global.asax.vb it seems to get to
| the line where it checks for the existence of the log and then returns
| without writing anything to the event log. I don;t know why or how to
| troubleshoot it. Any suggestions? Here's the code I'm using:
|
| ----------------
| Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
| ' Fires when an error occurs
| Dim ErrorDescription As String = Server.GetLastError.ToString
|
| 'Creation of event log if it does not exist
| Dim EventLogName As String = "Test"
| If (Not EventLog.SourceExists(EventLogName)) Then ' <--
| stepping through code seems to end here
| EventLog.CreateEventSource(EventLogName, EventLogName)
| End If
|
| ' Inserting into event log
| Dim Log As New EventLog
| Log.Source = EventLogName
| Log.WriteEntry(ErrorDescription, EventLogEntryType.Error)
|
| End Sub
| ----------------
|
| Thanks!
|
| Matt
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top