zoukankan      html  css  js  c++  java
  • .net自定义错误页面

    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.
    }
  • 相关阅读:
    SQL Server 索引的自动维护 <第十三篇>
    SQL Server 索引的图形界面操作 <第十二篇>
    python处理时间戳
    今天又犯了Java/Scala里面substring的错误
    新浪系统工程师笔试--shell
    把DEDE的在线文本编辑器换成Kindeditor不显示问题
    C语言 EOF是什么?
    Windows Server 2012 R2超级虚拟化之七 远程桌面服务的增强
    C++数据结构之最小生成树
    python sqlite 查询表的字段名 列名
  • 原文地址:https://www.cnblogs.com/dodui/p/1828484.html
Copyright © 2011-2022 走看看