Wizard
Wizard
CC#
Created by Wizard on 6/19/2023 in #help
❔ Output error when user missing .NET runtime
I have an AvaloniaUI project targetting .NET Framework 4.6.2 AND .NET6. It builds two EXEs: one for NET6 and one for NET462. Works great, except there's an annoyance I'd like to fix: When the user opens my program and they're missing the required NET runtime for that specific EXE, my program will crash to desktop. No errors, warnings, popups, nothing. Happens before any of my stuff is initialized so I can't even log to a file. I realize this is a user issue but I'd like to at least be able to tell my user the problem. Does anyone by chance know a solution?
18 replies
CC#
Created by Wizard on 6/19/2023 in #help
❔ Get locals and their values from an Exception object.
Is it possible to get the local variables and their values from a StackTrace object created from a System.Exception object? I have the method public void LogException(ex). I'm trying to log all variables names and values. I'm not using compiler optimizations if it matters. I tried using reflection and a few other crazy things but nothing seems to work properly. Current code:
public static void LogException( Exception ex )
{
Log( $"Exception: {ex.GetType().Name} - {ex.Message}" );
Log( $"Stack trace: {ex.StackTrace}" );

StackTrace stackTrace = new StackTrace( ex, true );
for ( int i = 0; i < stackTrace.FrameCount; i++ )
{
StackFrame frame = stackTrace.GetFrame( i );
MethodBase method = frame.GetMethod();
Type declaringType = method.DeclaringType;

Log( $"Local variables at frame {i}:" );

MethodInfo methodInfo = method as MethodInfo;
if ( methodInfo != null )
{
LocalVariableInfo[] localVariables = methodInfo.GetMethodBody()?.LocalVariables?.ToArray();
if ( localVariables != null )
{
foreach ( LocalVariableInfo localVariable in localVariables )
{
object value = frame.GetMethod().GetMethodBody()?.LocalVariables[localVariable.LocalIndex];
Log( $"{localVariable.LocalType} {localVariable} = {value}" );
}
}
}

Log( $"Method: {declaringType?.FullName}.{method.Name}" );
Log( $"File: {frame.GetFileName()}" );
Log( $"Line: {frame.GetFileLineNumber()}" );
}

ExceptionLogged.Invoke( ex ); // Raise the ExceptionLogged event
}
public static void LogException( Exception ex )
{
Log( $"Exception: {ex.GetType().Name} - {ex.Message}" );
Log( $"Stack trace: {ex.StackTrace}" );

StackTrace stackTrace = new StackTrace( ex, true );
for ( int i = 0; i < stackTrace.FrameCount; i++ )
{
StackFrame frame = stackTrace.GetFrame( i );
MethodBase method = frame.GetMethod();
Type declaringType = method.DeclaringType;

Log( $"Local variables at frame {i}:" );

MethodInfo methodInfo = method as MethodInfo;
if ( methodInfo != null )
{
LocalVariableInfo[] localVariables = methodInfo.GetMethodBody()?.LocalVariables?.ToArray();
if ( localVariables != null )
{
foreach ( LocalVariableInfo localVariable in localVariables )
{
object value = frame.GetMethod().GetMethodBody()?.LocalVariables[localVariable.LocalIndex];
Log( $"{localVariable.LocalType} {localVariable} = {value}" );
}
}
}

Log( $"Method: {declaringType?.FullName}.{method.Name}" );
Log( $"File: {frame.GetFileName()}" );
Log( $"Line: {frame.GetFileLineNumber()}" );
}

ExceptionLogged.Invoke( ex ); // Raise the ExceptionLogged event
}
35 replies