需要記錄日志的地方包括:進入方法的時候,傳參的時候,統計執行時間,方法返回參數的時候,退出語句塊的時候,出現異常的時候,等等。先來體驗不使用Micirosoft Unity進行日志記錄。
class PRogram{static void Main(string[] args){Add(1, 2);Console.ReadKey();}private static int Add(int a, int b){int result = 0;string temp = string.Empty;string returnValue = string.Empty;try{//記錄進入方法Console.WriteLine("馬上要執行方法了");temp = string.Format("輸入的參數為:a={0},b={1}", a, b);Console.WriteLine(temp);//統計方法執行時間Stopwatch watch = new Stopwatch();watch.Start();result = a + b;watch.Stop();Console.WriteLine("程序執行時間為{0}", watch.Elapsed);//記錄返回值returnValue = string.Format("返回結果是:{0}", result);Console.WriteLine(returnValue);//記錄方法執行接收Console.WriteLine("方法執行結束");}catch (Exception ex){//記錄異常Console.WriteLine(string.Format("異常信息是:{0},輸入參數是:{1}", ex.ToString(), temp));throw;}finally{//記錄異常處理
新聞熱點
疑難解答