using System.Runtime.InteropServices; using LogTool; using System.Text; namespace WcsMain; /// /// 输出信息 /// public class ConsoleLog { private static readonly object _locker = new(); /// /// 输出提示信息 /// /// public static void Info(params string[] messages) => Info(true, messages); /// /// 输出提示信息 /// /// /// public static void Info(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.Magenta; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteEventLog(stringBuilder); } } /// /// 输出错误信息 /// /// public static void Error(params string[] messages) => Error(true, messages); /// /// 输出错误信息 /// /// /// public static void Error(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.Red; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteEventLog(stringBuilder); } } /// /// 输出异常信息 /// /// public static void Exception(params string[] messages) => Exception(true, messages); /// /// 输出异常信息 /// /// /// public static void Exception(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteExceptionLog(stringBuilder); } } /// /// 输出成功信息 /// /// public static void Success(params string[] messages) => Success(true, messages); /// /// 输出成功信息 /// /// /// public static void Success(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteEventLog(stringBuilder); } } /// /// 输出警告信息 /// /// public static void Warning(params string[] messages) => Warning(true, messages); /// /// 输出警告信息 /// /// /// public static void Warning(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteEventLog(stringBuilder); } } /// /// 输出蓝色提示信息 /// /// public static void Tip(params string[] messages) => Tip(true, messages); /// /// 输出蓝色提示信息 /// /// /// public static void Tip(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.Blue; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteEventLog(stringBuilder); } } /// /// Tcp 通讯专用 /// /// public static void Tcp(params string[] messages) => Tcp(true, messages); /// /// Tcp 通讯专用 /// /// /// public static void Tcp(bool isShow, params string[] messages) { if (!isShow) return; if (messages == default || messages.Length == 0) return; DateTime now = DateTime.Now; StringBuilder stringBuilder = new(); stringBuilder.AppendLine(now.ToString("yyyy-MM-dd HH:mm:ss:fff")); foreach (string message in messages) { stringBuilder.AppendLine($" -- {message}"); } lock (_locker) { Console.ForegroundColor = ConsoleColor.White; Console.Write(stringBuilder.ToString()); WcsLog.Instance().WriteTcpLog(stringBuilder); } } const int STD_INPUT_HANDLE = -10; const uint ENABLE_QUICK_EDIT_MODE = 0x0040; [DllImport("kernel32.dll", SetLastError = true)] internal static extern nint GetStdHandle(int hConsoleHandle); [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool GetConsoleMode(nint hConsoleHandle, out uint mode); [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool SetConsoleMode(nint hConsoleHandle, uint mode); /// /// 禁用快速编辑 ---- 此项会屏蔽控制台输入 /// public static void DisbleQuickEditMode() { nint hStdin = GetStdHandle(STD_INPUT_HANDLE); GetConsoleMode(hStdin, out uint mode); mode &= ~ENABLE_QUICK_EDIT_MODE; SetConsoleMode(hStdin, mode); } }