Bladeren bron

修改Log

Klosszhu 2 jaren geleden
bovenliggende
commit
fbd70876ac

BIN
.vs/Fine.OPCDaClient/DesignTimeBuild/.dtbcache.v2


BIN
.vs/Fine.OPCDaClient/FileContentIndex/5c8c6bf1-369f-4cf3-bfb1-312f187cd530.vsidx


BIN
.vs/Fine.OPCDaClient/FileContentIndex/9fdede6f-3071-46a2-bc5e-e8232feddbed.vsidx


BIN
.vs/Fine.OPCDaClient/FileContentIndex/ab495d50-c59b-4ac1-9db1-fdc1fd53917e.vsidx


BIN
.vs/Fine.OPCDaClient/FileContentIndex/b275005d-9cc5-4745-b365-30aeec7bb9cb.vsidx


BIN
.vs/Fine.OPCDaClient/FileContentIndex/f5c0e535-194e-4b22-8b9e-9c04eb5f3c76.vsidx


BIN
.vs/ProjectEvaluation/fine.opcdaclient.metadata.v2


BIN
.vs/ProjectEvaluation/fine.opcdaclient.projects.v2


+ 67 - 55
Fine.HttpServer/HttpServer.cs

@@ -15,11 +15,11 @@ using System.Web;
 
 namespace Fine.HttpServer
 {
-    public class HttpServer<T> where T: Fine.IHttpServer.IHttpHandler
+    public class HttpServer<T> where T : Fine.IHttpServer.IHttpHandler
     {
         HttpListener listener;
         HttpServerParams _serverParams;
-       public bool Status { get; set; } = false;
+        public bool Status { get; set; } = false;
         public HttpServer(HttpServerParams serverparams)
         {
             _serverParams = serverparams;
@@ -27,15 +27,15 @@ namespace Fine.HttpServer
         }
         public void Start()
         {
-            
-                    
-            listener.Prefixes.Add("http://"+_serverParams.Host+":"+_serverParams.Port+"/");
+
+
+            listener.Prefixes.Add("http://" + _serverParams.Host + ":" + _serverParams.Port + "/");
             //listener.Prefixes.Add("https://" + _serverParams.Host + ":" + (_serverParams.Port + 1) + "/");
             if (_serverParams.SSL == true)
             {
                 listener.Prefixes.Add("https://" + _serverParams.Host + ":" + _serverParams.HttpsPort + "/");
             }
-            
+
             //启动监听器
             listener.Start();
             Status = true;
@@ -44,35 +44,36 @@ namespace Fine.HttpServer
             {
                 Console.WriteLine($"Start up Http Server:https://{_serverParams.Host}:{_serverParams.HttpsPort}");
             }
-            listener.BeginGetContext(Result,null);
 
-            
+            listener.BeginGetContext(Result, null);
+
+
 
             if (_serverParams.SSL)
             {
-                AddCertificate(_serverParams.Host+":"+ _serverParams.HttpsPort);
+                AddCertificate(_serverParams.Host + ":" + _serverParams.HttpsPort);
             }
-            
+
 
         }
 
         private void AddCertificate(string ipport)
         {
-            
-            X509Certificate2 cert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory+"/config/fine.pfx", _serverParams.CertPassword, X509KeyStorageFlags.MachineKeySet |
+
+            X509Certificate2 cert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "/config/fine.pfx", _serverParams.CertPassword, X509KeyStorageFlags.MachineKeySet |
                 X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
-            string hashstr=cert.GetCertHashString();
+            string hashstr = cert.GetCertHashString();
             Console.WriteLine("CertHash:" + hashstr);
             object[] attrs = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
             Guid id = new Guid(((System.Runtime.InteropServices.GuidAttribute)attrs[0]).Value);
             string appid = id.ToString();
-            Console.WriteLine("App ID:"+appid);
+            Console.WriteLine("App ID:" + appid);
             //95df4f3c-8179-4576-91d6-2dd20bba5942            
             X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
-            
+
             store.Open(OpenFlags.ReadWrite);
             store.Add(cert);
-            
+
             store.Close();
             delnetsh(ipport);
             addnetsh(ipport, hashstr, appid);
@@ -80,12 +81,12 @@ namespace Fine.HttpServer
 
         }
 
-        private void addnetsh(string ipport,string certhash,string appid)
+        private void addnetsh(string ipport, string certhash, string appid)
         {
             //netsh http del sslcert ipport=0.0.0.0:9003 certhash=E2D1C410BDFBFBD7E85368C2419F1B06078D4F7C appid={95df4f3c-8179-4576-91d6-2dd20bba5942}
-            
+
             ExcuteCMD("netsh", $"http add sslcert ipport={ipport} certhash={certhash} appid={{{appid}}}");
-            
+
         }
 
         private void ExcuteCMD(string cmd, string arg)
@@ -94,7 +95,7 @@ namespace Fine.HttpServer
             ProcessStartInfo psi = new ProcessStartInfo();
             psi.FileName = cmd;
             psi.Arguments = arg;
-                    
+
             Process proc = Process.Start(psi);
             proc.StartInfo.RedirectStandardOutput = true;
             proc.StartInfo.UseShellExecute = false;
@@ -120,41 +121,51 @@ namespace Fine.HttpServer
         private void Result(IAsyncResult ar)
         {
             //var httpcontext = listener.GetContext();
-
             DateTime stime = DateTime.Now;
-            if (listener.IsListening)
+            try
             {
-                listener.BeginGetContext(Result, null);
-                var httpcontext = listener.EndGetContext(ar);
 
-                httpcontext.Response.Headers.Add("Content-type", "text/html;charset=UTF-8");
-                httpcontext.Response.ContentEncoding = Encoding.UTF8;
 
-                httpcontext.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
-                httpcontext.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
-                httpcontext.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); //后台跨域请求
-                                                                                               //httpcontext.Response.AppendHeader("Server", "MyIIS");//后台跨域请求,通常设置为配置文件
-                httpcontext.Response.AppendHeader("Authorize", ConfigurationManager.AppSettings["auth"]);
+               
 
-                Fine.IHttpServer.HttpResult result = null;
-                try
+                if (listener.IsListening)
                 {
-                    result = ProcessRequest(httpcontext);
-                }
-                catch (Exception ee)
-                {
-                    result = Fine.IHttpServer.HttpResult.CreateError(ee);
-                }
-                string json = JsonUtil.SerializeObject(result);
+                    listener.BeginGetContext(Result, null);
+                    var httpcontext = listener.EndGetContext(ar);
+
+                    httpcontext.Response.Headers.Add("Content-type", "text/html;charset=UTF-8");
+                    httpcontext.Response.ContentEncoding = Encoding.UTF8;
 
-                byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
-                httpcontext.Response.OutputStream.Write(data, 0, data.Length);
-                httpcontext.Response.OutputStream.Flush();
-                httpcontext.Response.OutputStream.Close();
-                Console.WriteLine($"process request cost {(DateTime.Now - stime).TotalMilliseconds}");
+                    httpcontext.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
+                    httpcontext.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
+                    httpcontext.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); //后台跨域请求
+                                                                                                   //httpcontext.Response.AppendHeader("Server", "MyIIS");//后台跨域请求,通常设置为配置文件
+                                                                                                   //httpcontext.Response.AppendHeader("Authorize", ConfigurationManager.AppSettings["auth"]);
+
+                    Fine.IHttpServer.HttpResult result = null;
+                    try
+                    {
+                        result = ProcessRequest(httpcontext);
+                    }
+                    catch (Exception ee)
+                    {
+                        result = Fine.IHttpServer.HttpResult.CreateError(ee);
+                    }
+                    string json = JsonUtil.SerializeObject(result);
+
+                    byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
+                    httpcontext.Response.OutputStream.Write(data, 0, data.Length);
+                    httpcontext.Response.OutputStream.Flush();
+                    httpcontext.Response.OutputStream.Close();
+                    Console.WriteLine($"process request cost {(DateTime.Now - stime).TotalMilliseconds}");
+                }
+            }
+            catch
+            {
+                LogUtil.WriteLog("网络连接异常,服务将重启" + stime);
+                this.Start();
             }
