林豪 左 2 years ago
parent
commit
7dabcffa0d

+ 117 - 0
ServiceCenter/Extensions/TypeExtension.cs

@@ -254,6 +254,80 @@ namespace ServiceCenter.Extensions
         /// <returns></returns>
         internal static T ConvertTo<T>(this object value) => (T)typeof(T).FromObject(value);
 
+        public static string yyyy(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyy);
+        }
+
+        public static string yyyyMM(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMM);
+        }
+
+        public static string yyyyMMdd(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMMdd);
+        }
+
+        public static string yyyyMMddhh(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMMddhh);
+        }
+
+        public static string yyyyMMddhhmm(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMMddhhmm);
+        }
+
+        public static string yyyyMMddhhmmss(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmss);
+        }
+
+        public static string yyyyMMddhhmmssf(this DateTime time)
+        {
+            return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmssfffffff);
+        }
+
+        /// <summary>
+        ///  获取指定格式时间的字符串
+        /// </summary>
+        /// <param name="time">时间</param>
+        /// <param name="formatterEnum">类型</param>
+        /// <returns></returns>
+        public static string GetFormat(this DateTime time, GetFormatterEnum formatterEnum)
+        {
+            switch (formatterEnum)
+            {
+                case GetFormatterEnum.Default:
+                    return time.ToString();
+
+                case GetFormatterEnum.yyyy:
+                    return time.ToString("yyyy");
+
+                case GetFormatterEnum.yyyyMM:
+                    return time.ToString("yyyy-MM");
+
+                case GetFormatterEnum.yyyyMMdd:
+                    return time.ToString("yyyy-MM-dd");
+
+                case GetFormatterEnum.yyyyMMddhh:
+                    return time.ToString("yyyy-MM-dd hh");
+
+                case GetFormatterEnum.yyyyMMddhhmm:
+                    return time.ToString("yyyy-MM-dd hh:mm");
+
+                case GetFormatterEnum.yyyyMMddhhmmss:
+                    return time.ToString("yyyy-MM-dd hh:mm:ss");
+
+                case GetFormatterEnum.yyyyMMddhhmmssfffffff:
+                    return time.ToString("yyyy-MM-dd hh:mm:ss:fffffff");
+
+                default:
+                    return time.ToString();
+            }
+        }
+
         private static ConcurrentDictionary<Type, Func<string, object>> _dicFromObject = new ConcurrentDictionary<Type, Func<string, object>>();
 
         public static object FromObject(this Type targetType, object value, Encoding encoding = null)
@@ -468,4 +542,47 @@ namespace ServiceCenter.Extensions
             return sb.Append(">").ToString();
         }
     }
+
+    public enum GetFormatterEnum
+    {
+        /// <summary>
+        ///  默认类型
+        /// </summary>
+        Default = 0,
+
+        /// <summary>
+        ///  yyyy
+        /// </summary>
+        yyyy = 4,
+
+        /// <summary>
+        ///  yyyy-MM
+        /// </summary>
+        yyyyMM = 5,
+
+        /// <summary>
+        ///  yyyy-MM-dd
+        /// </summary>
+        yyyyMMdd = 6,
+
+        /// <summary>
+        ///  yyyy-MM-dd hh
+        /// </summary>
+        yyyyMMddhh = 7,
+
+        /// <summary>
+        ///  yyyy-MM-dd hh:mm
+        /// </summary>
+        yyyyMMddhhmm = 8,
+
+        /// <summary>
+        ///  yyyy-MM-dd hh:mm:ss
+        /// </summary>
+        yyyyMMddhhmmss = 9,
+
+        /// <summary>
+        ///  yyyy-MM-dd hh:mm:ss:fffffff
+        /// </summary>
+        yyyyMMddhhmmssfffffff = 10,
+    }
 }

+ 22 - 10
ServiceCenter/LogHub.cs

@@ -1,12 +1,12 @@
-using System.Collections.Concurrent;
-using System.IO;
-using WCS.Core;
+using Newtonsoft.Json;
+using ServiceCenter.Extensions;
+using ServiceCenter.Redis;
+using System.Collections.Concurrent;
 
 namespace ServiceCenter
 {
     public class LogHub
     {
-
         /// <summary>
         ///  日志队列
         /// </summary>
@@ -20,11 +20,26 @@ namespace ServiceCenter
         /// <param name="con">内容</param>
         public static void Publish(string path, string title, string con)
         {
-            Logs.Enqueue(new LogModel
+            RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
             {
                 path = path,
                 Title = title,
                 Con = con
+            }));
+        }
+
+        /// <summary>
+        ///   增加一条处理日志
+        /// </summary>
+        /// <param name="title">文件名</param>
+        /// <param name="con">内容</param>
+        public static void InterfaceProcessLog(string title, string con)
+        {
+            Logs.Enqueue(new LogModel
+            {
+                path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",
+                Title = $"{title}.txt",
+                Con = con
             });
         }
 
@@ -32,13 +47,12 @@ namespace ServiceCenter
         {
             while (true)
             {
-                foreach (var log in Logs)
+                var log = JsonConvert.DeserializeObject<LogModel>(RedisHub.Default.LPop("LogHub"));
+                if (log != null)
                 {
                     if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
                     File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
                 }
-                Logs.Clear();
-                Thread.Sleep(1000);
             }
         }
     }
@@ -49,7 +63,5 @@ namespace ServiceCenter
         public string Title { get; set; }
 
         public string Con { get; set; }
-
-
     }
 }

+ 3 - 2
ServiceCenter/WebApi/APICaller.cs

@@ -1,4 +1,5 @@
 using Newtonsoft.Json;
+using ServiceCenter.Extensions;
 using System.Collections.Concurrent;
 using System.Diagnostics;
 using System.Net;
@@ -79,7 +80,7 @@ namespace ServiceCenter.WebApi
 
         private static string HttpApi(string url, string jsonstr, string type)
         {
-            LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.ToString("yyyy-MM-dd")}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fffffff")}--接口{url}传参---------{JsonConvert.SerializeObject(jsonstr)}\n");
+            LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--接口{url}传参---------{JsonConvert.SerializeObject(jsonstr)}\n");
             var sw = new Stopwatch();
             sw.Start();
             Encoding encoding = Encoding.UTF8;
@@ -97,7 +98,7 @@ namespace ServiceCenter.WebApi
             {
                 var res = reader.ReadToEnd();
                 sw.Stop();
-                LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.ToString("yyyy-MM-dd")}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fffffff")}--接口{url}调用耗时{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}\n");
+                LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--接口{url}调用耗时:{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}\n");
                 return res;
             }
         }

+ 3 - 2
WCS.WorkEngineering/worlds/MainWorld.cs

@@ -1,4 +1,5 @@
 using ServiceCenter;
+using ServiceCenter.Extensions;
 using System.Collections.Concurrent;
 using System.ComponentModel;
 using WCS.Core;
@@ -58,8 +59,8 @@ namespace WCS.WorkEngineering.Worlds
                 var time = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:FF")}";
                 foreach (var log in Logs)
                 {
-                    var dir = $"D:\\WCSLogs\\{DateTime.Now.ToString("yyyy-MM-dd")}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
-                    var msg = $"{log.Time}--[{Thread.CurrentThread.ManagedThreadId}]--{log}\n";
+                    var dir = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
+                    var msg = $"{log.Time.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{log}\n";
                     LogHub.Publish(dir, $"{log.Log.Message.Split(":")[0]}.txt", msg);
                 }
             }