In a .net Exception how to get a stacktrace with argument values
I don't think System.Diagnostics.StackFrame supplies argument information (other than the method signature).
You could instrument the troublesome calls with trace logging via AOP, or even use its exception interception features to conditionally log without having to litter your code. Have a look around http://www.postsharp.org/.
Likewise, I've not found anything to derive the parameters automatically at runtime. Instead, I've used a Visual Studio add-in to generate code that explicitly packages up the parameters, like this:
public class ExceptionHandler
{
public static bool HandleException(Exception ex, IList<Param> parameters)
{
/*
* Log the exception
*
* Return true to rethrow the original exception,
* else false
*/
}
}
public class Param
{
public string Name { get; set; }
public object Value { get; set; }
}
public class MyClass
{
public void RenderSomeText(int lineNumber, string text, RenderingContext context)
{
try
{
/*
* Do some work
*/
throw new ApplicationException("Something bad happened");
}
catch (Exception ex)
{
if (ExceptionHandler.HandleException(
ex,
new List<Param>
{
new Param { Name = "lineNumber", Value=lineNumber },
new Param { Name = "text", Value=text },
new Param { Name = "context", Value=context}
}))
{
throw;
}
}
}
}
EDIT: or alternatively, by making the parameter to HandleException a params array:
public static bool HandleException(Exception ex, params Param[] parameters)
{
...
}
...
if (ExceptionHandler.HandleException(
ex,
new Param { Name = "lineNumber", Value=lineNumber },
new Param { Name = "text", Value=text },
new Param { Name = "context", Value=context}
))
{
throw;
}
...
It's a bit of a pain generating the extra code to explicitly pass the parameters to the exception handler, but with the use of an add-in you can at least automate it.
A custom attribute can be used to annotate any parameters that you don't want the add-in to pass to the exception handler:
public UserToken RegisterUser( string userId, [NoLog] string password )
{
}
2ND EDIT:
Mind you, I'd completely forgotten about AVICode:
http://www.avicode.com/
They use call interception techniques to provide exactly this kind of information, so it must be possible.