-           
-           
+
         }
 
         private Fine.IHttpServer.HttpResult ProcessRequest(HttpListenerContext context)
@@ -173,7 +184,7 @@ namespace Fine.HttpServer
                 urlpars = url.Substring(url.IndexOf('?') + 1, url.Length - (url.IndexOf('?') + 1));
 
             }
-            if (Convert.ToBoolean(ConfigurationManager.AppSettings["isauth"])&&!string.IsNullOrEmpty(request.Headers["auth"]) && request.Headers["auth"].ToLower() != ConfigurationManager.AppSettings["auth"].ToLower())
+            if (Convert.ToBoolean(ConfigurationManager.AppSettings["isauth"]) && !string.IsNullOrEmpty(request.Headers["auth"]) && request.Headers["auth"].ToLower() != ConfigurationManager.AppSettings["auth"].ToLower())
             {
                 rlt = new { code = 401, msg = "无权限" };
                 return Fine.IHttpServer.HttpResult.CreateSucess(rlt);
@@ -193,22 +204,22 @@ namespace Fine.HttpServer
                     byte[] content = new byte[1024];
                     System.IO.MemoryStream ms = new System.IO.MemoryStream();
 
-                    int readlen = 0;                    
+                    int readlen = 0;
                     do
                     {
                         readlen = request.InputStream.Read(content, 0, content.Length);
-                        
-                        ms.Write(content,0,readlen);
+
+                        ms.Write(content, 0, readlen);
                     } while (readlen != 0);
 
                     var requestdata = ms.ToArray();
-                    ms.Close();                    
+                    ms.Close();
                     par = System.Text.Encoding.UTF8.GetString(requestdata);
                 }
                 rlt = handler.Post(action, par);
             }
 
