I'm using Elmah with ASP.NET and wondering how I would add custom data, such as a session variable, to an unhandled exception email.
I've tried several handlers in the 开发者_C百科Global.asax file but can't seem to find the right one.
For this, I'd think you would need to modify the Elmah source and recompile. It shouldn't be too difficult to achieve. If you have a look in the constructor of the Elmah.Error
class, the HttpContext
is passed in, from which you should be able to get the info you need, e.g. Session, Form variables etc. You could add custom fields to the Elmah.Error
class for this data
I think the Elmah.ErrorMailHtmlFormatter
class is where the email is constructed using a HtmlTextWriter
, and here you could insert code in the RenderSummary()
method to include the custom fields you added to Elmah.Error
.
I know it may be a pain to start working with source, but personally I think it's the cleanest way as there currently is no facility for report/email templates, and it's better that bolting on something to change the output after it has been generated.
Andrew's answer helped a lot, thanks. I ended up doing the following:
- Added a OnBuilding event to the ErrorMail http module. The event args for this event have a NameValueCollection property.
- I handled the OnBuilding event in global.asax.
- Since HttpModules don't always have access to sessionstate, esp. if the exception occurs before the session is loaded, I copied the data i wanted reported into the HttpApplication cache(indexed by sessionid).
- When an exception occurs I grab the data i want out of the application cache via the sessionid stored in the request(specifically, in the cookie). I generate a NameValueCollection from this data and send it back to the httpmodule via the OnBuilding args.
- The data is then rendered to email similarly to how the server variables section is rendered.
精彩评论