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 WIDESEAWCS_Core.DB; 
 | 
using WIDESEAWCS_Core.Helper; 
 | 
using WIDESEAWCS_Core.HttpContextUser; 
 | 
using WIDESEAWCS_Core.Seed; 
 | 
  
 | 
namespace WIDESEAWCS_Core.LogHelper 
 | 
{ 
 | 
    public static class Logger 
 | 
    { 
 | 
        public static ConcurrentQueue<dynamic> loggerQueueData = new ConcurrentQueue<dynamic>(); 
 | 
        static Logger() 
 | 
        { 
 | 
            Task.Run(() => 
 | 
            { 
 | 
                StartWriteLog(); 
 | 
            }); 
 | 
  
 | 
        } 
 | 
  
 | 
        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()); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        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); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        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 Add(string requestParameter, string responseParameter) 
 | 
        { 
 | 
            dynamic log = null; 
 | 
            try 
 | 
            { 
 | 
                HttpContext context = App.HttpContext; 
 | 
                if (context == null) 
 | 
                { 
 | 
                    return; 
 | 
                } 
 | 
                if (context.Request.Method == "OPTIONS") return; 
 | 
                RequestLogModel logModel = (context.RequestServices.GetService(typeof(RequestLogModel)) as RequestLogModel) ?? new RequestLogModel { RequestDate = DateTime.Now }; 
 | 
  
 | 
                IUser user = App.User; 
 | 
                log = new 
 | 
                { 
 | 
                    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 
 | 
                }; 
 | 
            } 
 | 
            catch (Exception exception) 
 | 
            { 
 | 
                log = log ?? new 
 | 
                { 
 | 
                    BeginDate = DateTime.Now, 
 | 
                    EndDate = DateTime.Now, 
 | 
                    RequestParam = requestParameter, 
 | 
                    ResponseParam = responseParameter, 
 | 
                }; 
 | 
            } 
 | 
            //添加系统日志 
 | 
            loggerQueueData.Enqueue(log); 
 | 
        } 
 | 
  
 | 
        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; 
 | 
        } 
 | 
    } 
 | 
} 
 |