用户错误警报

时间:2013-01-15 14:07:45

标签: c# asp.net

我正在使用VS2010(使用C#)。我有一个在线申请。我有2个错误表单设置,如果用户遇到错误,则会将其重定向到。我有一个global.asax.cs文件,我在Application_Error事件中设置了代码,向我发送电子邮件,让我知道用户收到了错误。我的问题是,每次表单加载时我都会收到一封电子邮件。它应该只在发生错误时。以下是我在global.asax.cs文件中的代码:

protected void Application_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Server.GetLastError() != null)
    {
        Exception myException = HttpContext.Current.Server.GetLastError().GetBaseException();

        // Create the Outlook application by using inline initialization.
        Outlook.Application oApp = new Outlook.Application();

        Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));

        email.Recipients.Add(fake@fakeemail.com);

        email.Subject ="Error in COF page " + Request.Url.ToString();

        string message = string.Empty;

        message +="<b>Message</b><br />" + myException.Message + "<br />";

        message +="<b>StackTrace</b><br />" + myException.StackTrace + "<br />";

        message +="<b>Query String</b><br />" + Request.QueryString.ToString() + "<br />";

        email.Body = message;

        email.Send();

         //Clear out any lingering errors after they have been reported. 
        Server.ClearError();
    }
}

0 个答案:

没有答案