Application_error and windows 2003

R

Rippo

I have a problem with the application_error event being fired from my
asp.net application with windows 2003 server standard edition. The
event fires fine from my test machine (XP) and all works well. However
the event never appears to fire from windows 2003. I have tried just
about everything and can not find an answer. I cannot debug through the
windows 2003 server as I do not have VS installed on it plus it is a
live server

Can any one help?


Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim LastError As Exception = Server.GetLastError
ErrorHandler.ExceptionToString(LastError)
Server.ClearError()
Response.Redirect("~/Error.html")
End Sub
 
G

Guest

Hi,

Are you trying to test the exception management code of your app in ur
Win2k3 server? In other words, are you purposefully thowing an exception to
see whether it's handled properly?

If yes, how exactly are you doing that?
 
R

Rippo

Yes. I am purposefully throwing an exception. Like my original post
says I can get the error to display on XP but not on Windows 2003.

I have had a further thought and may think that this may be a
permission issue. The server in question is running the Ensim control
panel and it does stuff with user accounts that locks down IIS pretty
well. So for a check what group does the IIS user need to allow for
application_error to be fired?

At the moment the user is w#21 and groups are AllUsers@n2cb, iis_wpg
and wvh_anon_users_group
 
G

Guest

Hi,

I don't think permissions plat a role in application_error. My hunch is
there should be a permission problem in what is being done within the
application_error handler. And maybe that is the reason why it seems like the
error event is not firing.

Could you please post the code written in application_error?
 
R

Rippo

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
 
G

Guest

Hi,

I am going thru the code.

Meanwhile, one question - are you using impersonation? If yes, then you need
to provide the impersonated user the rights instead of the ASPNET user.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top