Browse Source

新增扩展方法;
新增高并发日志组件

懒得勤快 8 years ago
parent
commit
9b220e6734

+ 2 - 17
Masuit.Tools.sln

@@ -1,19 +1,12 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.25420.1
+# Visual Studio 15
+VisualStudioVersion = 15.0.26430.15
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools", "Masuit.Tools\Masuit.Tools.csproj", "{275D5A0D-C49C-497E-A4B5-F40285C2495F}"
 EndProject
-Project("{7CF6DF6D-3B04-46F8-A40B-537D21BCA0B4}") = "Masuit.Tools帮助文档", "Masuit.Tools帮助文档\Masuit.Tools帮助文档.shfbproj", "{9960A20A-7BAD-4125-B93C-BCA3906EBCC1}"
-	ProjectSection(ProjectDependencies) = postProject
-		{275D5A0D-C49C-497E-A4B5-F40285C2495F} = {275D5A0D-C49C-497E-A4B5-F40285C2495F}
-	EndProjectSection
-EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{91954D82-A5FC-417B-8FD9-D9C165C8F16C}"
 EndProject
-Project("{FF286327-C783-4F7A-AB73-9BCBAD0D4460}") = "MasuitToolsNugetPackage", "MasuitToolsNugetPackage\MasuitToolsNugetPackage.nuproj", "{EB6CC774-090E-4102-A8B2-D8E11EBEBDD7}"
-EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -24,18 +17,10 @@ Global
 		{275D5A0D-C49C-497E-A4B5-F40285C2495F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{275D5A0D-C49C-497E-A4B5-F40285C2495F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{275D5A0D-C49C-497E-A4B5-F40285C2495F}.Release|Any CPU.Build.0 = Release|Any CPU
-		{9960A20A-7BAD-4125-B93C-BCA3906EBCC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{9960A20A-7BAD-4125-B93C-BCA3906EBCC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{9960A20A-7BAD-4125-B93C-BCA3906EBCC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{9960A20A-7BAD-4125-B93C-BCA3906EBCC1}.Release|Any CPU.Build.0 = Release|Any CPU
 		{91954D82-A5FC-417B-8FD9-D9C165C8F16C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{91954D82-A5FC-417B-8FD9-D9C165C8F16C}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{91954D82-A5FC-417B-8FD9-D9C165C8F16C}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{91954D82-A5FC-417B-8FD9-D9C165C8F16C}.Release|Any CPU.Build.0 = Release|Any CPU
-		{EB6CC774-090E-4102-A8B2-D8E11EBEBDD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{EB6CC774-090E-4102-A8B2-D8E11EBEBDD7}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{EB6CC774-090E-4102-A8B2-D8E11EBEBDD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{EB6CC774-090E-4102-A8B2-D8E11EBEBDD7}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 3 - 3
Masuit.Tools/DateTimeExt/TimeHelper.cs

@@ -51,8 +51,8 @@ namespace Masuit.Tools.DateTimeExt
             switch (month)
             {
                 case 1:
-                    firstDay = System.DateTime.Now.ToString(year + "-0" + month + "-01");
-                    lastDay = System.DateTime.Now.ToString(year + "-0" + month + "-31");
+                    firstDay = DateTime.Now.ToString($"{year}-0{month}-01");
+                    lastDay = System.DateTime.Now.ToString($"{year}-0{month}-31");
                     break;
                 case 2:
                     firstDay = System.DateTime.Now.ToString(year + "-0" + month + "-01");
@@ -98,7 +98,7 @@ namespace Masuit.Tools.DateTimeExt
                     lastDay = System.DateTime.Now.ToString(year + "-" + month + "-30");
                     break;
                 default:
-                    firstDay = System.DateTime.Now.ToString(year + "-" + month + "-01");
+                    firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                     lastDay = System.DateTime.Now.ToString(year + "-" + month + "-31");
                     break;
             }

+ 301 - 106
Masuit.Tools/Strings/StringExt.cs → Masuit.Tools/Extensions.cs

@@ -1,13 +1,246 @@
 using System;
+using System.Collections.Generic;
 using System.Text.RegularExpressions;
 
-namespace Masuit.Tools.Strings
+namespace Masuit.Tools
 {
     /// <summary>
-    /// 字符串扩展类
+    /// 扩展方法
     /// </summary>
-    public static class StringExt
+    public static class Extensions
     {
+        /// <summary>
+        /// 遍历数组
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        public static void ForEach(this object[] objs, Action<object> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历IEnumerable
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        public static void ForEach(this IEnumerable<dynamic> objs, Action<object> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历集合
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        public static void ForEach(this IList<dynamic> objs, Action<object> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历数组
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        public static void ForEach<T>(this T[] objs, Action<T> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历IEnumerable
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        public static void ForEach<T>(this IEnumerable<T> objs, Action<T> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        public static void ForEach<T>(this IList<T> objs, Action<T> action)
+        {
+            foreach (var o in objs)
+            {
+                action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历数组并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this object[] objs, Func<object, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历IEnumerable并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this IEnumerable<dynamic> objs, Func<object, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历List并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this IList<dynamic> objs, Func<object, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+
+        /// <summary>
+        /// 遍历数组并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this T[] objs, Func<T, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历IEnumerable并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> objs, Func<T, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+        /// <summary>
+        /// 遍历List并返回一个新的List
+        /// </summary>
+        /// <param name="objs"></param>
+        /// <param name="action">回调方法</param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        public static IEnumerable<T> ForEach<T>(this IList<T> objs, Func<T, T> action)
+        {
+            foreach (var o in objs)
+            {
+                yield return action(o);
+            }
+        }
+
+        /// <summary>
+        /// 映射到目标类型(浅克隆)
+        /// </summary>
+        /// <param name="source">源</param>
+        /// <typeparam name="TDestination">目标类型</typeparam>
+        /// <returns>目标类型</returns>
+        public static TDestination MapTo<TDestination>(this object source) where TDestination : new()
+        {
+            TDestination dest = new TDestination();
+            dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source)); });
+            return dest;
+        }
+
+        /// <summary>
+        /// 映射到目标类型的集合
+        /// </summary>
+        /// <param name="source">源</param>
+        /// <typeparam name="TDestination">目标类型</typeparam>
+        /// <returns>目标类型集合</returns>
+        public static IEnumerable<TDestination> ToList<TDestination>(this object[] source) where TDestination : new()
+        {
+            foreach (var o in source)
+            {
+                var dest = new TDestination();
+                dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
+                yield return dest;
+            }
+        }
+
+        /// <summary>
+        /// 映射到目标类型的集合
+        /// </summary>
+        /// <param name="source">源</param>
+        /// <typeparam name="TDestination">目标类型</typeparam>
+        /// <returns>目标类型集合</returns>
+        public static IEnumerable<TDestination> ToList<TDestination>(this IList<dynamic> source) where TDestination : new()
+        {
+            foreach (var o in source)
+            {
+                var dest = new TDestination();
+                dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
+                yield return dest;
+            }
+        }
+
+        /// <summary>
+        /// 映射到目标类型的集合
+        /// </summary>
+        /// <param name="source">源</param>
+        /// <typeparam name="TDestination">目标类型</typeparam>
+        /// <returns>目标类型集合</returns>
+        public static IEnumerable<TDestination> ToList<TDestination>(this IEnumerable<dynamic> source) where TDestination : new()
+        {
+            foreach (var o in source)
+            {
+                var dest = new TDestination();
+                dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
+                yield return dest;
+            }
+        }
+
         #region UBB转HTML
 
         /// <summary>
@@ -55,8 +288,7 @@ namespace Masuit.Tools.Strings
 
             //处理换行,在每个新行的前面添加两个全角空格
             r = new Regex(@"(\r\n((&nbsp;)| )+)(?<正文>\S+)", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<BR>  " + m.Groups["正文"]);
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<BR>  " + m.Groups["正文"]);
             //处理换行,在每个新行的前面添加两个全角空格
             ubbStr = ubbStr.Replace("\r\n", "<BR>");
 
@@ -65,24 +297,21 @@ namespace Masuit.Tools.Strings
             #region 处[b][/b]标记
 
             r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2] + "</B>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2] + "</B>");
 
             #endregion
 
             #region 处[i][/i]标记
 
             r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2] + "</I>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2] + "</I>");
 
             #endregion
 
             #region 处[u][/u]标记
 
             r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2] + "</U>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2] + "</U>");
 
             #endregion
 
@@ -90,8 +319,7 @@ namespace Masuit.Tools.Strings
 
             //处[p][/p]标记
             r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3] + "</P>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3] + "</P>");
 
             #endregion
 
@@ -99,8 +327,7 @@ namespace Masuit.Tools.Strings
 
             //处[sup][/sup]标记
             r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2] + "</SUP>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2] + "</SUP>");
 
             #endregion
 
@@ -108,8 +335,7 @@ namespace Masuit.Tools.Strings
 
             //处[sub][/sub]标记
             r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2] + "</SUB>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2] + "</SUB>");
 
             #endregion
 
@@ -119,8 +345,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[2] + "</A>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[2] + "</A>");
             }
 
             #endregion
@@ -131,9 +356,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">"
-                   + m.Groups[3] + "</A>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[3] + "</A>");
             }
 
             #endregion
@@ -144,9 +367,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" +
-                   m.Groups[2] + "</A>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[2] + "</A>");
             }
 
             #endregion
@@ -157,9 +378,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" +
-                   m.Groups[3] + "</A>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[3] + "</A>");
             }
 
             #endregion
@@ -170,9 +389,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<FONT SIZE=" + m.Groups[2] + ">" +
-                   m.Groups[3] + "</FONT>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT SIZE=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
             }
 
             #endregion
@@ -183,9 +400,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<FONT COLOR=" + m.Groups[2] + ">" +
-                   m.Groups[3] + "</FONT>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT COLOR=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
             }
 
             #endregion
@@ -196,9 +411,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<FONT FACE=" + m.Groups[2] + ">" +
-                   m.Groups[3] + "</FONT>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT FACE=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
             }
 
             #endregion
@@ -209,8 +422,7 @@ namespace Masuit.Tools.Strings
             r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups[1] + "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1] + "\"></A>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups[1] + "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1] + "\"></A>");
             }
 
             #endregion
