• 测试程序的时候用到写参数或者错误日志的几个方法,用来方便发现错误


    /// <summary>
        /// 写接口日志类(by mo)
        /// </summary>
        public class LogWrite
        {
            /// <summary>
            /// 写异常日志
            /// </summary>
            /// <param name="path">相对路径("~/log")</param>
            /// <param name="content">异常内容</param>
            /// <param name="filename">文件名(不能带后缀格式,默认保存txt文本)</param>
            public static void WriteBug(string path, string content, string filename)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("----------bug日志--------
    ");
                sb.AppendFormat("时间:{0}
    异常内容:{1}
    
    ", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content);
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(path)))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
                }
                System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath(path + filename + ".txt"), sb.ToString(), System.Text.Encoding.Default);
            }
            /// <summary>
            /// 写请求参数日志(用于接口调用)
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="title">标题</param>
            /// <param name="content">内容</param>
            /// <param name="filename">文件名(不能带后缀格式,默认保存txt文本)</param>
            public static void WriteRequstParameters(string path, string title, string content, string filename)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("----------{0}--------
    ", title);
                sb.AppendFormat("请求时间={0},{1}
    
    ", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), content);
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(path)))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
                }
                System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath(path + filename + ".txt"), sb.ToString(), System.Text.Encoding.Default);
            }
            /// <summary>
            /// 写所有请求参数日志
            /// </summary>
            /// <param name="path">相对路径</param>
            /// <param name="title">标题</param>
            /// <param name="filename">文件名(不能带后缀格式,默认保存txt文本)</param>
            public static void WriteRequstStream(string path, string title, string filename)
            {
                string a = string.Empty;
                using (System.IO.Stream stream = System.Web.HttpContext.Current.Request.InputStream)
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
                    {
                        a = sr.ReadToEnd();
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("----------{0}--------
    ", title);
                sb.AppendFormat("请求时间:{0},
    请求参数:{1}
    
    ", DateTime.Now.ToString("yyyy-MM-dd HH:mm"), a);
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(path)))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
                }
                System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath(path + filename + ".txt"), sb.ToString(), System.Text.Encoding.Default);
            }
        }
  • 相关阅读:
    webpack学习之——模块(Modules)
    dns-prefetch对网站速度能提升有多少?详解dns-prefetch。
    类数组对象汇总
    HTML input type=file文件选择表单的汇总(二)
    238. 除自身以外数组的乘积
    1029.两地调度
    滑动窗口:无重复字符的最长子串
    统计网格中的矩形以及正方形
    关于正负数的二进制新发现以及求法
    基础练习: 矩阵乘法
  • 原文地址:https://www.cnblogs.com/sanjuantianshu/p/4923613.html
Copyright © 2020-2023  润新知