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("X-Request-Id", requestid); //客户端信息 var ct = new Fast.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.Split(',')[0].Trim(), 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 static Logging.LoggingModel GetLog(Microsoft.AspNetCore.Http.HttpContext context) { string reqPath = context.Request.Path.ToString(); string reqQueryString = context.Request.QueryString.ToString(); //客户端信息 var ct = new Fast.ClientTo(context); //用户信息 var userinfo = new Application.UserAuthService(context).Get(); //日志保存 var mo = new Logging.LoggingModel() { LogApp = "netnr.com", LogUid = userinfo?.UserName, LogNickname = userinfo?.Nickname, LogAction = reqPath, LogUrl = reqPath + reqQueryString, LogIp = ct.IPv4.Split(',')[0].Trim(), LogReferer = ct.Referer, LogCreateTime = DateTime.Now, LogUserAgent = ct.UserAgent, LogGroup = "1", LogLevel = "I" }; var ddk = reqPath.ToLower().TrimStart('/'); if (DicDescription.ContainsKey(ddk)) { mo.LogContent = DicDescription[ddk]; } return(mo); }
public override void OnActionExecuting(ActionExecutingContext context) { var hc = context.HttpContext; //日志记录,设置“__nolog”参数可忽略日志记录,为压力测试等环境考虑(即一些不需要记录请求日志的需求) //如果需要记录更详细的日志,可提取请求的参数、表单信息写入日志 //可能信息量较大,需要考虑分开存储,推荐方案:使用sqlite-net-pcl包按月或按天拆分写入SQLite数据库文件 if (GlobalTo.GetValue <bool>("logs:enable") && 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 Fast.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.Split(',')[0].Trim(), LogCreateTime = DateTime.Now, LogBrowserName = ct.BrowserName, LogSystemName = ct.SystemName, LogGroup = 1 }; 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) { //异步写入日志 System.Threading.ThreadPool.QueueUserWorkItem(_ => { //写入日志前查询IP所属区域 var dbpath = GlobalTo.GetValue("logs:dbpath").Replace("~", GlobalTo.ContentRootPath); using var ds = new IP2Region.DbSearcher(dbpath); foreach (var log in cacheLogs) { try { var ips = log.LogIp.Split(','); var ipi = new List <string>(); foreach (var ip in ips) { //内容格式:国家|区域|省份|市|运营商。无数据默认为0。 var listIpInfo = ds.MemorySearch(ip.Trim().Replace("::1", "127.0.0.1")).Region.Split('|').ToList(); listIpInfo.RemoveAt(1); listIpInfo = listIpInfo.Where(x => x != "0").Distinct().ToList(); ipi.Add(string.Join(",", listIpInfo)); } log.LogCity = string.Join(";", ipi); } catch (Exception) { log.LogCity = "fail"; } } using var db = new Data.ContextBase(Data.ContextBase.DCOB().Options); 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); }); } else { 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 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 Fast.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.Split(',')[0].Trim(), 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 OnActionExecuting(ActionExecutingContext context) { var hc = context.HttpContext; //日志记录,设置“__nolog”参数可忽略日志记录,为压力测试等环境考虑(即一些不需要记录请求日志的需求) if (GlobalTo.GetValue <bool>("logs:enable") && 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 Fast.ClientTo(hc); //用户信息 var userinfo = Application.CommonService.GetLoginUserInfo(hc); //日志保存 var mo = new Domain.SysLog() { SuName = userinfo.UserName, SuNickname = userinfo.Nickname, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4, LogUserAgent = ct.UserAgent, LogCreateTime = DateTime.Now, LogGroup = 1, LogLevel = "I" }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; #region 分批写入日志 //分批写入满足的条件:缓存的日志数量 int cacheLogCount = GlobalTo.GetValue <int>("logs:CacheWriteCount"); //分批写入满足的条件:缓存的时长,单位秒 int cacheLogTime = GlobalTo.GetValue <int>("logs:CacheWriteSecond"); //日志记录 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) { //异步写入日志 System.Threading.ThreadPool.QueueUserWorkItem(_ => { try { var ipto = new IPAreaTo(); //写入日志前 foreach (var log in cacheLogs) { log.LogId = Core.UniqueTo.LongId().ToString(); log.LogArea = ipto.Parse(log.LogIp); var uato = new UserAgentTo(log.LogUserAgent); log.LogBrowserName = uato.BrowserName + " " + uato.BrowserVersion; log.LogSystemName = uato.SystemName + " " + uato.SystemVersion; if (uato.IsBot) { log.LogGroup = 2; } } using var db = new Data.ContextBase(Data.ContextBase.DCOB().Options); db.SysLog.AddRange(cacheLogs); db.SaveChanges(); //清空数据及更新时间 cacheLogs = null; cacheLogWrite = DateTime.Now; Core.CacheTo.Set(cacheLogsKey, cacheLogs); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite); } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } }); } else { Core.CacheTo.Set(cacheLogsKey, cacheLogs); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite); } #endregion } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } } base.OnActionExecuting(context); }
public override void OnActionExecuting(ActionExecutingContext context) { var hc = context.HttpContext; //日志记录,设置“__nolog”参数可忽略日志记录,为压力测试等环境考虑(即一些不需要记录请求日志的需求) if (GlobalTo.GetValue <bool>("logs:enable") && 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 Fast.ClientTo(hc); //用户信息 var userinfo = Application.CommonService.GetLoginUserInfo(hc); //日志保存 var mo = new Domain.SysLog() { SuName = userinfo.UserName, SuNickname = userinfo.Nickname, LogAction = controller + "/" + action, LogUrl = url, LogIp = ct.IPv4.Split(',')[0].Trim(), LogCreateTime = DateTime.Now, LogGroup = 1 }; mo.LogContent = DicDescription[mo.LogAction.ToLower()]; #region 分批写入日志 //分批写入满足的条件:缓存的日志数量 int cacheLogCount = GlobalTo.GetValue <int>("logs:CacheWriteCount"); //分批写入满足的条件:缓存的时长,单位秒 int cacheLogTime = GlobalTo.GetValue <int>("logs:CacheWriteSecond"); //日志记录 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) { //异步写入日志 System.Threading.ThreadPool.QueueUserWorkItem(_ => { try { //写入日志前查询IP所属区域 var dbpath = Path.Combine(GlobalTo.GetValue("logs:path").Replace("~", GlobalTo.ContentRootPath), "ip2region.db"); if (File.Exists(dbpath)) { using var ds = new IP2Region.DbSearcher(dbpath); foreach (var log in cacheLogs) { var ips = log.LogIp.Split(','); var ipi = new List <string>(); foreach (var ip in ips) { //内容格式:国家|区域|省份|市|运营商。无数据默认为0。 var listIpInfo = ds.MemorySearch(ip.Trim().Replace("::1", "127.0.0.1")).Region.Split('|').ToList(); listIpInfo.RemoveAt(1); listIpInfo = listIpInfo.Where(x => x != "0").Distinct().ToList(); ipi.Add(string.Join(",", listIpInfo)); } log.LogCity = string.Join(";", ipi); } } //赋值主键(在前面赋值出现主键重复的问题,原因暂不明) cacheLogs.ForEach(x => x.LogId = Guid.NewGuid().ToString("N")); using var db = new Data.ContextBase(Data.ContextBase.DCOB().Options); db.SysLog.AddRange(cacheLogs); db.SaveChanges(); //清空数据及更新时间 cacheLogs = null; cacheLogWrite = DateTime.Now; Core.CacheTo.Set(cacheLogsKey, cacheLogs); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite); } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } }); } else { Core.CacheTo.Set(cacheLogsKey, cacheLogs); Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite); } #endregion } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } } base.OnActionExecuting(context); }