Global exception grabber

1

I have implemented a global trap of exceptions in my applications.

In program I have put these 2 sentences:

     Application.ThreadException +=
            new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_ThreadException);
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

     static void Application_ThreadException(object sender, UnhandledExceptionEventArgs e)
    {
        ExceptionUtility eu = new ExceptionUtility();
        eu.LogException((Exception)e.ExceptionObject, "");
        FormLogViewer log = new FormLogViewer();
        log.lwFlag = true;
        log.Show();
    }

    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        ExceptionUtility eu = new ExceptionUtility();
        eu.LogException(e.Exception, "");
        FormLogViewer log = new FormLogViewer();
        log.lwFlag = true;
        log.Show();
    }

    public void LogException(Exception e, string source)
    {
        error err = new error();
        err.amxFecha = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        err.amxUsuario = datosPublicos.awNomUsuario;
        err.nmxUsuario = datosPublicos.nwNumeroUsuario;
        if (e.InnerException != null)
        {
            err.amxType = e.InnerException.GetType().ToString();
            err.amxMessage = e.InnerException.Message;
            err.amxSource = e.InnerException.Source;
            if (e.InnerException.StackTrace != null)
            {
                err.mmxTrace = e.InnerException.StackTrace;
            }
            else
            {
                err.mmxTrace = " ";
            }
        }
        else
        { 
            err.amxType = " ";
            err.amxMessage = " ";
            err.amxSource = " ";
            err.mmxTrace = " ";
        }
        err.amxType1 = e.GetType().ToString();
        err.amxMessage1 = e.Message;
        if (source == "")
        {
            err.amxSource1 = " ";
        }
        else
        {
            err.amxSource1 = source;
        }
        if (e.StackTrace != null)
        {
            err.mmxTrace1 = e.StackTrace;
        }
        else
        {
            err.mmxTrace1 = " ";
        }
        if (err.mmxTrace != " ")
        {
            err.amxUbicacion = extraigoLineaError(err.mmxTrace);
        }
        else
        {
            err.amxUbicacion = extraigoLineaError(err.mmxTrace1);
        }
    }

    public string extraigoLineaError(string awTrace)
    {
        string awRetorno = "";
        string[] aLineas1 = awTrace.Split(new Char[] { '\' });
        for (int i = 0; i < aLineas1.Length; i++)
        {
            if (aLineas1[i].Contains("línea"))
            {
                awRetorno = aLineas1[i].Substring(aLineas1[i].LastIndexOf("\") + 1);
                string[] aLineas2 = awRetorno.Split(new Char[] { ' ' });
                awRetorno = aLineas2[0] + " " + aLineas2[1];
                break;
            }
        }
        return awRetorno;
    }

     public static void muestroError(Exception ex)
    {
        ExceptionUtility eu = new ExceptionUtility();
        eu.LogException(ex, "");
        FormLogViewer log = new FormLogViewer();
        log.lwFlag = true;
        log.Show();
    }

In ExceptionUtility what I do is record all the error information to be able to visualize it, everything is registered to facilitate finding and correcting the error.

If somewhere in the program I capture errors with try ... catch instead of doing a messagebox, what I do is execute the method MuestroError ().

All this together with other measures allowed me that my programs at the moment of being for sale do not need maintenance.

Formerly in Clipper I also captured a picture of the screen and the last key pressed by the user.

Today you should also store where you clicked with the mouse in case this was the last action before the error.

I have made several attempts to capture the photo but without success.

Questions:

A) Is it possible to capture the photo of the active window, the last key pressed or the last mouse click (on which component was it made)?

B) How would this be done?

C) Is it possible to improve what I am doing, perhaps by adding some other line to capture exceptions?

    
asked by Hugo Mariño 24.06.2016 в 03:12
source

0 answers