473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StrackTrace logging - Incorrect Line number.

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);
}

Aug 14 '07 #1
5 4469
IdleBrain wrote:
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.

--
-glenn-
Aug 14 '07 #2
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

Aug 14 '07 #3
IdleBrain wrote:
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.

--
-glenn-
Aug 14 '07 #4
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?



Aug 14 '07 #5
IdleBrain wrote:
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.

--
-glenn-
Aug 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pete Jereb | last post by:
Ok, I've managed to get the logging package to work, sort of. I'm writing a text parser, where data entry clerks can edit large, unwieldy and unforgiving textfiles that are VERY SENSITIVE as to...
0
by: Marek Augustyn | last post by:
Hello, I'm getting strange exception raised using module logging. It's strange because it happens only sometimes, and it happens in different places (but always in a logging method). i.e. this...
0
by: Tero Saarni | last post by:
I'm using Logging in my library module for producing trace log of certain events. For being more useful these log entries should be associated with the filename and line number of *users* code...
0
by: Neil Benn | last post by:
Hello, I'm running a test and having issues with logging, if I call logging.shutdown() and then want to start the logging going again then I get a problem as if I call shutdown, I can't get the...
1
by: nicholas.farrell | last post by:
Hi everyone. I've just been trying to add a bit more granularity to my logging code, as the jump from INFO (level 20) to DEBUG (level 10) is a bit too big. I was thinking of borrowing a few...
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
12
by: Tekkaman | last post by:
I'm getting a strange behaviour from the "pathname" and "lineno" formatter mapping keys. Instead of my file and my line number I get: /usr/lib/python2.4/logging/__init__.py as the file, and...
3
by: nicholas.petrella | last post by:
I am currently trying to use the python logging system as a core enterprise level logging solution for our development and production environments. The rotating file handler seems to be what I...
6
by: Larry Bates | last post by:
Every time I look at the logging module (up until now) I've given up and continue to use my home-grown logger that I've been using for years. I'm not giving up this time ;-) I find that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.