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
using Castle.DynamicProxy;
using iWareCommon.Properties;
using iWareCommon.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
 
namespace iWareCommon.Common.Entity
{
    /// <summary>
    /// 策略模式的代理类
    /// </summary>
    public class ServiceInterceptor : IInterceptor
    {
        private string userName;
 
        private string pageAddress;
 
        public ServiceInterceptor() { }
 
 
        public ServiceInterceptor(string userName, string pageAddress) { this.userName = userName; this.pageAddress = pageAddress; }
 
 
 
 
 
 
        public void Intercept(IInvocation invocation)
        {
            try 
            { 
                if (invocation.InvocationTarget != null)
                {
                    //继续执行方法
                    invocation.Proceed();
                    //类型
                    string logType=invocation.GetType().Name;
                    //记录时间
                    string logDate=DateTime.Now.ToString();
                    //用户名
                    string UserName = userName;
                    //登录IP地址
                    string userIpAddress = GetLocalIP();
                    /// 记录操作页面地址
                    string logPageAddress = pageAddress;
                    /// 操作调用方法
                    string logMethodName = invocation.Method.Name.ToString();
 
                    var resStr = HttpHelper.GetHttpResponse("http://localhost:2022" + "/Log/Savelog", new { logDate = logDate, userName = UserName, userIpAddress = userIpAddress, logPageAddress = logPageAddress, logMethodName = logMethodName }, 5000);
 
                    LogTextHelper.WriteLine(Resources.LogDir,"method={0}",invocation.Method.ToString());                                   
                }
            }
            catch(Exception ex)
            {
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Intercept", ex.Message);
            }
 
        }
 
 
        /// <summary>
        /// 获取本机IP地址
        /// </summary>
        /// <returns>本机IP地址</returns>
        public static string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
 
 
    }
}