自定义 Elmah YSOD 数据

发布于 2024-08-23 16:29:17 字数 121 浏览 3 评论 0原文

我正在将 Elmah 与 ASP.NET 结合使用,并想知道如何将自定义数据(例如会话变量)添加到未处理的异常电子邮件中。

我已经在 Global.asax 文件中尝试了几种处理程序,但似乎找不到合适的处理程序。

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 Global.asax file but can't seem to find the right one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-08-30 16:29:17

为此,我认为您需要修改 Elmah 和重新编译。实现它应该不会太难。如果您查看 Elmah.Error 类的构造函数,则会传入 HttpContext ,您应该能够从中获取所需的信息,例如 Session 、表单变量等。您可以将自定义字段添加到此数据的 Elmah.Error 类中,

我认为 Elmah.ErrorMailHtmlFormatter 类是使用 构造电子邮件的地方>HtmlTextWriter,在这里您可以在 RenderSummary() 方法中插入代码,以包含您添加到 Elmah.Error 的自定义字段。

我知道开始使用源代码可能会很痛苦,但我个人认为这是最干净的方法,因为目前没有报告/电子邮件模板的工具,并且最好在生成输出后添加一些东西来更改输出。

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.

冬天旳寂寞 2024-08-30 16:29:17

安德鲁的回答很有帮助,谢谢。我最终执行了以下操作:

  1. 将 OnBuilding 事件添加到 ErrorMail http 模块。此事件的事件参数具有 NameValueCollection 属性。
  2. 我在 global.asax 中处理了 OnBuilding 事件。
  3. 由于 HttpModule 并不总是能够访问会话状态,尤其是。如果在加载会话之前发生异常,我会将想要报告的数据复制到 HttpApplication 缓存中(由 sessionid 索引)。
  4. 当发生异常时,我通过存储在请求中的 sessionid(特别是在 cookie 中)从应用程序缓存中获取我想要的数据。我根据这些数据生成一个 NameValueCollection,并通过 OnBuilding args 将其发送回 httpmodule。
  5. 然后,数据将呈现到电子邮件,类似于呈现服务器变量部分的方式。

Andrew's answer helped a lot, thanks. I ended up doing the following:

  1. Added a OnBuilding event to the ErrorMail http module. The event args for this event have a NameValueCollection property.
  2. I handled the OnBuilding event in global.asax.
  3. 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).
  4. 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.
  5. The data is then rendered to email similarly to how the server variables section is rendered.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文