0

I am hosting a website on 123-Reg and currently display my error messages to users - I've asked them to send me screenshots in the past so I can debug what's going on.

However, I'd like to set up a custom error page with some text and then have the error emailed to my inbox. However anything I've found on the web seems to suggest making changes to IIS.

I don't have access to IIS due to my hosting with 123-Reg. Can this be achieved on an application level?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ant Moore
  • 123
  • 1
  • 12

2 Answers2

0

You can add Elmah easily:

  1. Right-click your project, select "Manage Nuget Packages"
  2. Click on "Online"--> "NuGet official package source"
  3. Type Elmah in the search box and select the first hit. Click on Install.
  4. Enable "Remote access" in your web.config by locating the following line and changing it as so:

    <elmah>
    <!--
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
        more information on remote access and securing ELMAH.
     -->
    <security allowRemoteAccess="true" />
    </elmah>
    

You are almost done: Make sure you follow the instructions in the URL above to secure your error pages and prevent unauthorized access to your logs.

UPDATE I should say that above will only show you errors on the Elmah dashboard. You can configure it to also email errors. You will find an example on how to do this here: Send email from Elmah?

Elmah

Community
  • 1
  • 1
Icarus
  • 63,293
  • 14
  • 100
  • 115
0

On the Global.asax page just add the below code

 void Application_Error(object sender, EventArgs e) 
   { 
        // Code that runs when an unhandled error occurs
        // Get the error details 
        HttpException lastErrorWrapper = Server.GetLastError() as HttpException;

        Exception lastError = lastErrorWrapper;
        if (lastErrorWrapper.InnerException != null)
            lastError = lastErrorWrapper.InnerException;

        string lastErrorTypeName = lastError.GetType().ToString();
        string lastErrorMessage = lastError.Message;
        string lastErrorStackTrace = lastError.StackTrace;

        const string ToAddress = "youremail@yourdomain.com";
        const string FromAddress = "noreply@yourdomain.com";
        const string Subject = "An Error Has Occurred on Application Name!";

        // Create the MailMessage object 
        MailMessage msg = new MailMessage();
        string[] eAddresses = ToAddress.Split(';');

        for (int i = 0; i < eAddresses.Length; i++)
        {
            if (eAddresses[i].Trim() != "")
            {
                msg.To.Add(new MailAddress(eAddresses[i]));

            }
        }
        msg.From = (new MailAddress(FromAddress));

        msg.Subject = Subject;
        msg.IsBodyHtml = true;
        msg.Priority = MailPriority.High;
        msg.Body = string.Format(@" 
<html> 
<body> 
  <h1>An Error Has Occurred!</h1> 
  <table cellpadding=""5"" cellspacing=""0"" border=""1""> 
  <tr> 
  <td text-align: right;font-weight: bold"">URL:</td> 
  <td>{0}</td> 
  </tr> 
  <tr> 
  <td text-align: right;font-weight: bold"">User:</td> 
  <td>{1}</td> 
  </tr> 
  <tr> 
  <td text-align: right;font-weight: bold"">Exception Type:</td> 
  <td>{2}</td> 
  </tr> 
  <tr> 
  <td text-align: right;font-weight: bold"">Message:</td> 
  <td>{3}</td> 
  </tr> 
  <tr> 
  <td text-align: right;font-weight: bold"">Stack Trace:</td> 
  <td>{4}</td> 
  </tr>  
  </table> 
</body> 
</html>",
            Request.Url,
            Request.ServerVariables["LOGON_USER"],
            lastErrorTypeName,
            lastErrorMessage,
            lastErrorStackTrace.Replace(Environment.NewLine, "<br />"));


        // Attach the Yellow Screen of Death for this error    
        string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage();
        if (!string.IsNullOrEmpty(YSODmarkup))
        {
            Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm");
            msg.Attachments.Add(YSOD);
        }


        // Send the email 
        SmtpClient smtp = new SmtpClient();
        smtp.Send(msg);

        Server.Transfer("/Error.aspx");

    }
Cherian M Paul
  • 646
  • 4
  • 11