0

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?

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

2 Answers2

1

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

.ToString("hh:mm:ss.fff")
laszlokiss88
  • 4,001
  • 3
  • 20
  • 26
0

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123