You could be right. I have set the full control permissions for the
asp_net account in reg edit for the event log WriteToEventLog function.
May be it is that that is causing the problem. I will write some code
to see.
Code below in case it could be something else
in web.config
<!-- CUSTOM ERROR MESSAGES -->
<customErrors mode="On" defaultRedirect ="~/Error.aspx" >
<error statusCode="404 " redirect="~/"/>
</customErrors>
in globbal
Sub Application_Err or(ByVal sender As Object, ByVal e As EventArgs)
Dim LastError As Exception = Server.GetLastE rror
ErrorHandler.Ex ceptionToString (LastError)
Server.ClearErr or()
Response.Redire ct("~/Error.html")
End Sub
------------------------------------------------
ErrorHandler.vb
Imports System.Reflecti on
Imports System.Text.Reg ularExpressions
Imports System.Xml
Imports System.Web
Imports System.Configur ation
Imports System.Web.Http Context
Imports System.Diagnost ics
Namespace codetools
Public Class ErrorHandler
Private Const m_strRootExcept ion As String =
"System.Web.Htt pUnhandledExcep tion"
Public Sub ExceptionToStri ng(ByVal ex As Exception)
Dim sb As New System.Text.Str ingBuilder
With sb
.Append(ex.ToSt ring)
.Append(Excepti onToStringPriva te(ex))
' get ASP specific settings
Try
.Append(GetASPS ettings())
Catch e As Exception
.Append(e.Messa ge)
End Try
.Append(Environ ment.NewLine)
End With
'HttpContext.Cu rrent.Response. Write(sb.ToStri ng)
WriteToEventLog (sb.ToString, "N2C", EventLogEntryTy pe.Error, "N2C")
End Sub
Private Function WriteToEventLog (ByVal Entry As String, _
Optional ByVal AppName As String = "VB.NET Application", _
Optional ByVal EventType As _
EventLogEntryTy pe = EventLogEntryTy pe.Information, _
Optional ByVal LogName As String = "Applicatio n") As Boolean
'************** *************** *************** *************** **
'PURPOSE: Write Entry to Event Log using VB.NET
'PARAMETERS: Entry - Value to Write
' AppName - Name of Client Application. Needed
' because before writing to event log, you must
' have a named EventLog source.
' EventType - Entry Type, from EventLogEntryTy pe
' Structure e.g., EventLogEntryTy pe.Warning,
' EventLogEntryTy pe.Error
' LogName: Name of Log (System, Application;
' Security is read-only) If you
' specify a non-existent log, the log will be
' created
'RETURNS: True if successful, false if not
'EXAMPLES:
'1. Simple Example, Accepting All Defaults
' WriteToEventLog "Hello Event Log"
'2. Specify EventSource, EventType, and LogName
' WriteToEventLog ("Danger, Danger, Danger", "MyVbApp", _
' EventLogEntryTy pe.Warning, "System")
'
'NOTE: EventSources are tightly tied to their log.
' So don't use the same source name for different
' logs, and vice versa
'************** *************** *************** **********
Dim objEventLog As New EventLog
Try
'Register the App as an Event Source
If Not objEventLog.Sou rceExists(AppNa me) Then
objEventLog.Cre ateEventSource( AppName, LogName)
End If
objEventLog.Sou rce = AppName
'WriteEntry is overloaded; this is one
'of 10 ways to call it
objEventLog.Wri teEntry(Entry, EventType)
Return True
Catch Ex As Exception
Return False
End Try
End Function
Private Function ExceptionToStri ngPrivate(ByVal ex As Exception, _
Optional ByVal blnIncludeSysIn fo As Boolean = True) As String
Dim sb As New System.Text.Str ingBuilder
If Not (ex.InnerExcept ion Is Nothing) Then
' sometimes the original exception is wrapped in a more relevant
outer exception
' the detail exception is the "inner" exception
' see
http://msdn.microsoft.com/library/de...ceptdotnet.asp
' don't return the outer root ASP exception; it is redundant.
If ex.GetType.ToSt ring = m_strRootExcept ion Then
Return ExceptionToStri ngPrivate(ex.In nerException)
Else
With sb
.Append(Excepti onToStringPriva te(ex.InnerExce ption, False))
.Append(Environ ment.NewLine)
.Append("(Outer Exception)")
.Append(Environ ment.NewLine)
End With
End If
End If
With sb
' get exception-specific information
.Append("Except ion Type: ")
Try
.Append(ex.GetT ype.FullName)
Catch e As Exception
.Append(e.Messa ge)
End Try
.Append(Environ ment.NewLine)
.Append("Except ion Message: ")
Try
.Append(ex.Mess age)
Catch e As Exception
.Append(e.Messa ge)
End Try
.Append(Environ ment.NewLine)
.Append("Except ion Source: ")
Try
.Append(ex.Sour ce)
Catch e As Exception
.Append(e.Messa ge)
End Try
.Append(Environ ment.NewLine)
.Append("Except ion Target Site: ")
Try
.Append(ex.Targ etSite.Name)
Catch e As Exception
.Append(e.Messa ge)
End Try
.Append(Environ ment.NewLine)
End With
Return sb.ToString
End Function
Private Function GetASPSettings( ) As String
Dim sb As New System.Text.Str ingBuilder
With sb
.Append("---- Collections ----")
.Append(Environ ment.NewLine)
.Append(Environ ment.NewLine)
.Append(GetHttp Vars(System.Web .HttpContext.Cu rrent.Request.Q ueryString,
"QueryStrin g"))
.Append(GetHttp Vars(System.Web .HttpContext.Cu rrent.Request.F orm,
"Form"))
.Append(GetHttp CookieVars)
.Append(GetHttp Vars(System.Web .HttpContext.Cu rrent.Request.S erverVariables,
"ServerVariable s", True, "^ALL_HTTP|^ALL _RAW"))
End With
Return sb.ToString
End Function
Private Function GetHttpVars(ByV al NameValueCollec tion As
Specialized.Nam eValueCollectio n, ByVal strTitle As String, _
Optional ByVal blnSuppressEmpt yValues As Boolean = False, _
Optional ByVal strSuppressKeyR egex As String = "") As String
' do we have data in the collection?
If Not NameValueCollec tion.HasKeys Then Return ""
' if so format the data
Dim sb As New System.Text.Str ingBuilder
sb.Append(strTi tle)
sb.Append(Envir onment.NewLine)
sb.Append(Envir onment.NewLine)
Dim blnDisplay As Boolean
Dim strItem As String
' loop through each item in the name value collection
For Each strItem In NameValueCollec tion
blnDisplay = True
' if we need to suppress empty values set display flag based on if
we have data
If blnSuppressEmpt yValues Then
blnDisplay = NameValueCollec tion(strItem) <> String.Empty
End If
' check to see if our regex does not match
If blnDisplay AndAlso strSuppressKeyR egex <> String.Empty Then
blnDisplay = Not Regex.IsMatch(s trItem, strSuppressKeyR egex)
End If
' if conditions where met render item
If blnDisplay Then
AppendLine(sb, strItem, NameValueCollec tion(strItem))
End If
Next
sb.Append(Envir onment.NewLine)
Return sb.ToString
End Function
Private Function AppendLine(ByVa l sb As System.Text.Str ingBuilder, _
ByVal strKey As String, ByVal strValue As String) As String
sb.Append(Strin g.Format(" {0, -30}{1}", strKey, strValue))
sb.Append(Envir onment.NewLine)
End Function
Private Function GetHttpCookieVa rs() As String
' do we have cookies?
If System.Web.Http Context.Current .Request.Cookie s.Count = 0 Then
Return ""
' build formatted cookie information
Dim sb As New System.Text.Str ingBuilder
sb.Append("Cook ies")
sb.Append(Envir onment.NewLine)
sb.Append(Envir onment.NewLine)
Dim strItem As String
For Each strItem In System.Web.Http Context.Current .Request.Cookie s
AppendLine(sb, strItem,
System.Web.Http Context.Current .Request.Cookie s(strItem).Valu e)
Next
sb.Append(Envir onment.NewLine)
Return sb.ToString
End Function
Private Function ProcessIdentity () As String
Dim strTemp As String
strTemp = CurrentWindowsI dentity()
If strTemp = "" Then
strTemp = CurrentEnvironm entIdentity()
End If
Return strTemp
End Function
Private Function CurrentWindowsI dentity() As String
Try
Return System.Security .Principal.Wind owsIdentity.Get Current.Name()
Catch ex As Exception
Return ""
End Try
End Function
Private Function CurrentEnvironm entIdentity() As String
Try
Return System.Environm ent.UserDomainN ame + "\" +
System.Environm ent.UserName
Catch ex As Exception
Return ""
End Try
End Function
Private Function WebCurrentUrl() As String
Dim strUrl As String
With System.Web.Http Context.Current .Request.Server Variables
strUrl = "http://" & .Item("server_n ame")
If .Item("server_p ort") <> "80" Then
strUrl &= ":" & .Item("server_p ort")
End If
strUrl &= .Item("url")
If .Item("query_st ring").Length > 0 Then
strUrl &= "?" & .Item("query_st ring")
End If
End With
Return strUrl
End Function
End Class
End Namespace