国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net中當服務器出錯時顯示指定的錯誤頁面,同時把錯誤信息寫入系統日志文件的探討

2024-07-10 12:58:37
字體:
來源:轉載
供稿:網友
asp.net中當服務器出錯時顯示指定的錯誤頁面同時把錯誤信息寫入系統日志文件的探討

一,在web.config中填寫出錯時顯示的頁面,可以根據不同的statuscode顯示不同的出錯頁面。
   <customerrors mode="on"  //如果設置為off則出錯只返回錯誤信息,不會跳到自己的指定頁面defaultredirect="/error/customerrorpage.aspx">
    <error statuscode="404" redirect="/error/404page.aspx"/>
    <error statuscode="403" redirect="/error/403page.aspx"/>
  </customerrors>

二,在global.asax文件中添加應用出錯代碼,寫入系統日志文件
protected void application_error(object sender, eventargs e)
        {
            exception lasterror = server.getlasterror();
            string errmessage = lasterror.tostring();

            string logname  = "mylog";
            string message = "url " + request.path + " error: " + errmessage;

            // create event log if it doesn't exist
        
            if (!eventlog.sourceexists(logname))
            {
                eventlog.createeventsource(logname, logname);
            }
            eventlog log = new eventlog();
            log.source  = logname;
            //these are the five options that will display a different icon.
            log.writeentry(message, eventlogentrytype.information, 1);
            log.writeentry(message, eventlogentrytype.error, 2);
            log.writeentry(message, eventlogentrytype.warning, 3);
            log.writeentry(message, eventlogentrytype.successaudit, 4);
            log.writeentry(message, eventlogentrytype.failureaudit, 5);

        }
三,現在你可以進行測試了。
我在default.aspx.cs中產生一個錯誤,果然跳到默認的錯誤頁面!
private void page_load(object sender, system.eventargs e)
        {
            // put user code to initialize the page here
            try
            {
                int y=0;
                int x=1/y;
            }
            catch (exception err)
            {
                throw new exception("404");//我想產生不同的錯誤,對應web.config中的statuscode,該如何實現?
                //err.
            }




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东兴市| 锦屏县| 盐池县| 巨野县| 山阳县| 乌审旗| 珲春市| 灌南县| 南川市| 新田县| 广元市| 虞城县| 巩义市| 咸宁市| 西昌市| 庆元县| 临夏市| 白朗县| 古田县| 澄城县| 东山县| 宝兴县| 宣化县| 闽侯县| 江永县| 涡阳县| 宜阳县| 县级市| 漳州市| 夏河县| 南靖县| 波密县| 砚山县| 奈曼旗| 邛崃市| 济南市| 万源市| 延安市| 万源市| 民权县| 黑龙江省|