码迷,mamicode.com
首页 > 其他好文 > 详细

LogHelper 日志和错误日志

时间:2016-06-27 12:13:22      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

1、这个是记录错误信息的

/// <summary>
        /// 这个是记录错误信息的
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="Method"></param>
        public static void Error(Exception ex, string Method)
        {
            try
            {
                string msg = "Call " + Method + " Err:" + DateTime.Now.ToShortTimeString() + "\t" + ex.Message;
                if (ex.InnerException != null) msg += "InnerException:" + ex.InnerException.Message;
                System.IO.File.AppendAllText(GetFilePath(true), msg + "\r\n");
            }
            catch (Exception)
            {

                //throw;
            }

        }


2、这个是作为简单的日志

/// <summary>
        /// 这个是作为简单的日志
        /// </summary>
        /// <param name="Log"></param>
        public static void Log(string Log)
        {
            try
            {
                System.IO.File.AppendAllText(GetFilePath(false), "Log:" + DateTime.Now.ToShortTimeString() + "\t" + Log + "\r\n");
            }
            catch (Exception)
            {

                //throw;
            }


        }
        private static string GetFilePath(bool isError)
        {
            string path = GetLogPath();
            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
            if (isError)
            {
                path += "\\" + DateTime.Today.ToString("yyyyMMdd") + ".err.txt";
            }
            else
            {
                path += "\\" + DateTime.Today.ToString("yyyyMMdd") + ".log.txt";
            }
            return path;
        }

        private static string GetLogPath()
        {
            string LogPath = System.Configuration.ConfigurationManager.AppSettings["logPath"] + "";
            if (string.IsNullOrEmpty(LogPath)) LogPath = HttpContext.Current.Server.MapPath("~") + "\\log";
            if (!System.IO.Directory.Exists(LogPath)) System.IO.Directory.CreateDirectory(LogPath);
            return LogPath;
        }
    }
}

LogHelper.Log("传进来的参数:" + "姓名" + StaffName + "工号" + Dept + "部门" + StaffNo);
            
LogHelper.Log("URL:" + svcdbWCt.Url);
             
LogHelper.Log("返回的Json参数:" + lstInternalStaffNoTel);

 

LogHelper 日志和错误日志

标签:

原文地址:http://www.cnblogs.com/iDennis/p/5619699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!