@@ -221,9 +433,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<P align=" + m.Groups[2] + ">" +
-                   m.Groups[3] + "</P>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<P align=" + m.Groups[2] + ">" + m.Groups[3] + "</P>");
             }
 
             #endregion
@@ -234,9 +444,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<H" + m.Groups[2] + ">" +
-                   m.Groups[3] + "</H" + m.Groups[2] + ">");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<H" + m.Groups[2] + ">" + m.Groups[3] + "</H" + m.Groups[2] + ">");
             }
 
             #endregion
@@ -247,14 +455,11 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                string strLI = m.Groups[5].ToString();
-                Regex rLI = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
-                Match mLI;
-                for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
-                    strLI = strLI.Replace(mLI.Groups[0].ToString(), "<LI>" + mLI.Groups[1]);
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                    "<UL TYPE=\"" + m.Groups[3] + "\"><B>" + m.Groups[4] + "</B>" +
-                    strLI + "</UL>");
+                string strLi = m.Groups[5].ToString();
+                Regex rLi = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
+                Match mLi;
+                for (mLi = rLi.Match(strLi); mLi.Success; mLi = mLi.NextMatch()) strLi = strLi.Replace(mLi.Groups[0].ToString(), "<LI>" + mLi.Groups[1]);
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<UL TYPE=\"" + m.Groups[3] + "\"><B>" + m.Groups[4] + "</B>" + strLi + "</UL>");
             }
 
             #endregion
