问题
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.
回答1:
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:
- Create the
ErrorLog_Logged
event handler inGlobal.asax
as you described, - Read the error's
Id
value (the GUID) of the just-raised error, - 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!
回答2:
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:
- Add a property to the Elmah.ErrorLogEntry type for the sequence value
- 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.
- 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.
来源:https://stackoverflow.com/questions/4397994/pass-the-elmah-error-sequence-number-not-guid-for-the-exception-to-custom-erro