StrackTrace logging - Incorrect Line number.

I

IdleBrain

I am trying to log the Application name, Method name, line number and
column number whenever an exception is generated in an C# 2005
application using the following code.

Problem is that the line number that is being obtained by
stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage
function's line number. I am trying to write the line number of the
statement which is causing an exception and not the LogMessage
function's line number. I donot understand why this is happening.

I appreciate your help..Thanks.

try
{
//Code goes here.
}
catch (Exception ex)
{
AnotherForm.LogMessage(ex.Message);
}


Another Form:
public static void LogMessage(String strMessage)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = new StackFrame(1, true); //
stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();

//Log the message into Event Logs.
EventLogger.WriteEntry(frmMain.APPLICATION_NAME + " :: "
+ methodBase.Name + " Line: " +
stackFrame.GetFileLineNumber()
+ " Col: " + stackFrame.GetFileColumnNumber() + " Msg:
" + strMessage, EventLogEntryType.Error);
}
 
G

GlennDoten

IdleBrain said:
I am trying to log the Application name, Method name, line number and
column number whenever an exception is generated in an C# 2005
application using the following code.

Problem is that the line number that is being obtained by
stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage
function's line number. I am trying to write the line number of the
statement which is causing an exception and not the LogMessage
function's line number. I donot understand why this is happening.

I appreciate your help..Thanks.

try
{
//Code goes here.
}
catch (Exception ex)
{
AnotherForm.LogMessage(ex.Message);
}


Another Form:
public static void LogMessage(String strMessage)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = new StackFrame(1, true); //
stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();

//Log the message into Event Logs.
EventLogger.WriteEntry(frmMain.APPLICATION_NAME + " :: "
+ methodBase.Name + " Line: " +
stackFrame.GetFileLineNumber()
+ " Col: " + stackFrame.GetFileColumnNumber() + " Msg:
" + strMessage, EventLogEntryType.Error);
}

You have to walk back up the call stack until you get to a frame that is
not for "LogMessage". That will give you the information you are looking
for.
 
I

IdleBrain

By walking back do you mean select a different stackframe??

Instead of:
StackFrame stackFrame = new StackFrame(1, true);

I have already tried using StackFrame(0,true); or StackFrame(2,true);
-- They for some unknown reason give me a line number of 0.

Any other thoughts?

Thanks
 
G

GlennDoten

IdleBrain said:
By walking back do you mean select a different stackframe??

Instead of:
StackFrame stackFrame = new StackFrame(1, true);

I have already tried using StackFrame(0,true); or StackFrame(2,true);
-- They for some unknown reason give me a line number of 0.

Yes, just keep walking back up the stack frame until you get to one that
is not "LogMessage".

Oh, and unless you've done a debug build, there won't be any line
numbers in the stack trace. Which is probably why you are seeing the zeros.
 
I

IdleBrain

Thanks for your reply Glenn.

Sorry for my ignorance, but by walking back up the stack frame, do you
mean selecting a different stack frame, which I already did?? I looked
at other frames and unfortunately none of the frames point out to the
line that generated the exception.

The build is currently set to debug... And are you saying that setting
the build to release wouldn't give any line numbers? My whole
intention of trying to log the line numbers is to get a better
understanding of which line in the code generated an exception. Am I
missing anything?

Is there a better way of tracking exceptions other than by logging
method name and stack trace?
 
G

GlennDoten

IdleBrain said:
Thanks for your reply Glenn.

Sorry for my ignorance, but by walking back up the stack frame, do you
mean selecting a different stack frame, which I already did?? I looked
at other frames and unfortunately none of the frames point out to the
line that generated the exception.

The build is currently set to debug... And are you saying that setting
the build to release wouldn't give any line numbers? My whole
intention of trying to log the line numbers is to get a better
understanding of which line in the code generated an exception. Am I
missing anything?

Is there a better way of tracking exceptions other than by logging
method name and stack trace?

Here's some code in a logger module I wrote years ago:

static MethodBase GetCaller()
{
// Get our own, current namespace name.
string namespaceName = new StackFrame(0).GetMethod()
.DeclaringType.ToString();

// We’ll use this to walk the stack looking for a
// method name that is not the same as our method
// name—that is, the name of the method that called
// this method name.
StackTrace trace = new StackTrace(true);

// Look for the first occurence of a stack frame that
// contains a namespace name that is different from our
// own.
string callerNamespace = null;
int i;
MethodBase callerFrame = null;
for (i=0; i<trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
callerFrame = frame.GetMethod();
if (callerFrame.DeclaringType.ToString() !=
namespaceName)
{
callerNamespace = callerFrame.DeclaringType
.ToString();
//callerName = callerFrame.Name;
break;
}
}

return callerFrame;
}

In this implementation, GetCaller returns the first method on the stack
that has a namespace different than the one that GetCaller is in (the
idea being that GetCaller lives in a utility library with a namespace
that is different than your application code's namespace). This should
get you on the right track.

You're not missing anything with the line numbers. Well, you'll be
missing them in a release build. That's just how it works. The stack
trace can't give you line numbers if they aren't there, and they are
only there when you do a debug build. But a stack trace of the error is
sure better than nothing.

Instead of trying to recreate the wheel here, you could just use
something like log4net for your logging and just send the stack trace to
it. There's probably not much point in you just trying to find the line
number yourself in the case of a debug build; just send the entire stack
trace output to your logging mechanism.

HTH.
 

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

Top