On Application Crash, list all local variables and values, including complete call stack,...

K

Kerem Gümrükcü

Hi,

the topic says it all: How can i list all current member local variables and
their values on a exception inside a applications member/function? Can
i use the DBGHELP.DLL from microsoft or does the .NET Runtime has any
capabillities to do so. I am still familliar with the StackTrace-Class, but
i need
more information, like from a debugger. I need to know all data that was
involved into that crash, including all local member data at the
member/function
level that throw the exception. Can this be done within the fault
application by
catching the exception and doing a backtrack or do i have to write my own
debugger for that? Once i did in the past in C with dbghelp library and SYM*
stuff, but that was really pain! Can this easily done within C# with .NET
available
stuff?

Regards

Kerem
 
M

Morten Wennevik [C# MVP]

Kerem Gümrükcü said:
Hi,

the topic says it all: How can i list all current member local variables and
their values on a exception inside a applications member/function? Can
i use the DBGHELP.DLL from microsoft or does the .NET Runtime has any
capabillities to do so. I am still familliar with the StackTrace-Class, but
i need
more information, like from a debugger. I need to know all data that was
involved into that crash, including all local member data at the
member/function
level that throw the exception. Can this be done within the fault
application by
catching the exception and doing a backtrack or do i have to write my own
debugger for that? Once i did in the past in C with dbghelp library and SYM*
stuff, but that was really pain! Can this easily done within C# with .NET
available
stuff?

Regards

Kerem

Hi Kerem,

I don't think you can. You can get the method causing the exception from
Exception.TargetSite but the state of the class containing this method or the
parameter values used calling this method is not available. If you catch the
exception at the place where it is thrown you can create a common
LogException class and feed it the parameter values and/or current instance.
The LogException class would then be able to create a state log of the
instance/method. You would, however, need to catch exceptions everywhere in
the code and it wouldn't help if you can't catch it where it gets thrown.

Something along the lines of:

LogException(ex, this, param1, param2);

....
private void LogException(Exception ex, object instance, params object[] data)
{
Console.WriteLine(ex.Message + Environment.NewLine);

StackFrame frame = new StackFrame(1);
MethodBase method = frame.GetMethod();

Console.WriteLine(LogMethod(method, data) + Environment.NewLine);
Console.WriteLine(LogObject(instance));
}

private string LogMethod(MethodBase method, object[] data)
{
ParameterInfo[] parameters = method.GetParameters();

StringBuilder methodString = new StringBuilder();
methodString.Append(method.Name + "(");
for (int i = 0; i < parameters.Length; i++)
{
methodString.Append(String.Format("{0} {1}",
parameters.ParameterType.Name, parameters.Name));
if (i < data.Length - 1)
methodString.Append(", ");
}
methodString.AppendLine(")");

methodString.Append(method.Name + "(");
for (int i = 0; i < parameters.Length; i++)
{
methodString.Append(i < data.Length ? data.ToString() : "");
if (i < data.Length - 1)
methodString.Append(", ");
}
methodString.AppendLine(")");

return methodString.ToString();
}

private static string LogObject(object obj)
{
if (obj == null)
return "";
PropertyInfo[] properties = obj.GetType().GetProperties();

StringBuilder sb = new StringBuilder();
foreach (PropertyInfo property in properties)
{
try
{
sb.AppendLine(property.Name + " (" + property.PropertyType.Name
+ "): " + property.GetValue(obj, null));
}
catch (Exception ex)
{
sb.AppendLine(property.Name + " (" + property.PropertyType.Name
+ "): " + ex.Message);
}
}

return sb.ToString();
}
 
K

Kerem Gümrükcü

Hi Morten,

thanks for your effort, but this is exactly what i do
in my code the last two days, but i would like to
have a view like the one in the VStudio Debugging
View. I guess i have to write my own Debugger. I am
pretty sure i can use the dbghelp.dll in my code to
backtrack the exception, but this would be pretty much
of work, pinvoking all the stuff. Once i wrote a Debugger
in C for one of my Applications, but it was really a lot of
code i had to write,...

Thanks anyway,....

Regards

Kerem

--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Morten Wennevik said:
Kerem Gümrükcü said:
Hi,

the topic says it all: How can i list all current member local variables
and
their values on a exception inside a applications member/function? Can
i use the DBGHELP.DLL from microsoft or does the .NET Runtime has any
capabillities to do so. I am still familliar with the StackTrace-Class,
but
i need
more information, like from a debugger. I need to know all data that was
involved into that crash, including all local member data at the
member/function
level that throw the exception. Can this be done within the fault
application by
catching the exception and doing a backtrack or do i have to write my own
debugger for that? Once i did in the past in C with dbghelp library and
SYM*
stuff, but that was really pain! Can this easily done within C# with .NET
available
stuff?

Regards

Kerem

Hi Kerem,

I don't think you can. You can get the method causing the exception from
Exception.TargetSite but the state of the class containing this method or
the
parameter values used calling this method is not available. If you catch
the
exception at the place where it is thrown you can create a common
LogException class and feed it the parameter values and/or current
instance.
The LogException class would then be able to create a state log of the
instance/method. You would, however, need to catch exceptions everywhere
in
the code and it wouldn't help if you can't catch it where it gets thrown.

Something along the lines of:

LogException(ex, this, param1, param2);

...
private void LogException(Exception ex, object instance, params object[]
data)
{
Console.WriteLine(ex.Message + Environment.NewLine);

StackFrame frame = new StackFrame(1);
MethodBase method = frame.GetMethod();

Console.WriteLine(LogMethod(method, data) + Environment.NewLine);
Console.WriteLine(LogObject(instance));
}

private string LogMethod(MethodBase method, object[] data)
{
ParameterInfo[] parameters = method.GetParameters();

StringBuilder methodString = new StringBuilder();
methodString.Append(method.Name + "(");
for (int i = 0; i < parameters.Length; i++)
{
methodString.Append(String.Format("{0} {1}",
parameters.ParameterType.Name, parameters.Name));
if (i < data.Length - 1)
methodString.Append(", ");
}
methodString.AppendLine(")");

methodString.Append(method.Name + "(");
for (int i = 0; i < parameters.Length; i++)
{
methodString.Append(i < data.Length ? data.ToString() : "");
if (i < data.Length - 1)
methodString.Append(", ");
}
methodString.AppendLine(")");

return methodString.ToString();
}

private static string LogObject(object obj)
{
if (obj == null)
return "";
PropertyInfo[] properties = obj.GetType().GetProperties();

StringBuilder sb = new StringBuilder();
foreach (PropertyInfo property in properties)
{
try
{
sb.AppendLine(property.Name + " (" + property.PropertyType.Name
+ "): " + property.GetValue(obj, null));
}
catch (Exception ex)
{
sb.AppendLine(property.Name + " (" + property.PropertyType.Name
+ "): " + ex.Message);
}
}

return sb.ToString();
}
 

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