jeudi 23 mai 2019

How can I print all local and global variables in case of an unhandled exception?

As a part of logging system for a vb.net application I am developing, in case of an unhandled exception (MyApplication_UnhandledException call in the Application Events) I want to dump the names and values of every global variable in every class, and the local variables of the function/sub that caused the exception, to a local .log file, which the clients could send back to me for debugging. I know this must be done through reflection, but I cannot figure out how to do it. I need this because the Stack Trace (which I also dump to the log file) doesn't store the values of function parameters, nor the values of global variables.

This is the part I have for global variables:

    Dim assbly As Assembly = [Assembly].GetExecutingAssembly()
    Dim types As Type() = assbly.GetTypes()

    For Each t As Type In types
        For Each p As FieldInfo In t.GetFields()
            Logger.addToLog(p.Name & "  - " & p.FieldType.Name) 'should also get value, maybe with p.getValue() ?
        Next
    Next

Unfortunately, it only prints the Public global variables of each class, while I need Private ones too. Also, this doesn't print the values. The p.getValue() call requires an object as a parameter, but I can't seem to understand what exactly should I give to it. As for the local variables, I haven't been able to come up with anything yet, and I hope someone here might know. I also get the feeling that there must be some better way to achieve what I want. Can someone advise, please?





Aucun commentaire:

Enregistrer un commentaire