By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,165 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,165 IT Pros & Developers. It's quick & easy.

Exception Stack Trace Parameter Values.

P: n/a

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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.