asp.net 2.0 中設置全局出錯發郵件及寫入系統日志

 1<%@ Application Language="C#" %>
 2<%@ Import Namespace="System.Diagnostics" %>
 3<%@ Import Namespace="System.Web.Mail" %>
 4
 5<script runat="server">
 6
 7    void Application_Start(object sender, EventArgs e) 
 8    {
 9        // 在应用程序启动时运行的代码
10
11    }

12    
13    void Application_End(object sender, EventArgs e) 
14    {
15        //  在应用程序关闭时运行的代码
16
17    }

18
19    protected void Application_Error(object sender, EventArgs e) 
20    
21        // 在出现未处理的错误时运行的代码
22        string strPageUrl = Request.Path;
23        string struserIP = System.Web.HttpContext.Current.Request.UserHostAddress;
24        Exception strErrorInfo = Server.GetLastError();
25        string strMessage = "Url:" + strPageUrl + "</br>";
26        strMessage = strMessage + "Time:" + DateTime.Now.ToString() + "</br>";
27        strMessage = strMessage + "UserIP:" + struserIP + "</br>";
28        strMessage = strMessage + " Error: ";
29        strMessage = strMessage + strErrorInfo.ToString() + "</br>";
30
31        MailMessage myMail = new MailMessage();
32        myMail.From = "CSS";
33        myMail.To = "sfwu@cclmotors.com";
34        myMail.Subject = "CSS Error";
35        myMail.BodyFormat = MailFormat.Html;
36        myMail.Body = strMessage;
37        myMail.BodyEncoding = Encoding.UTF8;
38        SmtpMail.SmtpServer = "sjexchange";
39        SmtpMail.Send(myMail);
40
41        string LogName = "CSS";
42        if ((!(EventLog.SourceExists(LogName))))
43        {
44            EventLog.CreateEventSource(LogName, LogName);
45        }

46        EventLog Log = new EventLog();
47        Log.Source = LogName;
48        Log.WriteEntry(strMessage, EventLogEntryType.Error);
49    }

50
51    void Session_Start(object sender, EventArgs e) 
52    {
53        // 在新会话启动时运行的代码
54
55    }

56
57    void Session_End(object sender, EventArgs e) 
58    {
59        // 在会话结束时运行的代码。 
60        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
61        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
62        // 或 SQLServer,则不会引发该事件。
63
64    }

65       
66</script>
67
原文地址:https://www.cnblogs.com/cnaspnet/p/515138.html