【发布时间】:2011-12-22 02:59:18
【问题描述】:
我正在使用 ELMAH 将错误记录到 SQL 数据库并发送电子邮件。我想将 ELMAH_Error.ErrorId 附加到 ELMAH 电子邮件的主题中。
我正在将 ElmahLog_Logged 事件中提供的 ErrorId ELMAH 保存到会话变量中,正如我在 this question 中发现的那样。 Atif Aziz 本人在this blog post 上评论了与此相关的以下信息:
如果确实想破解邮寄期间记录的错误,可以 必须在 ErrorLogModule.Logged 事件中选择它。这可能是 方便,例如,将记录的错误的 ID 推送到邮件中。 为此,您需要将 Logged 事件中的 Id 隐藏到 HttpContext 并稍后在 Mailing 事件中使用它。为此, 模块将被注册,以便 Mailing 事件 在 Logged 事件之后发生。
我认为由于 httpModules 首先添加了 ErrorLog,然后添加了 ErrorMail,这将实现在 Logged 事件之后注册 Mailing 事件。我想这是在讨论事件的顺序,而不是模块的应用顺序。
如何注册HttpModules的事件顺序?
我试过下面的代码无济于事。下面的代码会阻止电子邮件发送,但错误仍会记录到 SQL 表中。
Web.Config:
<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>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
</system.web>
...和...
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<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>
<handlers>
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
当然,and 已正确配置(我通过 NuGet 安装)。
Global.asax.cs:
protected void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
Session["ElmahId"] = args.Entry.Id;
}
protected void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
var elmahErrorId = Session["ElmahId"].ToString();
e.Mail.Subject = String.Format("{0}, see ELMAH_Error.ErrorID = {1}", e.Mail.Subject, elmahErrorId);
}
如果我将 global.asax.cs 中的 ErrorMail_Mailing 事件代码更改为以下代码,我将收到带有 ErrorMail_Mailing 事件代码中指定主题的电子邮件。我什至尝试将 Session.SessionID 放在主题中,这会破坏电子邮件功能。 这让我觉得 ErrorMail_Mailing 事件没有会话访问权限。这是正确的吗?
protected void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
e.Mail.Subject = "I changed the subject in global.asax";
}
【问题讨论】:
标签: c# asp.net event-handling httpmodule elmah