LogHelper 日志和错误日志

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);

 

posted @ 2016-06-27 11:36  iDennis  阅读(736)  评论(0编辑  收藏  举报