public class WebRequestErrorEventMvc : WebRequestErrorEvent
{
public WebRequestErrorEventMvc(string message, object eventSource, int eventCode, Exception exception) : base(message, eventSource, eventCode, exception)
{
}
public WebRequestErrorEventMvc(string message, object eventSource, int eventCode, int eventDetailCode, Exception exception) : base(message, eventSource, eventCode, eventDetailCode, exception)
{
}
}
public class MyHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
int MvcEventCode = WebEventCodes.WebExtendedBase + 1;
WebRequestErrorEventMvc webRequestErrorEventMvc = new WebRequestErrorEventMvc("An unhandled exception has occurred.", this,
MvcEventCode, filterContext.Exception);
webRequestErrorEventMvc.Raise();
string body ="Error is occured at the request :"+ webRequestErrorEventMvc.RequestInformation.RequestUrl;
SendMail("spencergong","yaya1234","spencergong@gmail.com","spencergong@yahoo.com",
"Email from MyHandlerError ",
body);
}
bool SendMail(string userName,string passWord, string from, string to, string subject, string body)
{
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new NetworkCredential(userName, passWord );
smtp.EnableSsl = true;
MailMessage message = new MailMessage(new MailAddress ( from ),
new MailAddress( to));
message.Body = body ;
message.Subject =subject;
try
{
smtp.Send (message);
return true;
}
catch (Exception ex )
{
return false;
}
}
}
设置web.config配置文件