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_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim LastError As Exception = Server.GetLastError
ErrorHandler.ExceptionToString(LastError)
Server.ClearError()
Response.Redirect("~/Error.html")
End Sub
------------------------------------------------
ErrorHandler.vb
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Web
Imports System.Configuration
Imports System.Web.HttpContext
Imports System.Diagnostics
Namespace codetools
Public Class ErrorHandler
Private Const m_strRootException As String =
"System.Web.HttpUnhandledException"
Public Sub ExceptionToString(ByVal ex As Exception)
Dim sb As New System.Text.StringBuilder
With sb
.Append(ex.ToString)
.Append(ExceptionToStringPrivate(ex))
' get ASP specific settings
Try
.Append(GetASPSettings())
Catch e As Exception
.Append(e.Message)
End Try
.Append(Environment.NewLine)
End With
'HttpContext.Current.Response.Write(sb.ToString)
WriteToEventLog(sb.ToString, "N2C", EventLogEntryType.Error, "N2C")
End Sub
Private Function WriteToEventLog(ByVal Entry As String, _
Optional ByVal AppName As String = "VB.NET Application", _
Optional ByVal EventType As _
EventLogEntryType = EventLogEntryType.Information, _
Optional ByVal LogName As String = "Application") 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 EventLogEntryType
' Structure e.g., EventLogEntryType.Warning,
' EventLogEntryType.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", _
' EventLogEntryType.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.SourceExists(AppName) Then
objEventLog.CreateEventSource(AppName, LogName)
End If
objEventLog.Source = AppName
'WriteEntry is overloaded; this is one
'of 10 ways to call it
objEventLog.WriteEntry(Entry, EventType)
Return True
Catch Ex As Exception
Return False
End Try
End Function
Private Function ExceptionToStringPrivate(ByVal ex As Exception, _
Optional ByVal blnIncludeSysInfo As Boolean = True) As String
Dim sb As New System.Text.StringBuilder
If Not (ex.InnerException 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/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp
' don't return the outer root ASP exception; it is redundant.
If ex.GetType.ToString = m_strRootException Then
Return ExceptionToStringPrivate(ex.InnerException)
Else
With sb
.Append(ExceptionToStringPrivate(ex.InnerException, False))
.Append(Environment.NewLine)
.Append("(Outer Exception)")
.Append(Environment.NewLine)
End With
End If
End If
With sb
' get exception-specific information
.Append("Exception Type: ")
Try
.Append(ex.GetType.FullName)
Catch e As Exception
.Append(e.Message)
End Try
.Append(Environment.NewLine)
.Append("Exception Message: ")
Try
.Append(ex.Message)
Catch e As Exception
.Append(e.Message)
End Try
.Append(Environment.NewLine)
.Append("Exception Source: ")
Try
.Append(ex.Source)
Catch e As Exception
.Append(e.Message)
End Try
.Append(Environment.NewLine)
.Append("Exception Target Site: ")
Try
.Append(ex.TargetSite.Name)
Catch e As Exception
.Append(e.Message)
End Try
.Append(Environment.NewLine)
End With
Return sb.ToString
End Function
Private Function GetASPSettings() As String
Dim sb As New System.Text.StringBuilder
With sb
.Append("---- Collections ----")
.Append(Environment.NewLine)
.Append(Environment.NewLine)
.Append(GetHttpVars(System.Web.HttpContext.Current.Request.QueryString,
"QueryString"))
.Append(GetHttpVars(System.Web.HttpContext.Current.Request.Form,
"Form"))
.Append(GetHttpCookieVars)
.Append(GetHttpVars(System.Web.HttpContext.Current.Request.ServerVariables,
"ServerVariables", True, "^ALL_HTTP|^ALL_RAW"))
End With
Return sb.ToString
End Function
Private Function GetHttpVars(ByVal NameValueCollection As
Specialized.NameValueCollection, ByVal strTitle As String, _
Optional ByVal blnSuppressEmptyValues As Boolean = False, _
Optional ByVal strSuppressKeyRegex As String = "") As String
' do we have data in the collection?
If Not NameValueCollection.HasKeys Then Return ""
' if so format the data
Dim sb As New System.Text.StringBuilder
sb.Append(strTitle)
sb.Append(Environment.NewLine)
sb.Append(Environment.NewLine)
Dim blnDisplay As Boolean
Dim strItem As String
' loop through each item in the name value collection
For Each strItem In NameValueCollection
blnDisplay = True
' if we need to suppress empty values set display flag based on if
we have data
If blnSuppressEmptyValues Then
blnDisplay = NameValueCollection(strItem) <> String.Empty
End If
' check to see if our regex does not match
If blnDisplay AndAlso strSuppressKeyRegex <> String.Empty Then
blnDisplay = Not Regex.IsMatch(strItem, strSuppressKeyRegex)
End If
' if conditions where met render item
If blnDisplay Then
AppendLine(sb, strItem, NameValueCollection(strItem))
End If
Next
sb.Append(Environment.NewLine)
Return sb.ToString
End Function
Private Function AppendLine(ByVal sb As System.Text.StringBuilder, _
ByVal strKey As String, ByVal strValue As String) As String
sb.Append(String.Format(" {0, -30}{1}", strKey, strValue))
sb.Append(Environment.NewLine)
End Function
Private Function GetHttpCookieVars() As String
' do we have cookies?
If System.Web.HttpContext.Current.Request.Cookies.Count = 0 Then
Return ""
' build formatted cookie information
Dim sb As New System.Text.StringBuilder
sb.Append("Cookies")
sb.Append(Environment.NewLine)
sb.Append(Environment.NewLine)
Dim strItem As String
For Each strItem In System.Web.HttpContext.Current.Request.Cookies
AppendLine(sb, strItem,
System.Web.HttpContext.Current.Request.Cookies(strItem).Value)
Next
sb.Append(Environment.NewLine)
Return sb.ToString
End Function
Private Function ProcessIdentity() As String
Dim strTemp As String
strTemp = CurrentWindowsIdentity()
If strTemp = "" Then
strTemp = CurrentEnvironmentIdentity()
End If
Return strTemp
End Function
Private Function CurrentWindowsIdentity() As String
Try
Return System.Security.Principal.WindowsIdentity.GetCurrent.Name()
Catch ex As Exception
Return ""
End Try
End Function
Private Function CurrentEnvironmentIdentity() As String
Try
Return System.Environment.UserDomainName + "\" +
System.Environment.UserName
Catch ex As Exception
Return ""
End Try
End Function
Private Function WebCurrentUrl() As String
Dim strUrl As String
With System.Web.HttpContext.Current.Request.ServerVariables
strUrl = "http://" & .Item("server_name")
If .Item("server_port") <> "80" Then
strUrl &= ":" & .Item("server_port")
End If
strUrl &= .Item("url")
If .Item("query_string").Length > 0 Then
strUrl &= "?" & .Item("query_string")
End If
End With
Return strUrl
End Function
End Class
End Namespace