• 又一款日志组件:ELMAH日志处理异常


    背景

    ELMAH就是一个日志的拦截和处理组件,说到.net的日志组件,大家的第一反应该是Log4Net、NLog等这些东西,关于Log4Net和NLog,可以说是.net日志组件里面使用最为广泛的组件了,它们功能强大、使用方便。

    优点

    相比它们:

    1、ELMAH的使用更加简单,它甚至不用写一句代码;

    2、ELMAH是一种“可拔插式”的组件,即在一个运行的项目里面我们可以随意轻松加入日志功能,或者移除日志功能;

    3、ELMAH组件自带界面,不用写任何代码,即可查看异常日志的界面;

    4、组件提供了一个用于集中记录和通知错误日志的机制,通过邮件的机制通知错误信息给相关人员。

    代码实现

    1、nuget安装 using Elmah;

     2、Application_Error 异常404处理

      protected void Application_Error(object sender, EventArgs e)
            {
                if (BQoolCommon.Helpers.Setting.CommonSetting.IsProd())
                {
                    if (e is ExceptionFilterEventArgs exceptionFilter)
                    {
                        if (exceptionFilter.Exception is HttpException httpException && httpException.Message.StartsWith(_exceptionMsg))
                        {
                            Response.Redirect("/");
                        }
                    }
                    Response.Clear();
                    Server.ClearError();
                    Response.StatusCode = 404;
                }
            }

    3、排除 Elmah 404 寄信通知

      public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
            {
                if (e.Exception is HttpException httpException && (httpException.GetHttpCode() == 404 || httpException.Message.StartsWith(_exceptionMsg)))
                {
                    e.Dismiss();
                }
            }

    4、自定 Elmah 发信主旨

     void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
            {
                string machineName = "none server";
                try
                {
                    if (Request != null)
                    {
                        machineName = Request.ServerVariables["HTTP_HOST"];
                    }
                }
                catch
                {
                }
    
                // 取得 Elamh ErrorMail 的主旨
                // "$MachineName$ at $ErrorTime$ : {0}"
                string elmahSubject = e.Mail.Subject;
                //替換 ErrorMail 的主旨內容
                string emailSubject = string.Format("BigCRM.Web Error => {0}",
                    elmahSubject
                        .Replace("$MachineName$", machineName)
                );
    
                e.Mail.Subject = emailSubject;
            }

    5、web.config配置

     <elmah>
        <!--
            See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
            more information on remote access and securing ELMAH.
        -->
        <security allowRemoteAccess="false"/>
      </elmah>
      <location path="elmah.axd" inheritInChildApplications="false">
        <system.web>
          <httpHandlers>
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
          </httpHandlers>
          <!-- 
            See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
            more information on using ASP.NET authorization securing ELMAH.
    
          <authorization>
            <allow roles="admin" />
            <deny users="*" />  
          </authorization>
          -->
        </system.web>
        <system.webServer>
          <handlers>
            <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode"/>
          </handlers>
        </system.webServer>
      </location>

    运行效果

     

    总结

    ELMAH对于中小项目来说不失为一种不错的选择;

  • 相关阅读:
    string
    auto和decltype
    const限定符
    &(引用) 和 *(指针)
    extern关键字
    关于将函数写入头文件问题(分离式编译)
    poj2154(polya定理+欧拉函数)
    bzoj2115(线性基)
    51nod1832(二叉树/高精度模板+dfs)
    51nod1464(trie + dfs)
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/14270638.html
Copyright © 2020-2023  润新知