Debugging with helper / extension Method Regarding output of method its name and time of operation

对着背影说爱祢 提交于 2020-01-06 19:32:46

问题


I am creating a "kind of" Extension method that will help me debug my future codes

there's a Listview "LV_MyAppLog" with columns

record # , MethodName, Method's OutPut, Time.

only that the time part i was unable to decide which is the simplest but non the less The professional way to implement.

this is the code i already built :

        public int LogCounter = 1;
        public void AHLog_AddRecord(string FunctionName = "N/A", string FunctionOutPut = "N/A")
        {

            ListViewItem MyItem= new ListViewItem();
            MyItem.Text = LogCounter.ToString();
            Lview_AutomationLog.Items.Insert(0, MyItem);
            MyItem.SubItems.Add(FunctionName);
            MyItem.SubItems.Add(FunctionOutPut);
            //place for the time SubItem(column)
            LogCounter++;

        }


        public void AddToAppLog()
        {
            StackTrace st = new StackTrace(); 
            StackFrame sf = st.GetFrame(1); 
if(Completed)
            AHLog_AddRecord(sf.GetMethod().ToString().Replace("Void", ""), "Success")  ; 
else 
            AHLog_AddRecord(sf.GetMethod().ToString().Replace("Void", ""), "Fail")  ; 

        }

i wanted to add the time when a given method took place,

Am i supposed to do it with DateTime now Like:

DateTime now = DateTime.Now;
string theTime = now.ToString();// if so , What is the format to use to get only time h:m:s 
AHLog_AddRecord(sf.GetMethod().ToString().Replace("Void", ""), "Fail" , theTime)

or should i use a stop watch which i am not familiar with at all (: but i will be happy to learn it through this code example of mine , only if it's the better approach.

can i ask for the right syntax using the correct implementation within my AddToAppLog() method?


回答1:


The Datetime.Now.ToString() is ok, but I'd log the milliseconds too.

.ToString("hh:mm:ss.fff")



回答2:


Use Stopwatch, if you want to measure a range of time, a range of execution.

In your case, instead, seems that you need just information about a time when execution happens.

Fill free to use a DateTime.Now in most suitable format.



来源:https://stackoverflow.com/questions/12155605/debugging-with-helper-extension-method-regarding-output-of-method-its-name-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!