Browse Source

文件工具及日志工具修改

yuzhengyang 9 years ago
parent
commit
3b320b79df

+ 48 - 0
Fork.Net/Y.Utils.Net20/FileUtils/FileTool.cs

@@ -166,5 +166,53 @@ namespace Y.Utils.Net20.FileUtils
             }
             return result;
         }
+
+        public static long Size(string fileName)
+        {
+            FileInfo fi = new FileInfo(fileName);
+            return fi.Length;
+        }
+
+        public static string SizeFormat(string fileName)
+        {
+            return SizeFormat(Size(fileName));
+        }
+        public static string SizeFormat(long size)
+        {
+            string rs = "";
+            if (size > 1024 * 1024 * 1024)
+            {
+                rs = Math.Round((double)size / 1024 / 1024 / 1024, 2) + " GB";
+            }
+            else if (size > 1024 * 1024)
+            {
+                rs = Math.Round((double)size / 1024 / 1024, 2) + " MB";
+            }
+            else if (size > 1024)
+            {
+                rs = Math.Round((double)size / 1024, 2) + " KB";
+            }
+            else
+            {
+                rs = size + " B";
+            }
+            return rs;
+        }
+        public static string SizeConvert(string fileName, string unit)
+        {
+            return SizeConvert(Size(fileName), unit);
+        }
+        public static string SizeConvert(long size, string unit)
+        {
+            double rs = 0;
+            switch (unit)
+            {
+                case "B": rs = (double)size; break;
+                case "KB": rs = (double)size / 1024; break;
+                case "MB": rs = (double)size / 1024 / 1024; break;
+                case "GB": rs = (double)size / 1024 / 1024 / 1024; break;
+            }
+            return Math.Round(rs, 2).ToString();
+        }
     }
 }

+ 14 - 28
Fork.Net/Y.Utils.Net20/LogUtils/Log.cs

@@ -19,24 +19,10 @@ namespace Y.Utils.Net20.LogUtils
     {
         //输出的 Log 格式
         const string LogFormat = "{0}  {1}  {2}";
-        const string TimeFormat = "MM-dd HH:mm:ss.fff";
+        const string TimeFormat = "HH:mm:ss.fff";
 
         private static object LogFileLock = new object();
 
-        #region 输出类型
-        /// <summary>
-        /// 输出类型
-        /// </summary>
-        enum PrintType
-        {
-            v,//verbose 啰嗦的意思
-            d,//debug 调试的信息
-            i,//information 一般提示性的消息
-            w,//warning 警告
-            e,//error 错误信息
-        }
-        #endregion
-
         #region Console 开启/关闭 API
         [DllImport("kernel32.dll")]
         public static extern Boolean AllocConsole();
@@ -49,15 +35,15 @@ namespace Y.Utils.Net20.LogUtils
         /// </summary>
         /// <param name="type">输出类型</param>
         /// <returns></returns>
-        private static ConsoleColor GetColor(PrintType type)
+        private static ConsoleColor GetColor(LogType type)
         {
             switch (type)
             {
-                case PrintType.v: return ConsoleColor.Gray;
-                case PrintType.d: return ConsoleColor.Blue;
-                case PrintType.i: return ConsoleColor.Green;
-                case PrintType.w: return ConsoleColor.Yellow;
-                case PrintType.e: return ConsoleColor.Red;
+                case LogType.v: return ConsoleColor.Gray;
+                case LogType.d: return ConsoleColor.Blue;
+                case LogType.i: return ConsoleColor.Green;
+                case LogType.w: return ConsoleColor.Yellow;
+                case LogType.e: return ConsoleColor.Red;
                 default: return ConsoleColor.Gray;
             }
         }
@@ -68,7 +54,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="type">类型</param>
         /// <param name="tag">标记</param>
         /// <param name="message">消息</param>
-        private static void Write(PrintType type, string message)
+        private static void Write(LogType type, string message)
         {
             Console.ForegroundColor = GetColor(type);
             Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
@@ -76,7 +62,7 @@ namespace Y.Utils.Net20.LogUtils
         }
 
 
-        private static void WriteFile(PrintType type, string message)
+        private static void WriteFile(LogType type, string message)
         {
             lock (LogFileLock)
             {
@@ -98,7 +84,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="tag">可选:标记</param>
         public static void v<T>(T msg)
         {
-            Write(PrintType.v, msg.ToString());
+            Write(LogType.v, msg.ToString());
         }
         /// <summary>
         /// 输出 Debug (调试信息)
@@ -107,7 +93,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="tag">可选:标记</param>
         public static void d<T>(T msg)
         {
-            Write(PrintType.d, msg.ToString());
+            Write(LogType.d, msg.ToString());
         }
         /// <summary>
         /// 输出 Information (重要信息)
@@ -116,7 +102,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="tag">可选:标记</param>
         public static void i<T>(T msg)
         {
-            Write(PrintType.i, msg.ToString());
+            Write(LogType.i, msg.ToString());
         }
         /// <summary>
         /// 输出 Warning (警告信息)
@@ -125,7 +111,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="tag">可选:标记</param>
         public static void w<T>(T msg)
         {
-            Write(PrintType.w, msg.ToString());
+            Write(LogType.w, msg.ToString());
         }
         /// <summary>
         /// 输出 Error (错误信息)
@@ -134,7 +120,7 @@ namespace Y.Utils.Net20.LogUtils
         /// <param name="tag">可选:标记</param>
         public static void e<T>(T msg)
         {
-            Write(PrintType.e, msg.ToString());
+            Write(LogType.e, msg.ToString());
         }
         #endregion
     }

+ 15 - 0
Fork.Net/Y.Utils.Net20/LogUtils/LogType.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Y.Utils.Net20.LogUtils
+{
+    public enum LogType
+    {
+        v,//verbose 啰嗦的意思
+        d,//debug 调试的信息
+        i,//information 一般提示性的消息
+        w,//warning 警告
+        e,//error 错误信息
+    }
+}

+ 1 - 0
Fork.Net/Y.Utils.Net20/Y.Utils.Net20.csproj

@@ -56,6 +56,7 @@
     <Compile Include="JsonUtils\JsonTool.cs" />
     <Compile Include="ListUtils\ListTool.cs" />
     <Compile Include="LogUtils\Log.cs" />
+    <Compile Include="LogUtils\LogType.cs" />
     <Compile Include="MysqlUtils\MysqlTool.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="StringUtils\SimilarString.cs" />