/// <summary> /// 异常处理写日志 /// 修改人rock 20151201,添加返回值返回错误信息编号, /// 修改人rock 20151203.添加BaseException信息输出,因为如果try里面有异常捕捉throw抛出异常的话异常的代码行会定位不准确 /// </summary> /// <param name="e"></param> /// <param name="message">自定义的消息</param> /// <returns>返回错误信息编号(供开发人员定位错误日志信息)</returns> public static string Error(Exception e, string message = null) { CASException ex = new CASException(); ex.Level = 1; ex.Message = e.Message; ex.Source = e.Source; ex.StackTrace = e.StackTrace; ex.Type = e.GetType().ToString(); ex.MethodName = e.TargetSite == null ? "" : e.TargetSite.Name; ex.ClassName = e.TargetSite == null ? "" : e.TargetSite.DeclaringType.FullName; //取session里的值 kevin ex.UserIP = WebCommon.GetIPAddress(); //ex.UserName = SessionHelper.Get(ConstCommon.UserName); //ex.CompanyID = StringHelper.TryGetInt(SessionHelper.Get(ConstCommon.CompanyId)); //ex.CompanyName = SessionHelper.Get(ConstCommon.CompanyName); //错误信息的编码,供开发人员定位错误日志信息 string s = Guid.NewGuid().ToString().Replace("-", ""); s = s.Substring(0, 10 > s.Length ? s.Length : 10); string errorCode = DateTime.Now.ToString("yyyyMMddHHmmssff") + "X" + s; string messageStr = ""; if (!string.IsNullOrEmpty(message)) { messageStr = "\r\nMessage2:" + message; } string errstr = "错误编号:" + errorCode + "\r\n" + "Message:{0}\r\n" + "Type:{1}\r\n" + "Source:{2}\r\n" + "StackTrace:{3}\r\n" + "Method:{4}\r\n" + "Class:{5}\r\n" + "UserIP:{6}\r\n" + "UserName:{7}\r\n" + "CompanyID:{8}\r\n" + "CompanyName:{9}\r\n"; string error = string.Format(errstr , ex.Message + messageStr, ex.Type, ex.Source, ex.StackTrace, ex.MethodName, ex.ClassName , ex.UserIP, ex.UserName, ex.CompanyID, ex.CompanyName); //添加BaseException信息输出,因为如果try里面有异常捕捉throw抛出异常的话异常的代码行会定位不准确 if (e != e.GetBaseException()) { error = error + "BaseException:"; CASLog.Error(error, e.GetBaseException()); } else { CASLog.Error(error); } return(errorCode); }
/// <summary> /// 检查时间合法的字符串参数 /// </summary> /// <returns></returns> public static string TimeString(string posts) { DateTime dt = DateTime.Now; //这里改为数组,原来用indexof会引起相似名称的参数冲突 kevin string[] tmp = null; string tmpstr = posts; if (!posts.StartsWith("http://") && !posts.StartsWith("?") && !posts.StartsWith("&")) { tmpstr = "&" + posts; } tmp = tmpstr.Split(new char[] { '?', '&' }); List <string> tmpkey = new List <string>(); for (int i = 0; i < tmp.Length; i++) { if (tmp[i].IndexOf("=") > 0) { tmpkey.Add(tmp[i].Split('=')[0]); } } if (!tmpkey.Contains <string>("strdate")) { posts += "&strdate=" + HttpUtility.UrlEncode(dt.ToString()); } if (!tmpkey.Contains <string>("strcode")) { posts += "&strcode=" + StringHelper.GetMd5("123" + dt.ToString() + "321"); } //加上当前IP posts += "&sourceip=" + WebCommon.GetIPAddress(); if (posts.StartsWith("http://") && posts.IndexOf("&") > 0 && posts.IndexOf("?") < 0) { posts = posts.Substring(0, posts.IndexOf("&")) + "?" + posts.Substring(posts.IndexOf("&") + 1);; } return(posts); }
/// <summary> /// 登录者信息添加到缓存中 /// </summary> /// <param name="account">账号</param> /// <param name="appId">应用id</param> /// <param name="cookie">是否保存cookie,默认是</param> /// <returns></returns> public string AddLoginUser(string account, string appId, bool cookie = true) { string token = Guid.NewGuid().ToString(); try { Operator operatorInfo = new Operator(); operatorInfo.appId = appId; operatorInfo.account = account; operatorInfo.logTime = DateTime.Now; operatorInfo.iPAddress = WebCommon.GetIPAddress(); operatorInfo.browser = WebCommon.GetClientBrowserVersions(); operatorInfo.token = token; if (cookie) //是否保存cookie { string cookieMark = WebHelper.GetCookie(ReadonlyKey.LoginUserMarkKey).ToString(); if (string.IsNullOrWhiteSpace(cookieMark)) { WebHelper.WriteCookie(ReadonlyKey.LoginUserMarkKey, operatorInfo.account); //写入登录者标识 } WebHelper.WriteCookie(ReadonlyKey.cacheKeyOperator + operatorInfo.account, operatorInfo.token); //adms_operator_XX = token } WebHelper.WriteCookie(ReadonlyKey.LoginUserToken, operatorInfo.token); // 写入登录秘钥 CacheHelper.Set <Operator>(ReadonlyKey.cacheKeyOperator + operatorInfo.account, operatorInfo); //写入缓存信息:当前连接用户 return(operatorInfo.token); #region code fail //if (cookie) //是否保存cookie //{ // string cookieMark = WebHelper.GetCookie(ReadonlyKey.LoginUserMarkKey).ToString(); //标记登录的浏览器 // if (string.IsNullOrWhiteSpace(cookieMark)) // { // //operatorInfo.loginMark = Guid.NewGuid().ToString(); //创建登录者标识 // WebHelper.WriteCookie(ReadonlyKey.LoginUserMarkKey, operatorInfo.browser + "|" + operatorInfo.iPAddress); //写入登录者标识 // } // else // operatorInfo.loginMark = cookieMark;// 登录者标识 = 标记登录的浏览器 // WebHelper.WriteCookie(ReadonlyKey.LoginUserToken, token); // 写入登录秘钥 //} //else // operatorInfo.loginMark = loginMark; // 登录者标识 = 设备标识 //当前用户信息 //Dictionary<string, string> dic = new Dictionary<string, string>(); //dic[ReadonlyKey.cacheKeyToken + account] = "1"; //Dictionary<string, string> dicOld = CacheHelper.Get<Dictionary<string, string>>(ReadonlyKey.cacheKeyToken + account); //if (dicOld != null && dicOld.Count > 0) //{ // CacheHelper.Remove(ReadonlyKey.cacheKeyToken + account); //} //CacheHelper.Set<Dictionary<string, string>>(ReadonlyKey.cacheKeyToken + account, dic); //CacheHelper.Set<Operator>(ReadonlyKey.cacheKeyOperator + operatorInfo.loginMark, operatorInfo); //写入缓存信息:当前连接用户 //return token; #endregion } catch (Exception ex) { return(ex.Message); } }