-2

I'd like to simply get notified for site errors, and I've this straight forward code that does work everywhere in the application except in global.asax!!

void Application_Error(object sender, EventArgs e) 
    {
        UIHelper.SendErrorMessage(Server.GetLastError());
    }

code in another project:

public static void SendErrorMessage(Exception ex)
        {
            MailMessage errorMessage = new MailMessage();
            errorMessage.From = new MailAddress("xxxxxx@gmail.com", "Site Errors");
            errorMessage.Subject = "Error in sending overseas user";
            errorMessage.CC.Add(new MailAddress("xxxxxxx@gmail.com", "xxxxx")); //test only
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Source:{0}\n", ex.Source);
            sb.AppendFormat("Message:{0}\n", ex.Message);
            sb.AppendFormat("Stack Trace:{0}\n", ex.StackTrace);
            sb.AppendFormat("Inner Exception:{0}\n", ex.InnerException == null ? string.Empty : ex.InnerException.Message);
            errorMessage.Body = sb.ToString();
            errorMessage.IsBodyHtml = false;

            var smtp = new SmtpClient();

            try
            {
                smtp.Send(errorMessage);
            }
            catch
            {
            }
        }

Update: I've wrote logic to write the exception to a file in a folder in my website, and I tried that logic in my dev box and it worked, but it never works in the Host!! this is drives me crazy! Email logic works in my box, file logging works in my box but none of them works in production?! this is really strange... any thoughts?

nolimit
  • 814
  • 10
  • 19
  • 3
    How does it not work? Does it throw an exception? If so, what exception? Also, empty `catch` clauses are considered a worst practice. Don't catch an exception unless you can actually do something about it. – Daniel Mann Feb 17 '12 at 03:34
  • Is it throws exception or something? – x2. Feb 17 '12 at 03:35
  • What is the Exception being thrown, it could be anything from a network or firewall issue to permissions. For a start you need to provide the correct Credentials to the SmtpClient... http://stackoverflow.com/questions/298363/how-can-i-make-smtp-authenticated-in-c-sharp – Lloyd Feb 17 '12 at 03:42
  • forgot to mention that it works on my dev box, but it is not working from godaddy hosting. cannot do really much unless I write to a file. and see what is the exception. – nolimit Feb 17 '12 at 20:23
  • You have lots of options. Elmah can store the exceptions in memory, to a database or even email them to you. – NotMe Feb 17 '12 at 22:56
  • Lloyd, you can declare your network credentials from web.config once so you won't have to authenticate everytime you send an email. – nolimit Feb 18 '12 at 01:25

1 Answers1

2

Use Elmah.

It is really a complete solution.

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245