HttpContext.Current returns null inside ErrorMail_Mailing handler (in Global.asax) for ELMAH
We are using ELMAH
for our application MVC asp.net
.
When any occurs, exception
ELMAH
an error message is sent that has an ErrorMail_Mailing event.I
write a handler for this event inside my Global.asax
and tries to read the value from the variable HttpContext.Current.Items
, but getting it null exception
.
Any work around please help.
I am applying what Scott suggests here:
http://scottonwriting.net/sowblog/archive/2011/01/06/customizing-elmah-s-error-emails.aspx
In global.asax.cs I am trying to write below event handler where I want to customize the subject line of the error message with something stored inside myHttpContext.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;
}
}
source to share
Internally, MVC will depend on where you set the value HttpContext.Current.Items
in terms of whether it will be available in the ErrorEmailEvent. Please refer to the previous SO request - HttpContext.Items with ASP.NET MVC for some guidance on setting this up correctly or using alternative means like creating your own ITempDataProvider .
source to share