Link to home
Start Free TrialLog in
Avatar of jmestep
jmestep

asked on

Getting name of web server in global.asax used to send page error email

We have a global.asax that someone had written to send an email everytime there was a page error. We have 3 load balanced servers now and need to be able to tell which server the error came from- Splat, Thump, or Bang.
Is there a way I can add to this code the capability to do that?

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Diagnostics" %>

<script language="vb" runat="server">

Sub Application_BeginRequest(Sender as Object, E as EventArgs)
    If (Request.Path.IndexOf(chr(92)) >= 0 OR _
        System.IO.Path.GetFullPath(Request.PhysicalPath) <> Request.PhysicalPath) then
        Throw New HttpException(404, "Not Found")
    End If
End Sub
</script>


<script language="VB" runat="server" AutoEventWireup="True">

Sub Application_Error(sender As Object, e As EventArgs)
    Dim strError as String
    Dim i as Integer
    Dim ErrorInfo As Exception
        strError = "Error in: " & Request.Path & vbLf & "Url: " & Request.RawUrl & vbLf & vbLf
   
    ' Get the exception object for the last error message that occured.
    ErrorInfo = Server.GetLastError().GetBaseException()
    strError = strError & "Error Message: " & ErrorInfo.Message.ToString() & vblf & "Error Source: " & ErrorInfo.Source.ToString() & vblf & "Error Target Site: " & ErrorInfo.TargetSite.ToString() & vblf & vblf & "QueryString Data:" & vblf & "-----------------" & vblf
   
    ' Gathering QueryString information
    For i = 0 To Context.Request.QueryString.Count - 1
        strError = strError & Context.Request.QueryString.Keys(i) & ":" & vbtab & vbtab & Context.Request.QueryString(i) & vblf
    Next
    strError = strError & vblf & "Post Data:" & vblf & "----------" & vblf

    ' Gathering Post Data information
    For i = 0 To Context.Request.Form.Count - 1
        strError = strError & Context.Request.Form.Keys(i) & ":" & vbtab & vbtab & Context.Request.Form(i) & vblf
    Next
    strError = strError & vblf
   
    if (User.Identity.IsAuthenticated) then strError = strError & "User:" & vbtab & vbtab & User.Identity.Name & vblf & vblf
   
    strError = strError & "Exception Stack Trace:" & vblf & "----------------------" & vblf & Server.GetLastError.StackTrace & vblf & vblf & "Server Variables:" & vblf & "-----------------" & vblf

    ' Gathering Server Variables information
    For i = 0 To Context.Request.ServerVariables.Count - 1
        strError = strError & Context.Request.ServerVariables.Keys(i) & ":" & vbtab & vbtab & Context.Request.ServerVariables(i) & vblf
    Next
    strError = strError & vblf

[Code to send emai]
    End Sub

</script>
ASKER CERTIFIED SOLUTION
Avatar of jnhorst
jnhorst

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmestep
jmestep

ASKER

And I can put it under:
strError = strError & vblf   ?
strError=strError & System.Environment.MachineName & vblf

I did that and it didn't break the site, I just need to wait for an error email to see if I put it in the right place.
Avatar of jmestep

ASKER

That worked- thank you very much