-            return Fine.IHttpServer.HttpResult.CreateSucess(rlt) ;
+            return Fine.IHttpServer.HttpResult.CreateSucess(rlt);
         }
 
         private string UrlParamsToJson(string urlpars)
@@ -246,7 +257,8 @@ namespace Fine.HttpServer
         }
 
 
-        public void Close() {
+        public void Close()
+        {
             if (Status)
             {
                 listener.Stop();
@@ -254,7 +266,7 @@ namespace Fine.HttpServer
             Status = false;
         }
 
-     
+
 
     }
 }

+ 5 - 1
Fine.OPCDaClient9000/App/AppHandle.cs

@@ -13,7 +13,11 @@ namespace Fine.OPCDaClient9000.App
     public class AppHandle : IHttpHandler
     {
 
-
+        ///<summary>
+        ///获取客户端IP地址
+        ///</summary>
+        ///<returns>IP地址</returns>
+     
         public object Get(string action, string pras)   //Alon 2023-03
         {
             LogUtil.WriteLog( "--前端调用--" + action + ":", $"【Get动作】--- {action}     【Params参数】--- {pras}");

+ 5 - 5
Fine.OPCDaClient9000/App/Common/AppUtil.cs

@@ -77,7 +77,7 @@ namespace Fine.OPCDaClient9000.Util
             var first = newsConfig.SelectMany(a=>a.Data).FirstOrDefault(a => "api/" + a.APIName == action && a.Method == method);
             if (first != null)
             {
-                LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + first.APIName, "进入查询!");
+                LogUtil.WriteLog(first.APIName, "进入查询!");
                 if (param != null)
                 {
                     return WrNewCheckList(first, param);
@@ -241,7 +241,7 @@ namespace Fine.OPCDaClient9000.Util
 
             if (list.Count > 20)   //判断写入的数量,超过20不进行操作
             {
-                LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "写入数据过多,执行失败");
+                LogUtil.WriteLog("错误信息", "写入数据过多,执行失败");
                 rlt_All.Sucessful = false;
                 rlt_All.Message = "写入数据过多,执行失败";
                 return rlt_All;
@@ -264,7 +264,7 @@ namespace Fine.OPCDaClient9000.Util
             /// 1:在不检查状态下,直接返回结果信息 
             if (!bCheck)   //在不检查状态下
             {
-                LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "不检查", "不检查");
+                LogUtil.WriteLog("不检查", "不检查");
                 rlt_All.Sucessful = true;
                 rlt_All.Message = "不检查";
                 rlt_All.PlcResults = new List<DataItem>() { new DataItem { TagAddress = "", IsSetSuccessful = false } };
@@ -314,7 +314,7 @@ namespace Fine.OPCDaClient9000.Util
                 //if (write.ToString() == check.ToString())    allen 2023-04 检查代码出错,进行屏蔽,修改
                 if (Rlst[i].Value == null)   //判断是否读取值,没有读取到(一般是标签值不存在情况),  跳出返回:写入失败
                 {
-                    LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---失败");
+                    LogUtil.WriteLog("错误信息", "批量写入---失败");
                     Rlst[i].IsSetSuccessful = false;
                     Rlst[i].Message = "写入失败";
                     rlt_All.Sucessful = false;
@@ -331,7 +331,7 @@ namespace Fine.OPCDaClient9000.Util
                 }
                 else
                 {
-                    LogUtil.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "错误信息", "批量写入---失败");
+                    LogUtil.WriteLog( "错误信息", "批量写入---失败");
                     Rlst[i].IsSetSuccessful = false;
                     Rlst[i].Message = "写入失败";
                     rlt_All.Sucessful = false;

+ 2 - 0
Fine.OPCDaClient9000/Form1.cs

@@ -212,6 +212,8 @@ namespace Fine.OPCDaClient9000
             form.Show();
         }
 
+       
+
         //private void LoadLog() {
         //    richTextBox1.Text = Read();
         //}