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 { /// /// 策略模式的代理类 /// 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); } } /// /// 获取本机IP地址 /// /// 本机IP地址 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; } } } }