|
@@ -0,0 +1,89 @@
|
|
|
+using CB.Data.SqlServer;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Sockets;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Web;
|
|
|
+
|
|
|
+namespace CB.Web.Kjh.Filters
|
|
|
+{
|
|
|
+ public class ActionFilter
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置当前工作线程的名称。供用来统一标识记录的日志
|
|
|
+ /// </summary>
|
|
|
+ public class ThreadNameFilter : IHttpModule
|
|
|
+ {
|
|
|
+
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ //throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Init(HttpApplication context)
|
|
|
+ {
|
|
|
+ //NewMethod(context);请求在此上下文中不可用
|
|
|
+
|
|
|
+ context.BeginRequest += context_BeginRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置当前工作线程的name
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ private void SetThreadName(object sender)
|
|
|
+ {
|
|
|
+ var task = new Task(() => {
|
|
|
+ if (null != Thread.CurrentThread.Name)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ HttpApplication application = (HttpApplication)sender;
|
|
|
+ HttpRequest request = application.Context.Request;
|
|
|
+ var url = request.Url.ToString();
|
|
|
+ var ip = GetWebClientIp(request);
|
|
|
+ VisitIPDAL.AddActionFilterLog(url, ip);
|
|
|
+ });
|
|
|
+
|
|
|
+ task.Start();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ void context_BeginRequest(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ SetThreadName(sender);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取Web客户端的Ip
|
|
|
+ /// </summary>
|
|
|
+ private static string GetWebClientIp(HttpRequest request)
|
|
|
+ {
|
|
|
+ var ip = "";
|
|
|
+ if (request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED_FOR"))
|
|
|
+ ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.ServerVariables["REMOTE_ADDR"];
|
|
|
+ else
|
|
|
+ ip = request.ServerVariables["REMOTE_ADDR"];
|
|
|
+
|
|
|
+ foreach (var hostAddress in Dns.GetHostAddresses(ip))
|
|
|
+ {
|
|
|
+ if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
|
|
|
+ {
|
|
|
+ return hostAddress.ToString();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return ip == "::1" ? "127.0.0.0" : ip;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return string.Empty;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|