首页 > 学院 > 开发设计 > 正文

使用Microsoft Unity进行日志记录

2019-11-17 02:54:32
字体:
来源:转载
供稿:网友

使用Microsoft Unity进行日志记录

需要记录日志的地方包括:进入方法的时候,传参的时候,统计执行时间,方法返回参数的时候,退出语句块的时候,出现异常的时候,等等。先来体验不使用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
            {
                //记录异常处理
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表