How to add SQL ELMAH_Error.ErrorID to ELMAH error email subject?

I am using ELMAH to register errors in an SQL database and will send an email. I would like to add ELMAH_Error.ErrorId to the ELMAH email item.

I am storing ErrorId ELMAH in the ElmahLog_Logged event in a session variable, as I found in this question . Atif Aziz himself commented on this blog post about the following information about it:

If you need a crack with a registered error during the distribution, you need to pick it up in the ErrorLogModule.Logged event. This should be, for example, push the identifier of the registered error into the mail. To do this, you remove the Id from the Logged event in the HttpContext and use it later in the Mailing event. For this to work, the modules would be registered so that the Mailing event occurs after the Logged event.

I thought that since the httpModules were added first with ErrorLog and ErrorMail, this will log the Mailing event after the Logged event. I believe that this is about the order of events, not about the order in which modules are used.

How do you register the order of events for HttpModules?

I tried the code below to no avail. The code below prohibits sending email, but the error is still logged in the SQL table.

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> 

... and ...

 <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, of course, and correctly configured (I installed via 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); } 

If I changed the ErrorMail_Mailing event code in the global.asax.cs file to the following code, I receive an email with the object specified in the ErrorMail_Mailing event code, as I should. I even tried to just put Session.SessionID in the subject and kill the email features. This makes me think that the ErrorMail_Mailing event does not have access to the session. Is it correct?

 protected void ErrorMail_Mailing(object sender, ErrorMailEventArgs e) { e.Mail.Subject = "I changed the subject in global.asax"; } 
+4
source share
1 answer

The answer was incredibly easy. The ErrorMailEventsArgs.Error object contains session information at the time of the exception. ErrorMail_Mailing does not even have access to the session context. So the solution is as simple as pulling your variable from ErrorMailEventArgs.

 protected void ErrorMail_Mailing(object sender, ErrorMailEventArgs e) { var elmahErrorId = e.Error.SessionVariables["ElmahId"].ToString(); if (!string.IsNullOrEmpty(elmahErrorId)) e.Mail.Subject = String.Format("{0}, see ELMAH_Error.ErrorID = {1}", e.Mail.Subject, elmahErrorId); } 

Also, by simply placing ElmahId in the session in the ErrorLog_Logged event, if your ErrorMail handler is registered after ErrorLog (made by adding ErrorLog to web.config), this session variable will be in the Session Variables section of the standard ELMAH email error.

 protected void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args) { Session["ElmahId"] = args.Entry.Id; } 

Update:. Atif Aziz said that Errors.SessionVariables is not in the source ELMAH, but in the patch. Here is my package.config:

 <packages> <package id="elmah.corelibrary" version="1.2" /> <package id="elmah" version="1.2.0.1" /> <package id="elmah.sqlserver" version="1.2" /> </packages> 
+5
source

Source: https://habr.com/ru/post/1387497/


All Articles