LoneXcoder
LoneXcoder

Reputation: 2163

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

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?

Upvotes: 0

Views: 363

Answers (2)

Tigran
Tigran

Reputation: 62256

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.

Upvotes: 0

laszlokiss88
laszlokiss88

Reputation: 4071

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

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

Upvotes: 1

Related Questions