wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Filters;
using Pcb.Business.Authentication;
//using Pcb.Business.Biz;
using Pcb.Common;
using Pcb.Common.Enum;
using Pcb.Domain;
using Pcb.Infrastructure.Ioc;
 
namespace Pcb.Api.Infrastructures
{
    /// <summary>
    /// API Action Filter
    /// </summary>
    public class ActionInterceptAttribute :  ActionFilterAttribute
    {
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
           
            base.OnActionExecuting(actionContext);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
 
            if (actionExecutedContext.Response != null)
            {
                actionExecutedContext.Response.Headers.Add("PerformanceMonitor","");
                actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                if (AppConfigHelper.EnableReqLogging)
                    LogReqSuccessInfo(actionExecutedContext);
            }
        }
 
        private void LogReqSuccessInfo(HttpActionExecutedContext actionExecutedContext)
        {
            var req = actionExecutedContext.Request;
            if (actionExecutedContext.Response.StatusCode != System.Net.HttpStatusCode.InternalServerError)
            {
                IEnumerable<string> listPhoneType;
                IEnumerable<string> listPhoneUniqueSign;
                IEnumerable<string> listToken;
                IEnumerable<string> listUserAgent;
                req.Headers.TryGetValues("phone_type", out listPhoneType);
                req.Headers.TryGetValues("phone_unique_sign", out listPhoneUniqueSign);
                req.Headers.TryGetValues("token", out listToken);
                req.Headers.TryGetValues("User-Agent", out listUserAgent);
                var url = req.RequestUri.LocalPath;
                var ip = Utils.GetIP();
                var phoneType = listPhoneType == null ? "" : listPhoneType.Aggregate((a, b) => a + b);
                var phoneUniqueSign = listPhoneType == null ? "" : listPhoneUniqueSign.Aggregate((a, b) => a + b);
                var token = listToken == null ? "" : listToken.Aggregate((a, b) => a + b);
                var userAgent = listUserAgent == null ? string.Empty : listUserAgent.Aggregate((a, b) => a + " " + b);
                var member = WebAuthenticationContext.AppAuthenticationUser;
                string info = string.Format("<br>地址:{0} 请求成功! <br> 参数: {1} <br> 耗时: {2}", url, req.RequestUri.Query,"");
                int? memberId = null;
                if (member != null) memberId = member.UserId;
                //ThreadPool.QueueUserWorkItem(delegate { IocContainerManager.Container.Resolve<IApiLogApiBiz>().LogInfo(info, url, time , phoneType, ip, userAgent, memberId); });
            }
        }
    }
}