QueueInterLog.cs
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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,
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);
}
}
}