How to get error line number of code using try-catch

前端 未结 10 939
轮回少年
轮回少年 2020-12-05 13:08

I want to get line number of code which cause error. For example;

static void Main(string[] args)
{
    using (SqlConnection conn = new SqlConnection(bagcum)         


        
10条回答
  •  粉色の甜心
    2020-12-05 13:58

    the following code exception log handler method is works fine :

    in catch :

     catch (Exception ex)
                {
                    CommonTools.vAddToLog(ex, EmpID, ErrorCodes.UnDefined);
                    Response.Redirect("~/ErrorPage.aspx");
                }
    

    in AddToLog method :

     string _exMsgErr = string.Empty;
                    var frame = oStackTrace.FrameCount > 1 ? oStackTrace.GetFrame(1) : oStackTrace.GetFrame(0);
                    if (oException.GetType() == typeof(JOVALException))
                    {
                        JOVALException _JOVALEx = (JOVALException)oException;
                        _exMsgErr = _JOVALEx.Message;
                    }
                    else
                    {
                        _exMsgErr = oException.Message;
                    }
                    ErrorLog oError = new ErrorLog(frame.GetMethod().Name, (string)frame.GetFileName(), (int)frame.GetFileLineNumber(), sCustomErrorMessage == string.Empty ? _exMsgErr : sCustomErrorMessage, sUserID, oErrCode);
                    //Cont. your code of log file
    

    Finally the xml log file looks like this :

    
    FillRolesDDLs
    
    F:\Projects\ERP\ERP\Pages\SystemSettings\Roles.aspx.cs
    
    61
    
    The given DataRow is not in the current DataRowCollection.
    
    1
    UnDefined
    
    
    

提交回复
热议问题