@@ -265,8 +470,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/SHADOW\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<TABLE WIDTH=" + m.Groups[2] + "STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3] + ",STRENGTH=" + m.Groups[4] + ")>" + m.Groups[5] + "</TABLE>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2] + "STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3] + ",STRENGTH=" + m.Groups[4] + ")>" + m.Groups[5] + "</TABLE>");
             }
 
             #endregion
@@ -277,9 +481,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<TABLE WIDTH=" + m.Groups[2] + "  STYLE=FILTER:GLOW(COLOR=" + m.Groups[3] + ", STRENGTH=" + m.Groups[4] + ")>" +
-                   m.Groups[5] + "</TABLE>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2] + "  STYLE=FILTER:GLOW(COLOR=" + m.Groups[3] + ", STRENGTH=" + m.Groups[4] + ")>" + m.Groups[5] + "</TABLE>");
             }
 
             #endregion
@@ -287,24 +489,21 @@ namespace Masuit.Tools.Strings
             #region 处[center][/center]标记
 
             r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2] + "</CENTER>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2] + "</CENTER>");
 
             #endregion
 
             #region 处[ IMG][ /IMG]标记
 
             r = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<br><a onfocus=this.blur() href=" + m.Groups[2] + "://" + m.Groups[3] + " target=_blank><IMG SRC=" + m.Groups[2] + "://" + m.Groups[3] + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<br><a onfocus=this.blur() href=" + m.Groups[2] + "://" + m.Groups[3] + " target=_blank><IMG SRC=" + m.Groups[2] + "://" + m.Groups[3] + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
 
             #endregion
 
             #region 处[em]标记
 
             r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<img src=pic/em" + m.Groups[2] + ".gif border=0 align=middle>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<img src=pic/em" + m.Groups[2] + ".gif border=0 align=middle>");
 
             #endregion
 
@@ -314,8 +513,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<a href=" + m.Groups[4] + " TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=movie VALUE=" + m.Groups[4] + "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + m.Groups[4] + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + m.Groups[2] + " height=" + m.Groups[3] + ">" + m.Groups[4] + "</embed></OBJECT>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<a href=" + m.Groups[4] + " TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=movie VALUE=" + m.Groups[4] + "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + m.Groups[4] + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + m.Groups[2] + " height=" + m.Groups[3] + ">" + m.Groups[4] + "</embed></OBJECT>");
             }
 
             #endregion
@@ -326,8 +524,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><param name=src value=" + m.Groups[4] + "><embed src=" + m.Groups[4] + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><param name=src value=" + m.Groups[4] + "><embed src=" + m.Groups[4] + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
             }
 
             #endregion
@@ -338,8 +535,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[2] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[2] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
             }
 
             #endregion
@@ -350,8 +546,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2] + " height=" + m.Groups[3] + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + m.Groups[4] + "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + m.Groups[4] + "  width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2] + " height=" + m.Groups[3] + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + m.Groups[4] + "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + m.Groups[4] + "  width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
             }
 
             #endregion
@@ -362,8 +557,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<embed src=" + m.Groups[4] + " width=" + m.Groups[2] + " height=" + m.Groups[3] + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<embed src=" + m.Groups[4] + " width=" + m.Groups[2] + " height=" + m.Groups[3] + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
             }
 
             #endregion
