将异常的 ELMAH 错误序列号(不是 GUID)传递给 ASP.NET 中的自定义错误页面

发布于 2024-10-06 11:38:46 字数 278 浏览 0 评论 0原文

我想在发生错误时向用户显示异常的序列号(来自 ELMAH_Error 表中的序列号列),而不是 GUID。这可能吗?我发现这篇文章 问题传递 ELMAH 日志id 到 ASP.NET 中的自定义错误页面,但它给出了 GUID,我想知道如何访问 ELMAH 组件的 Logged 事件中的序列号?提前致谢。

I want to show the sequence number for the exception (from sequence number column in ELMAH_Error table) , not the GUID to the user whenever an error occurs. is this possible? I found this post Problem passing ELMAH log id to Custom Error page in ASP.NET, but it gives the GUID, I would like to know how I can access the sequence number in Logged event of ELMAH component? thanks in advance.

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

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

发布评论

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

评论(2

悲念泪 2024-10-13 11:38:46

一些背景...

ELMAH 旨在将错误记录到指定的日志源。最常见的日志源是 SqlErrorLog 类,它将错误记录到 SQL Server 数据库,并且是核心 ELMAH 库的一部分。但是还有其他日志源可以将错误记录到电子邮件地址、XML 文件、Oracle 数据库等。

您引用的序列号是 SqlErrorLog 类特有的。如果您查看其他一些日志源(例如 XmlFileErrorLog),您会发现没有序列号的概念。

鉴于此,ELMAH 的 Error 类(表示错误)没有 Sequence 属性也就不足为奇了。 “序列”是特定于 SqlErrorLog 日志源的内容。 (如果您检查用于为 SqlErrorLog 日志源创建数据库对象的 T-SQL 脚本,您可以进一步看到这一点 - Sequence 字段是一个 IDENTITY< /code> 列,因此在插入时由数据库自动计算。它不是 ELMAH 提供的值。)

一个可能的解决方案...

MichaelvR 建议修改 ELMAH 的源代码,但我建议不要这样做,因为这样做你会将您的 ELMAH 分支绑定到特定的日志源。相反,也许一个不太优雅(而且更简单!)的解决方案是:

  1. 按照您的描述在 Global.asax 中创建 ErrorLog_Logged 事件处理程序,
  2. 读取错误的 Id刚刚引发的错误的值(GUID),
  3. 连接数据库并直接查询ELMAH_Error表。

以下是一个示例查询:

SELECT Sequence
FROM ELMAH_Error
WHERE ErrorId = TheErrorIdOfTheJustLoggedError

您对此解决方法有何看法?

快乐编程!

Some Background...

ELMAH is designed to log errors to a specified log source. The most common log source is the SqlErrorLog class, which logs errors to a SQL Server database and is part of the core ELMAH library. But there are other log sources available that log errors to an email address, to an XML file, to an Oracle database, and so on.

The sequence number to which you refer is something specific to the SqlErrorLog class. If you look at some of the other log sources, like the XmlFileErrorLog, you'll see there's no concept of a sequence number.

Given this, it should come as no surprise that ELMAH's Error class - which is what represents an error - has no Sequence property. The "sequence" is something specific to the SqlErrorLog log source. (You can further see this if you examine the T-SQL script used to create the database objects for the SqlErrorLog log source - the Sequence field is an IDENTITY column and therefore is automatically computed by the database on insert. It is not a value provided by ELMAH.)

A Possible Solution...

MichaelvR suggested modifying ELMAH's source code, but I'd suggest against that since by doing so you'd be tying your branch of ELMAH to a particular log source. Instead, perhaps a less elegant (and simpler!) solution is in order:

  1. Create the ErrorLog_Logged event handler in Global.asax as you described,
  2. Read the error's Id value (the GUID) of the just-raised error,
  3. Connect to the database and query the ELMAH_Error table directly.

Here is a sample query:

SELECT Sequence
FROM ELMAH_Error
WHERE ErrorId = TheErrorIdOfTheJustLoggedError

What do you think of this workaround?

Happy Programming!

土豪 2024-10-13 11:38:46

只是想到这里,我自己还没有尝试过。

您必须对 elmah 的源代码进行一些更改并编译您自己的小版本的 Elmah。如果您将 Elmah 配置为在其他地方记录日志,我不确定是否使用序列列。这些是我认为您需要采取的步骤,可能特定于 SQL Server 日志记录:

  1. 向序列值的 Elmah.ErrorLogEntry 类型添加一个属性
  2. 更改 ELMAH_LogError 存储过程,以便它返回插入到序列列中的值。序列列被定义为标识列,因此 SQL Server 在插入时自动生成该列的值。
  3. 更改 Elmah.SqlErrorLog.GetError 方法中的代码,以便它获取存储过程返回的序列值并将其放入您添加到 ErrorLogEntry 类型的属性中。

Just thinking out of the box here, haven't tried it myself.

You will have to make some changes to the source of elmah and compile your own little version of Elmah. I'm not sure if the sequence column is used if you configure Elmah to log somewhere else. These are the steps I think you need to take, probably specific to SQL server logging:

  1. Add a property to the Elmah.ErrorLogEntry type for the sequence value
  2. Change ELMAH_LogError stored procedure so that it returns the value that was inserted into the sequence column. The sequence column is defined as an Identity column, so SQL server auto generates the value for this column upon insertion.
  3. Change the code in the Elmah.SqlErrorLog.GetError method so that it takes the sequence value returned by stored procedure and puts it into the property you added to the ErrorLogEntry type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文