我们正在将 ELMAH
用于我们的 MVC asp.net
应用程序。
当发生任何异常
时,ELMAH
会发送包含事件ErrorMail_Mailing的错误邮件,
我正在 Global.asax
中编写此事件的处理程序
并尝试从 HttpContext.Current.Items
变量读取值,但收到 null 异常
。
任何解决办法请帮忙。
我正在应用斯科特在这里建议的内容:-
http://scottonwriting.net/sowblog/archive/2011/01/06/customizing-elmah-s-error-emails.aspx
在 global.asax.cs 中,我尝试编写下面的事件处理程序,我想在其中使用存储在我的 HttpContext.Current.Items
protected void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
{
if (HttpContext.Current.Items["CustomerName"] != null)
{
e.Mail.Subject = "Error came to / " + HttpContext.Current.Items["CustomerName"] + " / Error Type: " +
e.Error.Type;
}
}
最佳答案
在 MVC 中,这取决于您设置 HttpContext.Current.Items
的位置。值是否在 ErrorEmailEvent 中可用。请引用之前的SO问题 - HttpContext.Items with ASP.NET MVC有关正确设置此值或使用替代方法(例如 creating your own ITempDataProvider)的一些指导.
关于asp.net-mvc - HttpContext.Current 在 ELMAH 的 ErrorMail_Mailing 处理程序(在 Global.asax 中)内返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751326/