| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Concurrent; |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.HttpContextUser; |
| | | using WIDESEA_Core.Seed; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | |
| | | namespace WIDESEA_Core.LogHelper |
| | | { |
| | | public class Logger |
| | | { |
| | | private static readonly ILog log = LogManager.GetLogger(typeof(Logger)); |
| | | |
| | | public static void Debug(string message) |
| | | public static ConcurrentQueue<dynamic> loggerQueueData = new ConcurrentQueue<dynamic>(); |
| | | static Logger() |
| | | { |
| | | Task.Run(() => |
| | | { |
| | | StartWriteLog(); |
| | | }); |
| | | |
| | | } |
| | | |
| | | public static void Debug(string message, Exception exception) |
| | | static void StartWriteLog() |
| | | { |
| | | DataTable queueTable = CreateEmptyTable(); |
| | | while (true) |
| | | { |
| | | try |
| | | { |
| | | if (loggerQueueData.Count() > 0 && queueTable.Rows.Count < 500) |
| | | { |
| | | DequeueToTable(queueTable); continue; |
| | | } |
| | | //æ¯5ç§å䏿¬¡æ°æ® |
| | | Thread.Sleep(5000); |
| | | |
| | | if (queueTable.Rows.Count == 0) { continue; } |
| | | |
| | | SqlSugarScope sugarClient = new SqlSugarScope(new ConnectionConfig() |
| | | { |
| | | ConnectionString = DBContext.GetMainConnectionDb().Connection, |
| | | IsAutoCloseConnection = true, |
| | | DbType = MainDb.DbType, |
| | | }); |
| | | |
| | | int rows = sugarClient.Fastest<DataTable>().AS("Sys_Log").BulkCopy(queueTable); |
| | | |
| | | queueTable.Clear(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine(ex.ToString()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void Info(string message) |
| | | private static void DequeueToTable(DataTable queueTable) |
| | | { |
| | | |
| | | loggerQueueData.TryDequeue(out dynamic log); |
| | | if (log != null) |
| | | { |
| | | DataRow row = queueTable.NewRow(); |
| | | if (log.BeginDate == null || log.BeginDate?.Year < 2010) |
| | | { |
| | | log.BeginDate = DateTime.Now; |
| | | } |
| | | if (log.EndDate == null) |
| | | { |
| | | log.EndDate = DateTime.Now; |
| | | } |
| | | // row["Id"] = log.Id; |
| | | row["RequestParam"] = log.RequestParam?.Replace("\r\n", ""); |
| | | row["ResponseParam"] = log.ResponseParam?.Replace("\r\n", ""); |
| | | //row["Success"] = log.Success ?? -1; |
| | | row["BeginDate"] = log.BeginDate; |
| | | row["EndDate"] = log.EndDate; |
| | | row["ElapsedTime"] = ((DateTime)log.EndDate - (DateTime)log.BeginDate).TotalMilliseconds; |
| | | row["UserIP"] = log.UserIP; |
| | | row["Url"] = log.Url; |
| | | row["UserId"] = log.UserId ?? -1; |
| | | row["UserName"] = log.UserName; |
| | | queueTable.Rows.Add(row); |
| | | } |
| | | } |
| | | |
| | | public static void Info(string message, Exception exception) |
| | | private static DataTable CreateEmptyTable() |
| | | { |
| | | |
| | | DataTable queueTable = new DataTable(); |
| | | queueTable.Columns.Add("BeginDate", Type.GetType("System.DateTime")); |
| | | queueTable.Columns.Add("ElapsedTime", Type.GetType("System.Int32")); |
| | | queueTable.Columns.Add("EndDate", Type.GetType("System.DateTime")); |
| | | queueTable.Columns.Add("RequestParam", typeof(string)); |
| | | queueTable.Columns.Add("ResponseParam", typeof(string)); |
| | | //queueTable.Columns.Add("Success", Type.GetType("System.Int32")); |
| | | queueTable.Columns.Add("Url", typeof(string)); |
| | | queueTable.Columns.Add("UserIP", typeof(string)); |
| | | queueTable.Columns.Add("UserName", typeof(string)); |
| | | queueTable.Columns.Add("UserId", Type.GetType("System.Int32")); |
| | | //queueTable.Columns.Add("LogType", typeof(string)); |
| | | //queueTable.Columns.Add("ExceptionInfo", typeof(string)); |
| | | //queueTable.Columns.Add("ServiceIP", typeof(string)); |
| | | //queueTable.Columns.Add("BrowserType", typeof(string)); |
| | | //queueTable.Columns.Add("Role_Id", Type.GetType("System.Int32")); |
| | | return queueTable; |
| | | } |
| | | |
| | | public static void Warn(string message) |
| | | public static void Add(string requestParameter, string responseParameter) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Warning(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Error(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Error(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Fatal(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Fatal(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void WriteApiLog2DB(HttpContext context, string requestParameter, DateTime beginDate, string responseParameter, DateTime endDate, LoggerStatus loggerStatus) |
| | | { |
| | | dynamic log = null; |
| | | try |
| | | { |
| | | if (context.Request.Method == "OPTIONS") return; |
| | | |
| | | HttpContext context = App.HttpContext; |
| | | if (context == null) |
| | | { |
| | | Console.WriteLine($"æªè·åå°httpcontextä¿¡æ¯,reqParam:{requestParameter},respParam:{responseParameter},success:{loggerStatus}"); |
| | | return; |
| | | } |
| | | if (context.Request.Method == "OPTIONS") return; |
| | | RequestLogModel logModel = (context.RequestServices.GetService(typeof(RequestLogModel)) as RequestLogModel) ?? new RequestLogModel { RequestDate = DateTime.Now }; |
| | | |
| | | Dictionary<string, object> dic = new Dictionary<string, object> |
| | | IUser user = App.User; |
| | | log = new |
| | | { |
| | | {"BeginDate",beginDate }, |
| | | {"ElapsedTime",(endDate - beginDate).TotalMilliseconds.DoubleToInt() }, |
| | | {"EndDate",endDate }, |
| | | {"RequestParam",requestParameter }, |
| | | {"ResponseParam",responseParameter }, |
| | | {"Success",1 }, |
| | | {"Url",context.Request.Path.Value??"" }, |
| | | {"UserIP",context.GetUserIp() }, |
| | | {"UserName","App.User?.Name" }, |
| | | {"User_Id","0" } |
| | | |
| | | //{"BeginDate",beginDate }, |
| | | //{"ElapsedTime",(endDate - beginDate).TotalMilliseconds.ObjToInt() }, |
| | | //{"EndDate",endDate }, |
| | | //{"RequestParam",requestParameter }, |
| | | //{"ResponseParam",responseParameter }, |
| | | //{"Success",1 }, |
| | | //{"Url",context.Request.Path.Value??"" }, |
| | | //{"UserIP",context.GetUserIp() }, |
| | | //{"UserName","App.User?.Name" }, |
| | | //{"User_Id","App.User?.ID" } |
| | | BeginDate = logModel.RequestDate, |
| | | EndDate = DateTime.Now, |
| | | RequestParam = requestParameter, |
| | | ResponseParam = responseParameter, |
| | | Url = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase + context.Request.Path, |
| | | UserIP = GetClientIP(context)?.Replace("::ffff:", ""), |
| | | UserId = user.UserId, |
| | | UserName = user.UserName |
| | | }; |
| | | |
| | | |
| | | SqlSugarClient sqlSugarClient = DBContext.GetCustomDB(DBContext.GetConnectionConfig()); |
| | | sqlSugarClient.Insertable(dic).AS("Sys_Log").ExecuteCommand(); |
| | | } |
| | | catch (Exception ex) |
| | | catch (Exception exception) |
| | | { |
| | | |
| | | log = log ?? new |
| | | { |
| | | BeginDate = DateTime.Now, |
| | | EndDate = DateTime.Now, |
| | | RequestParam = requestParameter, |
| | | ResponseParam = responseParameter, |
| | | }; |
| | | } |
| | | //æ·»å ç³»ç»æ¥å¿ |
| | | loggerQueueData.Enqueue(log); |
| | | } |
| | | } |
| | | |
| | | public enum LoggerStatus |
| | | { |
| | | Success = 1, |
| | | Error = 2, |
| | | Info = 3 |
| | | public static string GetClientIP(HttpContext context) |
| | | { |
| | | var ip = context.Request.Headers["X-Forwarded-For"].ObjToString(); |
| | | if (string.IsNullOrEmpty(ip)) |
| | | { |
| | | ip = context.Connection.RemoteIpAddress.ObjToString(); |
| | | } |
| | | |
| | | return ip; |
| | | } |
| | | } |
| | | } |