@@ -371,24 +565,21 @@ namespace Masuit.Tools.Strings
             #region 处[QUOTE][/QUOTE]标记
 
             r = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<div style='border:#CCCCCC 1px dashed; width:94%; color:#999999; padding:3px; background:#F8F8F8'>" + m.Groups[2] + "</div><br /> ");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<div style='border:#CCCCCC 1px dashed; width:94%; color:#999999; padding:3px; background:#F8F8F8'>" + m.Groups[2] + "</div><br /> ");
 
             #endregion
 
             #region 处[move][/move]标记
 
             r = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
 
             #endregion
 
             #region 处[FLY][/FLY]标记
 
             r = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase);
-            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
+            for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
 
             #endregion
 
@@ -398,8 +589,7 @@ namespace Masuit.Tools.Strings
             r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
             for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
             {
-                ubbStr = ubbStr.Replace(m.Groups[0].ToString(),
-                   "<img src=\"" + m.Groups[2] + "\" border=0 align=middle><br>");
+                ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<img src=\"" + m.Groups[2] + "\" border=0 align=middle><br>");
             }
 
             #endregion
@@ -571,8 +761,7 @@ namespace Masuit.Tools.Strings
         /// <returns>UBB代码</returns>
         public static string HtmltoUBB(this string chr)
         {
-            if (chr == null)
-                return "";
+            if (chr == null) return "";
             chr = chr.Replace("&lt", "<");
             chr = chr.Replace("&gt", ">");
             chr = chr.Replace("<br/>", " ");
@@ -671,6 +860,7 @@ namespace Masuit.Tools.Strings
         }
 
         #region 检测字符串中是否包含列表中的关键词
+
         /// <summary>
         /// 检测字符串中是否包含列表中的关键词
         /// </summary>
@@ -741,9 +931,11 @@ namespace Masuit.Tools.Strings
             isMatch = match.Success;
             return isMatch ? match : null;
         }
+
         #endregion
 
         #region 权威校验身份证号码
+
         /// <summary>
         /// 根据GB11643-1999标准权威校验中国身份证号码的合法性
         /// </summary>
@@ -753,64 +945,65 @@ namespace Masuit.Tools.Strings
         {
             if (s.Length == 18)
             {
-                long n = 0;
-                if (long.TryParse(s.Remove(17), out n) == false
-                    || n < Math.Pow(10, 16) || long.TryParse(s.Replace('x', '0').Replace('X', '0'), out n) == false)
+                long n;
+                if (long.TryParse(s.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(s.Replace('x', '0').Replace('X', '0'), out n) == false)
                 {
-                    return false;//数字验证  
+                    return false; //数字验证  
                 }
                 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
-                if (address.IndexOf(s.Remove(2)) == -1)
+                if (address.IndexOf(s.Remove(2), StringComparison.Ordinal) == -1)
                 {
-                    return false;//省份验证  
+                    return false; //省份验证  
                 }
                 string birth = s.Substring(6, 8).Insert(6, "-").Insert(4, "-");
-                DateTime time = new DateTime();
-                if (DateTime.TryParse(birth, out time) == false)
+                DateTime time;
+                if (!DateTime.TryParse(birth, out time))
                 {
-                    return false;//生日验证  
+                    return false; //生日验证  
                 }
                 string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
-                string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
-                char[] Ai = s.Remove(17).ToCharArray();
+                string[] wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
+                char[] ai = s.Remove(17).ToCharArray();
                 int sum = 0;
                 for (int i = 0; i < 17; i++)
                 {
-                    sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
+                    sum += wi[i].ToInt32() * ai[i].ToString().ToInt32();
                 }
-                int y = -1;
+                int y;
                 Math.DivRem(sum, 11, out y);
                 if (arrVarifyCode[y] != s.Substring(17, 1).ToLower())
                 {
-                    return false;//校验码验证  
+                    return false; //校验码验证  
                 }
-                return true;//符合GB11643-1999标准  
+                return true; //符合GB11643-1999标准  
             }
             if (s.Length == 15)
             {
-                long n = 0;
+                long n;
                 if (long.TryParse(s, out n) == false || n < Math.Pow(10, 14))
                 {
-                    return false;//数字验证  
+                    return false; //数字验证  
                 }
                 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
-                if (address.IndexOf(s.Remove(2)) == -1)
+                if (address.IndexOf(s.Remove(2), StringComparison.Ordinal) == -1)
                 {
-                    return false;//省份验证  
+                    return false; //省份验证  
                 }
                 string birth = s.Substring(6, 6).Insert(4, "-").Insert(2, "-");
-                DateTime time = new DateTime();
+                DateTime time;
                 if (DateTime.TryParse(birth, out time) == false)
                 {
-                    return false;//生日验证  
+                    return false; //生日验证  
                 }
                 return true;
             }
             return false;
         }
+
         #endregion
 
         #region 校验IP地址的合法性
+
         /// <summary>
         /// 校验IP地址的正确性,同时支持IPv4和IPv6
         /// </summary>
@@ -842,9 +1035,11 @@ namespace Masuit.Tools.Strings
             }
             return isMatch ? match : null;
         }
+
         #endregion
 
         #region 校验手机号码的正确性
+
         /// <summary>
         /// 匹配手机号码
         /// </summary>
@@ -857,7 +1052,7 @@ namespace Masuit.Tools.Strings
             isMatch = match.Success;
             return isMatch ? match : null;
         }
