开发者

Gathering useful information about an error in ASP.NET?

开发者 https://www.devze.com 2022-12-14 13:52 出处:网络
I am examining our legacy system, and there is a method that sends an e-mail when there is an exception.

I am examining our legacy system, and there is a method that sends an e-mail when there is an exception.

It looks like this:

        string strError = "Error in: " + Request.Path + 
            "\nUrl: " + Request.RawUrl + "\n\n";

        // Get the exception object for the last error message that occured.
        Exception ErrorInfo = Server.GetLastError().GetBaseException();
        strError += "Error Message: " + ErrorInfo.Message + 
            "\nError Source: " + ErrorInfo.Source + 
            "\nError Target Site: " + ErrorInfo.TargetSite + 
            "\n\nQueryString Data:\n-----------------\n";

        // Gathering QueryString information
        for (int i = 0; i < Context.Request.QueryString.Count; i++) 
            strError += Context.Request.QueryString.Keys[i] + ":\t\t" + Context.Request.QueryString[i] + "\n";
        strError += "\nPost Data:\n----------\n";

        // Gathering Post Data information
        for (int i = 0; i < Context.Request.Form.Count; i++) 
            strError += Context.Request.Form.Keys[i] + ":\t\t" + Context.Request.Form[i] + "\n";
        strError += "\n";

        if (User.Identity.IsAuthenticated) strError += "User:\t\t" + User.Identity.Name + "\n\n";

        strError += "Exception Stack Trace:\n----------------------\n" + Server.GetLastError().StackTrace + 
            "\n\nServer Variables:\n-----------------\n";

        // Gathering Server Variables information
        for (int i = 0; i < Context.Request.ServerVariables.Count; i++) 
            strError += Context.Request.ServerVariables.Keys[i] + ":\t\t" + Context.Request.ServerVariables[i] + "\n";
        strError += "\n";

        // Sending error message to administration vi开发者_Python百科a e-mail
    SmtpClient smtp = new SmtpClient();
    MailMessage email = new MailMessage();
    email.From = new MailAddress("noreply@mysite.com");
    email.To.Add("me@mysite.com");
    email.Subject = "Site error notification";
    email.Body = strError;

    smtp.Send(email);

This may or may not be hideous, some of the code in our system is beyond shocking, but I have never attempted to investigate the best way to report errors. What do you think?


you can try elmah


Looks like what many websites do - this is not unusual.

Some will log to the filesystem or to the event log (sometimes to many different sources).

It really depends on the volume of errors, who is looking at them and how much information is needed.

There are third party libraries that help with this, elmah being one, the Logging Application Block is another.


The problem with using email to report errors is that email itself isn't very reliable.

Much better to write to the Windows error log. Then maybe have a service that watches the log and emails occasional reports, or something along those lines.

0

精彩评论

暂无评论...
验证码 换一张
取 消