zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    AntSword 中国蚁剑的下载安装配置(附下载文件)
    开园第一笔
    四舍五入小技巧
    PAT B# 1025 反转链表
    WebService如何根据对方提供的xml生成对象
    解决Web部署 svg/woff/woff2字体 404错误
    解决TryUpdateModel对象为空的问题
    IIS集成模式下,URL重写后获取不到Session值
    SQLServer清空数据库中所有的表并且ID自动归0
    win2003 64位系统IIS6.0 32位与64位间切换
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/515138.html
Copyright © 2011-2022 走看看