-        #endregion
 
+        #endregion
     }
 }

+ 212 - 0
Masuit.Tools/Logging/LogManager.cs

@@ -0,0 +1,212 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+using System.Threading;
+using System.Threading.Tasks;
+using static System.DateTime;
+
+namespace Masuit.Tools.Logging
+{
+    /// <summary>
+    /// 日志组件
+    /// </summary>
+    public class LogManager
+    {
+        static readonly ConcurrentQueue<Tuple<string, string>> LogQueue = new ConcurrentQueue<Tuple<string, string>>();
+
+        static readonly Task WriteTask;
+
+        static LogManager()
+        {
+            WriteTask = new Task(obj =>
+            {
+                while (true)
+                {
+                    Pause.WaitOne(1000, true);
+                    List<string[]> temp = new List<string[]>();
+                    foreach (var logItem in LogQueue)
+                    {
+                        string logPath = logItem.Item1;
+                        string logMergeContent = String.Concat(logItem.Item2, Environment.NewLine, "-----------------------------------------------------------", Environment.NewLine);
+                        string[] logArr = temp.FirstOrDefault(d => d[0].Equals(logPath));
+                        if (logArr != null)
+                        {
+                            logArr[1] = string.Concat(logArr[1], logMergeContent);
+                        }
+                        else
+                        {
+                            logArr = new[] { logPath, logMergeContent };
+                            temp.Add(logArr);
+                        }
+                        LogQueue.TryDequeue(out Tuple<string, string> val);
+                    }
+                    foreach (string[] item in temp)
+                    {
+                        WriteText(item[0], item[1]);
+                    }
+                }
+            }, null, TaskCreationOptions.LongRunning);
+            WriteTask.Start();
+        }
+
+        private static AutoResetEvent Pause = new AutoResetEvent(false);
+
+        /// <summary>
+        /// 日志存放目录
+        /// </summary>
+        public static string LogDirectory { get; set; } = AppDomain.CurrentDomain.BaseDirectory;
+
+        /// <summary>
+        /// 写入Info级别的日志
+        /// </summary>
+        /// <param name="source"></param>
+        /// <param name="info"></param>
+        public static void Info(string source, string info)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}   {source}  {info}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入error级别日志
+        /// </summary>
+        /// <param name="error">异常对象</param>
+        public static void Error(Exception error)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {error.Source}  {error.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {error.Source}  {error.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入error级别日志
+        /// </summary>
+        /// <param name="source">异常源的类型</param>
+        /// <param name="error">异常对象</param>
+        public static void Error(Type source, Exception error)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source.FullName}  {error.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source.FullName}  {error.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入error级别日志
+        /// </summary>
+        /// <param name="source">异常源的类型</param>
+        /// <param name="error">异常对象</param>
+        public static void Error(string source, Exception error)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source}  {error.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source}  {error.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入debug级别日志
+        /// </summary>
+        /// <param name="source">异常源的类型</param>
+        /// <param name="debug">异常对象</param>
+        public static void Debug(string source, string debug)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(debug).ToUpper()}   {source}  {debug}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入fatal级别日志
+        /// </summary>
+        /// <param name="fatal">异常对象</param>
+        public static void Fatal(Exception fatal)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {fatal.Source}  {fatal.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {fatal.Source}  {fatal.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入fatal级别日志
+        /// </summary>
+        /// <param name="source">异常源的类型</param>
+        /// <param name="fatal">异常对象</param>
+        public static void Fatal(Type source, Exception fatal)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source.FullName}  {fatal.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source.FullName}  {fatal.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        /// <summary>
+        /// 写入fatal级别日志
+        /// </summary>
+        /// <param name="source">异常源的类型</param>
+        /// <param name="fatal">异常对象</param>
+        public static void Fatal(string source, Exception fatal)
+        {
+            string logPath = GetLogPath();
+            string logContent = $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source}  {fatal.Message}{Environment.NewLine}{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source}  {fatal.StackTrace}";
+            LogQueue.Enqueue(new Tuple<string, string>(logPath, logContent));
+        }
+
+        private static string GetLogPath()
+        {
+            string newFilePath;
+            String logDir = string.IsNullOrEmpty(LogDirectory) ? Path.Combine(Environment.CurrentDirectory, "logs") : LogDirectory;
+            if (!Directory.Exists(logDir))
+            {
+                Directory.CreateDirectory(logDir);
+            }
+            string extension = ".log";
+            string fileNameNotExt = String.Concat(Now.ToString("yyyyMMdd"));
+            string fileNamePattern = string.Concat(fileNameNotExt, "(*)", extension);
+            List<string> filePaths = Directory.GetFiles(logDir, fileNamePattern, SearchOption.TopDirectoryOnly).ToList();
+
+            if (filePaths.Count > 0)
+            {
+                int fileMaxLen = filePaths.Max(d => d.Length);
+                string lastFilePath = filePaths.Where(d => d.Length == fileMaxLen).OrderByDescending(d => d).FirstOrDefault();
+                if (new FileInfo(lastFilePath).Length > 1 * 1024 * 1024)
+                {
+                    string no = new Regex(@"(?is)(?<=\()(.*)(?=\))").Match(Path.GetFileName(lastFilePath)).Value;
+                    int tempno;
+                    bool parse = int.TryParse(no, out tempno);
+                    string formatno = $"({(parse ? (tempno + 1) : tempno)})";
+                    string newFileName = String.Concat(fileNameNotExt, formatno, extension);
+                    newFilePath = Path.Combine(logDir, newFileName);
+                }
+                else
+                {
+                    newFilePath = lastFilePath;
+                }
+            }
+            else
+            {
+                string newFileName = String.Concat(fileNameNotExt, $"({0})", extension);
+                newFilePath = Path.Combine(logDir, newFileName);
+            }
+            return newFilePath;
+        }
+
+        private static void WriteText(string logPath, string logContent)
+        {
+            try
+            {
+                if (!File.Exists(logPath))
+                {
+                    File.CreateText(logPath).Close();
+                }
+                StreamWriter sw = File.AppendText(logPath);
+                sw.Write(logContent);
+                sw.Close();
+            }
+            catch (Exception)
+            {
+            }
+        }
+    }
+}

+ 6 - 6
Masuit.Tools/Masuit.Tools.csproj

@@ -48,13 +48,12 @@
       <HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
       <Private>True</Private>
     </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.2\lib\net45\Newtonsoft.Json.dll</HintPath>
-      <Private>True</Private>
+      <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
     </Reference>
-    <Reference Include="StackExchange.Redis, Version=1.2.1.0, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\StackExchange.Redis.1.2.1\lib\net45\StackExchange.Redis.dll</HintPath>
-      <Private>True</Private>
+    <Reference Include="StackExchange.Redis, Version=1.2.4.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\StackExchange.Redis.1.2.4\lib\net45\StackExchange.Redis.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Configuration" />
@@ -77,6 +76,7 @@
     <Compile Include="DateTimeExt\DateTimeHelper.cs" />
     <Compile Include="DateTimeExt\DateUtil.cs" />
     <Compile Include="DateTimeExt\TimeHelper.cs" />
+    <Compile Include="Extensions.cs" />
     <Compile Include="Files\Compress.cs" />
     <Compile Include="Files\ExtensionAttach.cs" />
     <Compile Include="Files\FileTree.cs" />
@@ -85,6 +85,7 @@
     <Compile Include="Html\HtmlPager.cs" />
     <Compile Include="Html\HtmlTools.cs" />
     <Compile Include="Html\ListBuilder.cs" />
+    <Compile Include="Logging\LogManager.cs" />
     <Compile Include="Net\CacheHelper.cs" />
     <Compile Include="Net\CookieHelper.cs" />
     <Compile Include="Net\SocketClient.cs" />
@@ -104,7 +105,6 @@
     <Compile Include="Security\HashEncode.cs" />
     <Compile Include="Security\RSACryption.cs" />
     <Compile Include="Objects\ObjectExt.cs" />
-    <Compile Include="Strings\StringExt.cs" />
     <Compile Include="Strings\ValidateCode.cs" />
     <Compile Include="Net\WebExtension.cs" />
     <Compile Include="Win32\Windows.cs" />

+ 3 - 0
Masuit.Tools/Models/IPData.cs

@@ -2,6 +2,9 @@
 
 namespace Masuit.Tools.Models
 {
+    /// <summary>
+    /// IP地址信息
+    /// </summary>
     public class IPData
     {
         /// <summary>

+ 78 - 57
Masuit.Tools/Win32/Windows.cs

@@ -10,7 +10,7 @@ namespace Masuit.Tools.Win32
     /// <summary>
     /// Windows系统的系列方法
     /// </summary>
-    public class Windows
+    public static class Windows
     {
         /// <summary>
         /// 跨平台调用C++的方法
@@ -38,11 +38,11 @@ namespace Masuit.Tools.Win32
         {
             GC.Collect();
             GC.WaitForPendingFinalizers();
-            foreach (Process p in Process.GetProcesses())
+            foreach(Process p in Process.GetProcesses())
             {
-                using (p)
+                using(p)
                 {
-                    if ((p.ProcessName.Equals("System")) && (p.ProcessName.Equals("Idle")))
+                    if((p.ProcessName.Equals("System")) && (p.ProcessName.Equals("Idle")))
                     {
                         //两个系统的关键进程,不整理
                         continue;
@@ -51,7 +51,7 @@ namespace Masuit.Tools.Win32
                     {
                         EmptyWorkingSet(p.Handle);
                     }
-                    catch (Exception e)
+                    catch
                     {
                         // ignored
                     }
@@ -78,47 +78,48 @@ namespace Masuit.Tools.Win32
         /// <summary>
         /// CPU主频
         /// </summary>
-        public string[] CpuMhz;//CPU频率  单位:hz
+        public string[] CpuMhz; //CPU频率  单位:hz
 
         /// <summary>
         /// mac地址
         /// </summary>
-        public string MacAddress;//计算机的MAC地址
+        public string MacAddress; //计算机的MAC地址
 
         /// <summary>
         /// 硬盘ID
         /// </summary>
-        public string DiskId;//硬盘的ID
+        public string DiskId; //硬盘的ID
 
         /// <summary>
         /// 硬盘大小
         /// </summary>
-        public string DiskSize;//硬盘大小  单位:bytes
+        public string DiskSize; //硬盘大小  单位:bytes
 
         /// <summary>
         /// IP地址
         /// </summary>
-        public string IpAddress;//计算机的IP地址
+        public string IpAddress; //计算机的IP地址
 
         /// <summary>
         /// 系统当前登录用户
         /// </summary>
-        public string LoginUserName;//操作系统登录用户名
+        public string LoginUserName; //操作系统登录用户名
 
         /// <summary>
         /// 计算机名
         /// </summary>
-        public string ComputerName;//计算机名
+        public string ComputerName; //计算机名
 
         /// <summary>
         /// 操作系统架构
         /// </summary>
-        public string SystemType;//系统类型
+        public string SystemType; //系统类型
 
         /// <summary>
         /// 物理内存,单位MB
         /// </summary>
         public string TotalPhysicalMemory; //总共的内存  单位:M 
+
         private static WindowsServer _instance;
 
         /// <summary>
@@ -128,11 +129,11 @@ namespace Masuit.Tools.Win32
         {
             get
             {
-                if (_instance == null)
-                    _instance = new WindowsServer();
+                if(_instance == null) _instance = new WindowsServer();
                 return _instance;
             }
         }
+
         /// <summary>
         /// 构造函数
         /// </summary>
@@ -150,17 +151,18 @@ namespace Masuit.Tools.Win32
             TotalPhysicalMemory = GetTotalPhysicalMemory();
             ComputerName = GetComputerName();
         }
+
         string GetCpuID()
         {
             try
             {
                 //获取CPU序列号代码 
-                string cpuInfo = " ";//cpu序列号 
-                using (var mc = new ManagementClass("Win32_Processor"))
+                string cpuInfo = " "; //cpu序列号 
+                using(var mc = new ManagementClass("Win32_Processor"))
                 {
-                    foreach (ManagementObject mo in mc.GetInstances())
+                    foreach(ManagementObject mo in mc.GetInstances())
                     {
-                        using (mo)
+                        using(mo)
                         {
                             cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                         }
@@ -173,13 +175,18 @@ namespace Masuit.Tools.Win32
                 return "unknow ";
             }
         }
+
+        /// <summary>
+        /// 获取CPU个数
+        /// </summary>
+        /// <returns></returns>
         public static int GetCpuCount()
         {
             try
             {
-                using (var mCpu = new ManagementClass("Win32_Processor"))
+                using(var mCpu = new ManagementClass("Win32_Processor"))
                 {
-                    using (ManagementObjectCollection cpus = mCpu.GetInstances())
+                    using(ManagementObjectCollection cpus = mCpu.GetInstances())
                     {
                         return cpus.Count;
                     }
@@ -191,19 +198,24 @@ namespace Masuit.Tools.Win32
             }
             return -1;
         }
+
+        /// <summary>
+        /// 获取CPU主频
+        /// </summary>
+        /// <returns></returns>
         public static string[] GetCpuMHZ()
         {
-            using (var mc = new ManagementClass("Win32_Processor"))
+            using(var mc = new ManagementClass("Win32_Processor"))
             {
-                using (ManagementObjectCollection cpus = mc.GetInstances())
+                using(ManagementObjectCollection cpus = mc.GetInstances())
                 {
                     var MHz = new string[cpus.Count];
                     int c = 0;
-                    using (var mySearch = new ManagementObjectSearcher("select * from Win32_Processor"))
+                    using(var mySearch = new ManagementObjectSearcher("select * from Win32_Processor"))
                     {
-                        foreach (ManagementObject mo in mySearch.Get())
+                        foreach(ManagementObject mo in mySearch.Get())
                         {
-                            using (mo)
+                            using(mo)
                             {
                                 MHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
                                 c++;
@@ -214,13 +226,18 @@ namespace Masuit.Tools.Win32
                 }
             }
         }
+
+        /// <summary>
+        /// 获取磁盘大小
+        /// </summary>
+        /// <returns></returns>
         public static string GetSizeOfDisk()
         {
-            using (var mc = new ManagementClass("Win32_DiskDrive"))
+            using(var mc = new ManagementClass("Win32_DiskDrive"))
             {
-                foreach (ManagementObject m in mc.GetInstances())
+                foreach(ManagementObject m in mc.GetInstances())
                 {
-                    using (m)
+                    using(m)
                     {
                         return m.Properties["Size"].Value.ToString();
                     }
@@ -228,19 +245,20 @@ namespace Masuit.Tools.Win32
             }
             return "-1";
         }
+
         string GetMacAddress()
         {
             try
             {
                 //获取网卡硬件地址 
                 string mac = " ";
-                using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
+                using(var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                 {
-                    foreach (ManagementObject mo in mc.GetInstances())
+                    foreach(ManagementObject mo in mc.GetInstances())
                     {
-                        using (mo)
+                        using(mo)
                         {
-                            if ((bool)mo["IPEnabled"] == true)
+                            if((bool) mo["IPEnabled"] == true)
                             {
                                 mac = mo["MacAddress"].ToString();
                                 break;
@@ -255,23 +273,24 @@ namespace Masuit.Tools.Win32
                 return "unknow ";
             }
         }
+
         string GetIPAddress()
         {
             try
             {
                 //获取IP地址 
                 string st = String.Empty;
-                using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
+                using(var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                 {
-                    foreach (ManagementObject mo in mc.GetInstances())
+                    foreach(ManagementObject mo in mc.GetInstances())
                     {
-                        using (mo)
+                        using(mo)
                         {
-                            if ((bool)mo["IPEnabled"] == true)
+                            if((bool) mo["IPEnabled"] == true)
                             {
                                 //st=mo[ "IpAddress "].ToString(); 
                                 Array ar;
-                                ar = (Array)(mo.Properties["IpAddress"].Value);
+                                ar = (Array) (mo.Properties["IpAddress"].Value);
                                 st = ar.GetValue(0).ToString();
                                 break;
                             }
@@ -287,21 +306,21 @@ namespace Masuit.Tools.Win32
             finally
             {
             }
-
         }
+
         string GetDiskID()
         {
             try
             {
                 //获取硬盘ID 
                 string HDid = String.Empty;
-                using (var mc = new ManagementClass("Win32_DiskDrive"))
+                using(var mc = new ManagementClass("Win32_DiskDrive"))
                 {
-                    foreach (ManagementObject mo in mc.GetInstances())
+                    foreach(ManagementObject mo in mc.GetInstances())
                     {
-                        using (mo)
+                        using(mo)
                         {
-                            HDid = (string)mo.Properties["Model"].Value;
+                            HDid = (string) mo.Properties["Model"].Value;
                         }
                     }
                 }
@@ -312,6 +331,7 @@ namespace Masuit.Tools.Win32
                 return "unknow ";
             }
         }
+
         ///    <summary>  
         ///   操作系统的登录用户名 
         ///    </summary>  
@@ -321,11 +341,11 @@ namespace Masuit.Tools.Win32
             try
             {
                 string st = String.Empty;
-                using (var mc = new ManagementClass("Win32_ComputerSystem"))
+                using(var mc = new ManagementClass("Win32_ComputerSystem"))
                 {
-                    foreach (ManagementObject mo in mc.GetInstances())
+                    foreach(ManagementObject mo in mc.GetInstances())
                     {
-                        using (mo)
+                        using(mo)
                         {
                             st = mo["UserName"].ToString();
                         }
@@ -337,20 +357,20 @@ namespace Masuit.Tools.Win32
             {
                 return "unknow ";
             }
-
         }
+
         string GetSystemType()
         {
             try
             {
                 string st = String.Empty;
-                using (var mc = new ManagementClass("Win32_ComputerSystem"))
+                using(var mc = new ManagementClass("Win32_ComputerSystem"))
                 {
-                    foreach (var o in mc.GetInstances())
+                    foreach(var o in mc.GetInstances())
                     {
-                        using (o)
+                        using(o)
                         {
-                            var mo = (ManagementObject)o;
+                            var mo = (ManagementObject) o;
                             st = mo["SystemType"].ToString();
                         }
                     }
@@ -362,19 +382,19 @@ namespace Masuit.Tools.Win32
                 return "unknow ";
             }
         }
+
         string GetTotalPhysicalMemory()
         {
             try
             {
-
                 string st = String.Empty;
-                using (var mc = new ManagementClass("Win32_ComputerSystem"))
+                using(var mc = new ManagementClass("Win32_ComputerSystem"))
                 {
-                    using (ManagementObjectCollection moc = mc.GetInstances())
+                    using(ManagementObjectCollection moc = mc.GetInstances())
                     {
-                        foreach (var o in moc)
+                        foreach(var o in moc)
                         {
-                            var mo = (ManagementObject)o;
+                            var mo = (ManagementObject) o;
 
                             st = mo["TotalPhysicalMemory"].ToString();
                         }
@@ -387,6 +407,7 @@ namespace Masuit.Tools.Win32
                 return "unknow ";
             }
         }
+
         string GetComputerName()
         {
             try
@@ -399,4 +420,4 @@ namespace Masuit.Tools.Win32
             }
         }
     }
-}
+}

+ 2 - 2
Masuit.Tools/packages.config

@@ -2,7 +2,7 @@
 <packages>
   <package id="AngleSharp" version="0.9.9" targetFramework="net45" />
   <package id="HtmlSanitizer" version="3.4.156" targetFramework="net45" />
-  <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
+  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
   <package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
-  <package id="StackExchange.Redis" version="1.2.1" targetFramework="net45" />
+  <package id="StackExchange.Redis" version="1.2.4" targetFramework="net45" />
 </packages>

BIN
Masuit.Tools帮助文档/Help/Masuit.Tools帮助文档.chm


+ 1 - 1
Test/Program.cs

@@ -3,7 +3,7 @@ using Masuit.Tools.Win32;
 
 namespace Test
 {
-    class Program
+    internal static class Program
     {
         static void Main(string[] args)
         {