简体   繁体   English

是否正在收集有关ASP.NET中错误的有用信息?

[英]Gathering useful information about an error in ASP.NET?

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 via 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? 你怎么看?

你可以试试艾玛

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. 有第三方库可以帮助解决此问题 ,其中elmah是一个库, Logging Application Block是另一个。

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. 写入Windows错误日志要好得多。 Then maybe have a service that watches the log and emails occasional reports, or something along those lines. 然后,也许有一项服务可以监视日志并通过电子邮件偶尔发送报告,或者类似的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM