zx 6 anni fa
parent
commit
81fa2e6b89

+ 5 - 0
CP55128/CB.Web.Kjh/CB.Web.Kjh.csproj

@@ -49,6 +49,9 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\CB.Web\dll\Core.Net.dll</HintPath>
     </Reference>
+    <Reference Include="Dapper, Version=1.50.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\Dapper.1.50.0\lib\net40\Dapper.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
@@ -169,6 +172,8 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AppCode\CacheUtils.cs" />
+    <Compile Include="Filters\ActionFilter.cs" />
+    <Compile Include="Filters\DapperHelper.cs" />
     <Compile Include="Footer.ascx.cs">
       <DependentUpon>Footer.ascx</DependentUpon>
       <SubType>ASPXCodeBehind</SubType>

+ 89 - 0
CP55128/CB.Web.Kjh/Filters/ActionFilter.cs

@@ -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;
+        }
+    }
+}

+ 77 - 0
CP55128/CB.Web.Kjh/Filters/DapperHelper.cs

@@ -0,0 +1,77 @@
+using Dapper;
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Data;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Web;
+
+namespace CB.Web.Kjh.Filters
+{
+    public class DapperHelper
+    {
+        class Nested
+        {
+            internal static readonly string connString = ConfigurationManager.ConnectionStrings["CB55128ConnectionString"].ToString();
+        }
+
+        #region 属性
+
+
+
+        /// <summary>
+        /// 获取 数据库连接串
+        /// </summary>
+        private IDbConnection Connection
+        {
+            get
+            {
+                var dbconnection = new SqlConnection(Nested.connString);
+                if (dbconnection.State == ConnectionState.Closed)
+                {
+                    dbconnection.Open();
+                }
+                return dbconnection;
+            }
+
+        }
+
+        #endregion 属性
+
+        /// <summary>
+        /// 新增sql
+        /// </summary>
+        /// <param name="sql">sql语句</param>
+        /// <param name="para">参数化</param>
+        public void Insert(string sql, object para = null)
+        {
+
+            using (var db = Connection)
+            {
+                db.Execute(sql, para);
+            }
+        }
+    }
+
+    public class VisitIPDAL{
+        /// <summary>
+        /// 拦截器日志
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="ip"></param>
+        public static void AddActionFilterLog(string url, string ip)
+        {
+            var sql = @"
+INSERT INTO VisitIP(IP,Url,Time)
+VALUES (@IP,@Url,@Time)
+";
+            var para = new SqlParameter[] {
+                new SqlParameter(@"IP",ip),
+                new SqlParameter(@"Url",url),
+                new SqlParameter(@"Time",DateTime.Now),
+            };
+            new DapperHelper().Insert(sql, new { IP = ip, Url = url, Time = DateTime.Now });
+        }
+    }
+}

+ 6 - 0
CP55128/CB.Web.Kjh/Web.config

@@ -1,5 +1,9 @@
 <?xml version="1.0"?>
 <configuration>
+  <connectionStrings>
+   <add name="CB55128ConnectionString" connectionString="server=119.23.72.139;database=CB55128;uid=sa;pwd=NK4Qwql8Z?csv(XNew;Connect Timeout=30;persist security info=false;Pooling=true;" providerName="System.Data.SqlClient"/>
+
+  </connectionStrings>
   <appSettings>
     <add key="blackips" value="192.168.1.200;"/>
     <add key="getZX" value="http://ht.55128.cn"></add>
@@ -72,6 +76,8 @@
 		</defaultDocument>
     <modules>
       <add name="blackips" type="CB.Common.BlackIpHttpModule"/>
+      <add name="threadNameFilter" type="CB.Web.Kjh.Filters.ThreadNameFilter" preCondition="managedHandler" />
+      <!--只对托管资源起作用-->
     </modules>
 	</system.webServer>
 </configuration>

+ 1 - 0
CP55128/CB.Web.Kjh/packages.config

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
+  <package id="Dapper" version="1.50.0" targetFramework="net40" />
   <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40" />
 </packages>