需要记录日志的地方包括:进入方法的时候,传参的时候,统计执行时间,方法返回参数的时候,退出语句块的时候,出现异常的时候,等等。先来体验不使用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{//记录异常处理
新闻热点
疑难解答