public override void OnActionExecuted(ActionExecutedContext filterContext) { var hc = filterContext.HttpContext; //是Ajax请求 if (hc.Request.IsAjaxRequest()) { string controller = filterContext.RouteData.Values["controller"].ToString().ToLower(); string action = filterContext.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.ToString(); try { //客户端信息 var ct = new Core.ClientTo(); //用户信息 var userinfo = Func.Common.GetLoginUserInfo(); //日志保存 var mo = new Domain.SysLog() { LogId = Guid.NewGuid().ToString(), SuName = userinfo.UserName, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1 }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; System.Threading.ThreadPool.QueueUserWorkItem(_ => { using (var db = new ContextBase()) { db.SysLog.Add(mo); db.SaveChanges(); } }); } catch (Exception) { //throw new System.Exception("写入操作日志失败"); } } base.OnActionExecuted(filterContext); }
public override void OnActionExecuting(ActionExecutingContext context) { //日志 try { var hc = context.HttpContext; string controller = context.RouteData.Values["controller"].ToString().ToLower(); string action = context.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.Value; var referer = hc.Request.Headers["referer"].ToString(); var requestid = Core.UniqueTo.LongId().ToString(); hc.Response.Headers.Add("_qid", requestid); //客户端信息 var ct = new Core.ClientTo(hc); //用户信息 var userinfo = new Func.UserAuthAid(hc).Get(); //日志保存 var mo = new Func.LogsAid.LogsVM() { LogName = userinfo?.UserName, LogNickname = userinfo?.Nickname, LogRequestId = requestid, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogReferer = referer, LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1, LogLevel = "info" }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; Func.LogsAid.Insert(mo); } catch (Exception) { } base.OnActionExecuting(context); }
public override void OnResultExecuted(ResultExecutedContext context) { var hc = context.HttpContext; string controller = context.RouteData.Values["controller"].ToString().ToLower(); string action = context.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.Value; try { //客户端信息 var ct = new Core.ClientTo(hc); //用户信息 var userinfo = Func.Common.GetLoginUserInfo(hc); //日志保存 var mo = new Domain.SysLog() { LogId = Guid.NewGuid().ToString(), SuName = userinfo.UserName, SuNickname = userinfo.Nickname, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1 }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; using (var db = new Data.ContextBase()) { db.SysLog.Add(mo); db.SaveChanges(); } } catch (Exception) { //throw new System.Exception("写入操作日志失败"); } base.OnResultExecuted(context); }
public override void OnResultExecuted(ResultExecutedContext context) { var hc = context.HttpContext; string controller = context.RouteData.Values["controller"].ToString().ToLower(); string action = context.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.Value; try { //客户端信息 var ct = new Core.ClientTo(hc); //用户信息 var userinfo = Func.Common.GetLoginUserInfo(hc); //日志保存 var mo = new Domain.SysLog() { ID = Guid.NewGuid().ToString(), UserName = userinfo.UserName, Nickname = userinfo.Nickname, Action = controller + "/" + action, Url = url, Ip = ct.IPv4, CreateTime = DateTime.Now, BrowserName = ct.BrowserName, SystemName = ct.SystemName, LogGroup = 1, LogContent = Func.ProjectDocs.QueryNotes(controller, action, hc) }; using (var ru = new Data.RepositoryUse()) { ru.SysLogRepository.Insert(mo); } } catch (Exception) { //throw new System.Exception("写入操作日志失败"); } base.OnResultExecuted(context); }
public override void OnActionExecuting(ActionExecutingContext context) { var hc = context.HttpContext; //日志记录,设置“__nolog”参数可忽略日志记录,为压力测试等环境考虑(即一些不需要记录请求日志的需求) //如果需要记录更详细的日志,可提取请求的参数、表单信息写入日志 //可能信息量较大,需要考虑分开存储,推荐方案:使用sqlite-net-pcl包按月或按天拆分写入SQLite数据库文件 if (string.IsNullOrWhiteSpace(hc.Request.Query["__nolog"].ToString())) { string controller = context.RouteData.Values["controller"].ToString().ToLower(); string action = context.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.Value; try { //客户端信息 var ct = new Core.ClientTo(hc); //用户信息 var userinfo = Func.Common.GetLoginUserInfo(hc); //日志保存 var mo = new Domain.SysLog() { LogId = Guid.NewGuid().ToString(), SuName = userinfo.UserName, SuNickname = userinfo.Nickname, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1 }; try { //IP城市 var city = new ipdb.City(GlobalTo.GetValue("logs:ipdb").Replace("~", GlobalTo.ContentRootPath)); var ips = mo.LogIp.Split(','); var ipc = string.Empty; foreach (var ip in ips) { var listCity = city.find(ip.Trim().Replace("::1", "127.0.0.1"), "CN").Distinct(); ipc += string.Join(",", listCity).TrimEnd(',') + ";"; } mo.LogCity = ipc.TrimEnd(';'); } catch (Exception) { mo.LogCity = "fail"; } mo.LogContent = DicDescription[mo.LogAction.ToLower()]; #region 分批写入日志 //分批写入满足的条件:缓存的日志数量 int cacheLogCount = GlobalTo.GetValue <int>("logs:batchwritecount"); //分批写入满足的条件:缓存的时长,单位秒 int cacheLogTime = GlobalTo.GetValue <int>("logs:batchwritetime"); //日志记录 var cacheLogsKey = "Global_Logs"; //上次写入的时间 var cacheLogWriteKey = "Global_Logs_Write"; if (!(Core.CacheTo.Get(cacheLogsKey) is List <Domain.SysLog> cacheLogs)) { cacheLogs = new List <Domain.SysLog>(); } cacheLogs.Add(mo); var cacheLogWrite = Core.CacheTo.Get(cacheLogWriteKey) as DateTime?; if (!cacheLogWrite.HasValue) { cacheLogWrite = DateTime.Now; } if (cacheLogs?.Count > cacheLogCount || DateTime.Now.ToTimestamp() - cacheLogWrite.Value.ToTimestamp() > cacheLogTime) { using (var db = new Data.ContextBase()) { db.SysLog.AddRange(cacheLogs); db.SaveChanges(); } cacheLogs = null; cacheLogWrite = DateTime.Now; } Core.CacheTo.Set(cacheLogsKey, cacheLogs, 3600 * 24 * 30); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite, 3600 * 24 * 30); #endregion } catch (Exception) { //throw new System.Exception("写入操作日志失败"); } } base.OnActionExecuting(context); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { var hc = filterContext.HttpContext; //是Ajax请求 if (hc.Request.IsAjaxRequest()) { string controller = filterContext.RouteData.Values["controller"].ToString().ToLower(); string action = filterContext.RouteData.Values["action"].ToString().ToLower(); string url = hc.Request.Path.ToString() + hc.Request.QueryString.ToString(); try { //客户端信息 var ct = new Core.ClientTo(); //用户信息 var userinfo = Func.Common.GetLoginUserInfo(); //日志保存 var mo = new Domain.SysLog() { LogId = Guid.NewGuid().ToString(), SuName = userinfo.UserName, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1 }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; #region 分批写入日志 //分批写入满足的条件:缓存的日志数量 int cacheLogCount = 2000; //分批写入满足的条件:缓存的时长,单位秒 int cacheLogTime = 60; //日志记录 var cacheLogsKey = "Global_Logs"; //上次写入的时间 var cacheLogWriteKey = "Global_Logs_Write"; if (!(Core.CacheTo.Get(cacheLogsKey) is List <Domain.SysLog> cacheLogs)) { cacheLogs = new List <Domain.SysLog>(); } cacheLogs.Add(mo); var cacheLogWrite = Core.CacheTo.Get(cacheLogWriteKey) as DateTime?; if (!cacheLogWrite.HasValue) { cacheLogWrite = DateTime.Now; } if (cacheLogs?.Count > cacheLogCount || DateTime.Now.ToTimestamp() - cacheLogWrite.Value.ToTimestamp() > cacheLogTime) { using (var db = new ContextBase()) { db.SysLog.AddRange(cacheLogs); db.SaveChanges(); } cacheLogs = null; cacheLogWrite = DateTime.Now; } Core.CacheTo.Set(cacheLogsKey, cacheLogs, 3600 * 24 * 30); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite, 3600 * 24 * 30); #endregion } catch (Exception) { //throw new System.Exception("写入操作日志失败"); } } base.OnActionExecuted(filterContext); }