473,382 Members | 1,665 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,382 software developers and data experts.

Exception Stack Trace Parameter Values.


Hi all.

If I wanted to write something so that, when an exception was thrown, and
the stack unwound, the stack trace was captured with the values of the
parameters (instead of just the parameter signature for method), is this
possible without exception-wrapping the guts of each method?

I can see how to get parameter values out of MethodInfo, but can't see how
to do it without doing something like adding 7 lines to each method, taking
the following code:

public void myMethod( Class1 object1 )
{
object1.doSomething()
}

and turning it into:

public void myMethod( Class1 object1 )
{
try
{
object1.doSomething()
}
catch (Exception e )
{
throw new CustomException( MethodInfo, e )
}
}

I thought of using attributes, but can't see a way to do it. I also thougth
of using a using block, which is 3 lines instead of 7, but I can't figure
how to do this either.

Is anybody smart out there who knows how to do this in C#? I miss c++
macros, for this particular purpose.

Cheers,

Steve.
Mar 30 '06 #1
2 6461
Hi, sorry it's not possible to get the values at runtime. If it is
possible then I don't know how you do it :-(

If it's any use you can get the stack trace and stack frames from the
System.Diagnostics:
using System.Diagnostics;
using System.Reflection;

try
{
MethodThatThrowsException("test");
}
catch(Exception ex)
{
StackTrace trace = new StackTrace();
Debug.WriteLine("Frame Count:" + trace.FrameCount);
for(int i = 0; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
Debug.WriteLine("Frame " + i + " FileName:" + frame.GetFileName());

MethodBase method = frame.GetMethod();
Debug.WriteLine(" MethodName:" + method.Name);

ParameterInfo[] parameters = method.GetParameters();
Debug.WriteLine("Params: " + parameters.Length);
foreach(ParameterInfo param in parameters)
{
Debug.WriteLine(" Name: " + param.Name);
}

}

Mar 31 '06 #2
Neither reflection nor StackFrame provide features to grab runtime values.
However you can do some custom coding to retrieve these values e.g. I am
using params obj[] to provide logging functionality. Here is the sample code.

Thanks, Arif
////////////////////Sample code/////////////////////////////////
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Sample One
int val = 10;
string name = "Arif";
BusinessFunction1(val,name);
//Sampe Two
int b = 10;
string name2 ="Test";
bool check = true;
BusinessFunction2(b,name2,check);
}
static void BusinessFunction1(int val, string name)
{
LogTrace(val,name);
}
static void BusinessFunction2(int b, string name2, bool
check)
{
LogTrace(b,name2,check);
}
public static void LogTrace(params object[] args)
{
StackTrace callStack = new StackTrace(1, true);
StackFrame callingMethodFrame =
callStack.GetFrame(0);
if (args == null || args.Length == 0)
{
Console.WriteLine("no parameters");
}
else
{
MethodBase callingMethod =
callingMethodFrame.GetMethod();
ParameterInfo[] parameters =
callingMethod.GetParameters();
int inParamCount = parameters.Length;
foreach (ParameterInfo parameter in
parameters)
if (parameter.IsOut) inParamCount--;
Debug.Assert(inParamCount == args.Length,
String.Format("Incorrect
number of arguments. Expected {0} but was {1}.", parameters.Length,
args.Length));
int paramCount = parameters.Length;
int argIndex = 0;
for (int i = 0; i < paramCount; i++)
{

Console.WriteLine(parameters[i].Name);

if (parameters[i].IsOut)

Console.WriteLine("<Unknown>");
else
{
if (args[argIndex] == null)

Console.WriteLine("<null>");
else
{
Console.WriteLine(args[argIndex].ToString().Trim());

}
argIndex++;
}
if (i < paramCount - 1)
Console.WriteLine(",");
}
}
}
}


"Jason Hales" wrote:
Hi, sorry it's not possible to get the values at runtime. If it is
possible then I don't know how you do it :-(

If it's any use you can get the stack trace and stack frames from the
System.Diagnostics:
using System.Diagnostics;
using System.Reflection;

try
{
MethodThatThrowsException("test");
}
catch(Exception ex)
{
StackTrace trace = new StackTrace();
Debug.WriteLine("Frame Count:" + trace.FrameCount);
for(int i = 0; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
Debug.WriteLine("Frame " + i + " FileName:" + frame.GetFileName());

MethodBase method = frame.GetMethod();
Debug.WriteLine(" MethodName:" + method.Name);

ParameterInfo[] parameters = method.GetParameters();
Debug.WriteLine("Params: " + parameters.Length);
foreach(ParameterInfo param in parameters)
{
Debug.WriteLine(" Name: " + param.Name);
}

}

Mar 31 '06 #3

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

Similar topics

7
by: Andy Fish | last post by:
Hi, in my c# code I have something like this: try { ... } catch (Exception ex) { ... throw ex; }
7
by: Markus Dehmann | last post by:
What's the best way to implement an exception stack trace? What I want is sth like printStackTrace() in Java. It could look like this: Car.cpp:381:runtime_exception:Could not find the brake...
0
by: MeDhanush | last post by:
Hi, Can sb pl tell me,when there is some message like this, System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider) +48 System.Convert.ToInt32(Object value) +36...
2
by: Lasse Vågsæther Karlsen | last post by:
If I got the following code: try { // something that might throw an exception } catch (Exception ex) { // Log contents of ex here throw;
1
by: Oleg.Ogurok | last post by:
Hi there, The .pdb files are generally not installed in a production environment. As a result, when an exception occurs, the runtime can't resolve the lines of the code where the problem...
4
by: TS | last post by:
I have an application on one server that shows line numbers in stack trace, but on another server the "in c:\xxx.aspx:line 9999" part that accompanies the "at" part doesn't show. Besides this...
5
by: Mr. SweatyFinger | last post by:
WHY CAN'T THE CLOWN -HOLES WHO WROTE ASP.NET PROVIDE AN ERROR LINE NUMBER??? HONEST TO SH@THOLE PETE Server Error in '/New Folder (7)' Application....
3
by: Dave Anson | last post by:
I have a custom exception and I want to write the information to the event log, including the Stack Trace. I can create the message and write to the event log no problem, but the Stack Trace is...
3
by: Paul McGuire | last post by:
Is there any way to hide portions of an exception stack trace? When users get exceptions when using pyparsing, there are usually many layers of pyparsing-internal stack messages that are not at...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.