QueueInterLog.cs 3.3 KB
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Hh.Mes.Common.Http;
using Hh.Mes.POJO.Entity;

namespace Hh.Mes.Service.Logs
{
    /// <summary>
    /// 接口日志队列
    /// </summary>
    public class QueueInterLog
    {
        private static QueueInterLog _Log;
        private static ConcurrentQueue<sys_interface_log> queue;
        private static readonly object LockHelper = new object();

        private QueueInterLog()
        {
        }

        public static QueueInterLog GetInstance
        {
            get
            {
                if (_Log == null)
                {
                    lock (LockHelper)
                    {
                        if (_Log == null)
                        {
                            _Log = new QueueInterLog();
                            queue = new ConcurrentQueue<sys_interface_log>();
                        }
                    }
                }
                return _Log;
            }
        }

        public ConcurrentQueue<sys_interface_log> Queue()
        {
            return queue;
        }
        public string getFileName()
        {
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(5);//1代表上级,2代表上上级,以此类推
            MethodBase method = frame.GetMethod();
            string className = method.ReflectedType?.Name;
            return "【" + className + "】" + method;
        }

        /// <summary>
        /// 接口日志队列
        /// </summary>
        /// <param name="httpItem"></param>
        /// <param name="httpResult">返回结果</param>
        /// <param name="type">发送或者接收</param>
        /// <param name="remark"></param>
        /// <param name="totalMilliseconds">消耗时间</param>
        /// <param name="user">用户</param>
        /// <param name="sysTitle">标题 </param>
        public void EnqueueInterLog(HttpItem httpItem, HttpResult httpResult, string type, string remark, double totalMilliseconds, string user = "", string sysTitle = "定时器")
        {
            var sysLog = new sys_interface_log
            {
                type = type,
                remark = remark,
                createTime = DateTime.Now,
                logTime = DateTime.Now,
                apiGroup = "",
                actionName = getFileName(),
                system = sysTitle,
                createBy = "Api",
                name = user,
                totalMilliseconds = totalMilliseconds,
                request = !string.IsNullOrWhiteSpace(remark) ? remark : string.Empty
            };
            if (httpItem != null && httpResult != null)
            {
                sysLog.method = httpItem.Method;
                sysLog.server = httpItem.Host;
                sysLog.path = httpItem.URL;
                sysLog.request = httpItem.Postdata;
                sysLog.response = httpResult.Html;
                sysLog.logTime = DateTime.Now;
                sysLog.ip = httpItem.URL;
                sysLog.browser = httpItem.UserAgent;
                sysLog.result = httpResult.Html;
                sysLog.createTime = DateTime.Now;
            }
            queue.Enqueue(sysLog);
        }
    }
}