출처 : http://windstop.tistory.com/22


 
에러 처리할 때 어느 함수에서 에러가 났는지 로그가 남으면 알기 편할 것 같아서 로거를 만들다가...
현재 실행중인 함수 이름 가져오기



using System.Reflection;

 

 

private void button4_Click(object sender, EventArgs e)

{

    try

    {

        int[] a = {1,2,3,4};

 

        for (int i = 0; i < 10; i++)

        {

         int x =    a[i];

        }

    }

    catch (Exception x)

    {

        string str = string.Format("{0}\n\n{1}\n\n{2}\n\n{3}\n\n{4}\n\n{5}",

            MethodBase.GetCurrentMethod().Name

            , x.StackTrace

            , x.TargetSite

            , x.Data

            , x.InnerException

            , x.Source

            );

        MessageBox.Show(str);

    }

}

  






이런식으로 사용하면 된다.
더 자세한 함수 호출관계를 알려면 StackTrace, StackFrame, MethodBase 클래스들을 잘 섞어쓰면 된다. 

 



Posted by motolies
,