我如何获得表单名称和行号。在catch块中。 [英] How can i get form name and line no. in catch block.

查看:57
本文介绍了我如何获得表单名称和行号。在catch块中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我使用下面提到的代码来获取表单名称和行号。在应用程序中发生任何异常时在catch块中:



我的代码在本地计算机上正常工作但在托管服务器时它无法正常工作。有人可以帮我解决这个问题。





非常感谢。



我尝试过:



Dear all,

I am using code which is mentioned below to get form name and line no. in catch block when any exception occurs in application:

My code is working properly in local machine but when hosted on server its not working. Can anybody help me to solve this issue in my application.


Thanks a lot.

What I have tried:

private static String[] GetExceptionDetail(Exception e)
{

    string[] strArr = new string[2] { "", "" };
    try
    {

        //Line No.  
        strArr[0] = Convert.ToString(e.StackTrace.Substring(e.StackTrace.LastIndexOf('')));

        //Form Name
        strArr[1] = Convert.ToString(e.StackTrace.Substring(e.StackTrace.LastIndexOf('\\') + 1, e.StackTrace.LastIndexOf(':') - (e.StackTrace.LastIndexOf('\\') + 1)));
    }
    catch
    {
        //Stack trace is not available!
    }
    return strArr;
}

推荐答案

在服务器上记录整个异常并检查您的解析是否按预期工作。为什么你的异常处理程序(原始catch)只是将this.Name放入strArr [0]?它会省你一些解析。为什么没有整个堆栈跟踪而不是行号?



无论如何,你可以使用StackTrace类

StackTrace Class(System.Diagnostics)

这将适用于你将.pdb文件与您的解决方案文件一起使用。



Log the whole exception on the server and check that your parsing works as you expect it to. Why doesn't your exception handler (the original catch) just put this.Name into strArr[0]? It would save you some parsing. And why not have whole stack trace instead of line number?

Anyhow, there is StackTrace class that you can use
StackTrace Class (System.Diagnostics)
This will work if you have .pdb files along with your solution files.

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}


这篇关于我如何获得表单名称和行号。在catch块中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