|
|
@@ -4,6 +4,8 @@ using System.Linq;
|
|
|
using System.Runtime.InteropServices;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using Y.Utils.FileUtils;
|
|
|
+using Y.Utils.TxtUtils;
|
|
|
|
|
|
namespace Y.Utils.LogUtils
|
|
|
{
|
|
|
@@ -18,61 +20,67 @@ namespace Y.Utils.LogUtils
|
|
|
public class Log
|
|
|
{
|
|
|
//输出的 Log 格式
|
|
|
- const string LogFormat = "{0} {1} {2}";
|
|
|
- const string TimeFormat = "MM-dd HH:mm:ss.fff";
|
|
|
+ const string LogFormat = "{0} {1} {2}";
|
|
|
+ const string TimeFormat = "HH:mm:ss.fff";
|
|
|
+
|
|
|
+ public static bool IsWriteFile = true;
|
|
|
+
|
|
|
+ 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();
|
|
|
[DllImport("kernel32.dll")]
|
|
|
public static extern Boolean FreeConsole();
|
|
|
#endregion
|
|
|
- #region 输出颜色
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取输出颜色
|
|
|
/// </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;
|
|
|
}
|
|
|
}
|
|
|
- #endregion
|
|
|
- #region 写出 Log
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 写出到控制台
|
|
|
/// </summary>
|
|
|
/// <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)
|
|
|
{
|
|
|
- DateTime now = DateTime.Now;
|
|
|
Console.ForegroundColor = GetColor(type);
|
|
|
- Console.WriteLine(LogFormat, now.ToString(TimeFormat), type.ToString(), message);
|
|
|
+ Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
|
|
|
+ if (IsWriteFile) WriteFile(type, message);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void WriteFile(LogType type, string message)
|
|
|
+ {
|
|
|
+ if (IsWriteFile)
|
|
|
+ {
|
|
|
+ lock (LogFileLock)
|
|
|
+ {
|
|
|
+ //设置日志目录
|
|
|
+ string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
|
|
|
+ string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
|
|
|
+ //创建日志目录
|
|
|
+ DirTool.Create(logPath);
|
|
|
+ //写出日志
|
|
|
+ TxtTool.Append(file, string.Format(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- #endregion
|
|
|
|
|
|
#region 分类详细输出
|
|
|
/// <summary>
|
|
|
@@ -80,47 +88,46 @@ namespace Y.Utils.LogUtils
|
|
|
/// </summary>
|
|
|
/// <param name="message">消息</param>
|
|
|
/// <param name="tag">可选:标记</param>
|
|
|
- public static void v(string message)
|
|
|
+ public static void v<T>(T msg)
|
|
|
{
|
|
|
- Write(PrintType.v, message);
|
|
|
+ Write(LogType.v, msg.ToString());
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 输出 Debug (调试信息)
|
|
|
/// </summary>
|
|
|
/// <param name="message">消息</param>
|
|
|
/// <param name="tag">可选:标记</param>
|
|
|
- public static void d(string message)
|
|
|
+ public static void d<T>(T msg)
|
|
|
{
|
|
|
- Write(PrintType.d, message);
|
|
|
+ Write(LogType.d, msg.ToString());
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 输出 Information (重要信息)
|
|
|
/// </summary>
|
|
|
/// <param name="message">消息</param>
|
|
|
/// <param name="tag">可选:标记</param>
|
|
|
- public static void i(string message)
|
|
|
+ public static void i<T>(T msg)
|
|
|
{
|
|
|
- Write(PrintType.i, message);
|
|
|
+ Write(LogType.i, msg.ToString());
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 输出 Warning (警告信息)
|
|
|
/// </summary>
|
|
|
/// <param name="message">消息</param>
|
|
|
/// <param name="tag">可选:标记</param>
|
|
|
- public static void w(string message)
|
|
|
+ public static void w<T>(T msg)
|
|
|
{
|
|
|
- Write(PrintType.w, message);
|
|
|
+ Write(LogType.w, msg.ToString());
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 输出 Error (错误信息)
|
|
|
/// </summary>
|
|
|
/// <param name="message">消息</param>
|
|
|
/// <param name="tag">可选:标记</param>
|
|
|
- public static void e(string message)
|
|
|
+ public static void e<T>(T msg)
|
|
|
{
|
|
|
- Write(PrintType.e, message);
|
|
|
+ Write(LogType.e, msg.ToString());
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
-
|
|
|
}
|