OperLogFilter.cs
3.72 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
100
101
102
103
104
105
106
107
108
109
110
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Net.Http.Headers;
using System.Collections.Generic;
using System.Diagnostics;
using WebRepository;
namespace WebApp
{
public class OperLogFilter : ActionFilterAttribute
{
private string ActionArguments { get; set; }
private Stopwatch Stopwatch { get; set; }
protected readonly IRepository<SysOperLog> _app;
public OperLogFilter(IRepository<SysOperLog> app, IAuth authUtil)
{
_app = app;
AuthStrategyContext authStrategyContext = authUtil.GetCurrentUser();
if (authStrategyContext != null)
{
_app._loginInfo = new LoginInfo
{
Id = authStrategyContext.User.Id,
Account = authStrategyContext.User.Account,
Name = authStrategyContext.User.Name,
};
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
ActionArguments = Newtonsoft.Json.JsonConvert.SerializeObject(filterContext.ActionArguments);
Stopwatch = new Stopwatch();
Stopwatch.Start();
}
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
Stopwatch.Stop();
IHeaderDictionary headersDictionary = context.HttpContext.Request.Headers;
string request = headersDictionary[HeaderNames.UserAgent].ToString();
string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
var description =
(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor;
var actionName = description.ActionName;
Dictionary<string, string> OperType = new Dictionary<string, string>();
OperType.Add("Index", "查看");
OperType.Add("Ins", "增加");
OperType.Add("Upd", "更新");
OperType.Add("DelByIds", "删除");
var otype = "";
if (OperType.ContainsKey(actionName))
{
otype = OperType[actionName];
}
else
{
otype = "其他";
}
string method = context.HttpContext.Request.Method;
string Name = _app._loginInfo.Name;
string Ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
string parameter = ActionArguments.Replace("\r", "").Replace("\n", "");
dynamic result = (context.Result == null ? null : (context.Result.GetType().Name == "EmptyResult" ? new { Value = "无返回结果" } : context.Result as dynamic));
string response = "在返回结果前发生了异常";
try
{
if (result != null)
{
response = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value).Replace("\r", "").Replace("\n", "");
}
}
catch (System.Exception)
{
response = "";
}
_app.Add(new SysOperLog()
{
Url = url,
OperType = otype,
Method = method,
Request = request,
Parameter = parameter,
Response = response,
TotalMilliseconds = Stopwatch.Elapsed.TotalMilliseconds,
LogTime = System.DateTime.Now,
Name = Name,
Ip = Ip
});
}
}
}