当前位置: 首页 > 工具软件 > ELMAH > 使用案例 >

Elmah .net 最方便最好用的日志,异常记录插件

丰胤运
2023-12-01

首先需要下载 Elmah.dll 并引用到项目中.


然后在web.config中配置启用 Elmah

配置项如下.

web.config

<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
    </sectionGroup>
  </configSections> 
  <system.web> 
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
  </system.web>
  <system.webServer> 
    <handlers> 
      <add name="elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> 
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    </modules> 
  </system.webServer>
  <elmah>
    <security allowRemoteAccess="false" /> <!-- true 可以远程访问啦 -->
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/Elmah.Errors" />
  </elmah>
  
</configuration>

为了方便你使用,我已将与elmah无关的给删除掉了.

在代码中你就可以像下面这样用了.

 try
                {
                    string WeiXinDatabaseConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["WeiXinDatabaseConnectionString"].ToString();
                    DataClassesDataContext dbContext = new DataClassesDataContext(WeiXinDatabaseConnectionString);
                    dbContext.WeiXiu.InsertOnSubmit(viewmodel);
                    dbContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Elmah.Error error = new Elmah.Error(ex, System.Web.HttpContext.Current);
                    var errorid = Elmah.XmlFileErrorLog.GetDefault(System.Web.HttpContext.Current).Log(error);

                    ViewBag.Message = "亲,对不起,系统崩溃啦.错误号:" + errorid;
                    return View(viewmodel);
                }


当然最好的写法是


Error error = new Error(e, context);
ErrorLog errorLog =  GetDefault(context); //适配各种类型的错误日志
error.ApplicationName = errorLog.ApplicationName;
string id = errorLog.Log(error);
entry = new ErrorLogEntry(errorLog, id, error);




当客户向你反馈了错误号以后,你可以这样看.

单个错误的地址: http://localhost/elmah.axd/detail?id=4170d014-ea2c-405d-947e-d32af2d13110

id=后面跟上错误号...就是哪个错误.

平常没事的时候可以自己上去看看所有的错误

地址: http://localhost/elmah.axd 


 类似资料: