Writing errors to the Event Log

  • Thread starter Thread starter MattB
  • Start date Start date
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
 
Is it throwing an exception?
Asp.net does not by default have rights to create new event logs. Try
playing around with security settings.
 
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
 
Back
Top