使用helper/extension方法调试关于方法的输出,它的名称和操作时间



我正在创建一种"某种"扩展方法,它将帮助我调试未来的代码

有一个列为的Listview"LV_MyAppLog"

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

只是时间部分我无法决定哪一种是最简单但又不太专业的实施方式。

这是我已经构建的代码:

        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")  ; 
        }

我想加上一个给定方法发生的时间,

我应该用DateTime now来做吗?比如:

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)

或者我应该使用一个我根本不熟悉的秒表(:但我很乐意通过我的这个代码示例来学习它,前提是它是更好的方法。

我可以在AddToAppLog()方法中使用正确的实现来请求正确的语法吗?

日期时间。现在ToString()可以,但我也会记录毫秒数。

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

如果要测量时间范围执行范围,请使用秒表。

相反,在您的情况下,您似乎只需要有关执行发生时间的信息。

免费填写以使用最合适格式的DateTime.Now

最新更新