Error Logging

G

Guest

I am trying to implement database error logging in my asp.net application.
When I inspect the exception variable, I see that there is a property called
'procedure' which I believe is the procedure or fucntion that caused the
error. How can I get at that variable to write it to my error log?

Thanks,
Denise
 
G

Guest

Brock,

Thanks for your reply. But perhaps this is not the correct link. I don't
see any mention of referencing the 'procedure' property of an exception.

Denise
 
G

Gordon Smith

AFAIK, the 'Procedure' property is available specifically for exceptions
that are of type SqlException, and it tells you the name of the SQL Server
stored procedure that threw an exception (perhaps other exception types have
a 'Procedure' as well - but not "otherwise uncategorized" exceptions).

Here is a snippet I have in my exception logging routine that writes out the
information. It tells me the name of the specific SQL Server stored
procedure that choked (this is the 'Procedure' you are asking about) and the
specific line number etc. very helpful to have this info:

if (exIncoming is SqlException) {
SqlException e = (SqlException) exIncoming;

//Exception Type
MyWriter.WriteElementString(@"ExceptionType", "SqlException");

// Exception Details
for (int i=0; i < e.Errors.Count; i++) {
MyWriter.WriteStartElement(@"ExceptionDetails");
MyWriter.WriteElementString(@"Message",
HttpUtility.HtmlEncode(e.Errors.Message));
MyWriter.WriteElementString(@"LineNumber",
HttpUtility.HtmlEncode(e.Errors.LineNumber.ToString()));
MyWriter.WriteElementString(@"Procedure",
HttpUtility.HtmlEncode(e.Errors.Procedure));
MyWriter.WriteElementString(@"SQLErrorNumber",
HttpUtility.HtmlEncode(e.Errors.Number.ToString()));
MyWriter.WriteElementString(@"SQLServerName",
HttpUtility.HtmlEncode(e.Errors.Server));
MyWriter.WriteElementString(@"Provider",
HttpUtility.HtmlEncode(e.Errors.Source));
MyWriter.WriteElementString(@"SQLStateValue",
HttpUtility.HtmlEncode(e.Errors.State.ToString()));
MyWriter.WriteEndElement(); // End Exception Details
}
}

HTH !
 
B

Brock Allen

Hmm, well there is no Procedure property that I can see. Perhaps you're looking
for:

ex.InnerException.TargetSite.Name

The one thing to note about exceptions in ASP.NET is that there are two categories
in essence. There are the ones your code caused and then there are other
ones that are raised by ASP.NET due to configuration errors and compiler
errors and things like that. When you receive the notification in the Application_Error
event in global.asax you usually check for which category the exception is
in. If it's in the first category then the type of exception thrown is the
HttpUnhandledException. If it's the second category then it's some other
excetionon (ConfgurationException for example). So you tend to write your
code like this:

void Application_Error(Object sender, EventArgs e)
{
Exception ex = Context.Error;
if (ex is HttpUnhandledException)
{
ex = Context.Error.InnerException;
}

// now log ex
}

So if you want the name of the method where the exception was raised, go
look for ex.TargetSite.Name. The other two good pieces of info are Source
which is the assembly and StackTarce which is usually crucial when tracking
down the error. If you just call Exception.ToString() you get all of that
info in a nice neat package. That's what the link I posted was trying to
show :S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top