From cfb4a5b3f6bd27ded76193f2a3f524fc3d260000 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 19 Mar 2021 00:58:34 +0800 Subject: [PATCH] The Debug configuration will make the build attach to the console and write the application log to standard output --- Netch/Global.cs | 6 ------ Netch/Netch.cs | 13 +++++++++++-- Netch/Utils/Logging.cs | 10 ++++------ Netch/Utils/Utils.cs | 3 --- UnitTest/TestBase.cs | 10 +--------- 5 files changed, 16 insertions(+), 26 deletions(-) diff --git a/Netch/Global.cs b/Netch/Global.cs index 0ddb2523..7dc496d2 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -34,12 +34,6 @@ namespace Netch public static Mutex Mutex => LazyMutex.Value; -#if DEBUG - public static bool Testing = false; -#else - public const bool Testing = false; -#endif - /// /// 用于读取和写入的配置 /// diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 9bba7de6..45d77260 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -22,9 +22,12 @@ namespace Netch [STAThread] public static void Main(string[] args) { +#if DEBUG + AttachConsole(); +#else if (args.Contains("-console")) - if (!NativeMethods.AttachConsole(-1)) - NativeMethods.AllocConsole(); + AttachConsole(); +#endif // 设置当前目录 Directory.SetCurrentDirectory(Global.NetchDir); @@ -85,6 +88,12 @@ namespace Netch Application.Run(Global.MainForm); } + private static void AttachConsole() + { + if (!NativeMethods.AttachConsole(-1)) + NativeMethods.AllocConsole(); + } + public static void Application_OnException(object sender, ThreadExceptionEventArgs e) { Logging.Error(e.Exception.ToString()); diff --git a/Netch/Utils/Logging.cs b/Netch/Utils/Logging.cs index 11c95335..fd479511 100644 --- a/Netch/Utils/Logging.cs +++ b/Netch/Utils/Logging.cs @@ -40,14 +40,12 @@ namespace Netch.Utils private static void Write(string text, LogLevel logLevel) { var contents = $@"[{DateTime.Now}][{logLevel.ToString()}] {text}{Global.EOF}"; - if (Global.Testing) - { - Console.WriteLine(contents); - return; - } - +#if DEBUG + Console.WriteLine(contents); +#else lock (FileLock) File.AppendAllText(LogFile, contents); +#endif } } } \ No newline at end of file diff --git a/Netch/Utils/Utils.cs b/Netch/Utils/Utils.cs index 174a6f9e..107bbbe3 100644 --- a/Netch/Utils/Utils.cs +++ b/Netch/Utils/Utils.cs @@ -21,8 +21,6 @@ namespace Netch.Utils { public static bool Open(string path) { - if (Global.Testing) - return true; try { Process.Start(new ProcessStartInfo @@ -139,7 +137,6 @@ namespace Netch.Utils return File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : string.Empty; } - public static void DrawCenterComboBox(object sender, DrawItemEventArgs e) { if (sender is ComboBox cbx) diff --git a/UnitTest/TestBase.cs b/UnitTest/TestBase.cs index 8b404aa0..9fef3e87 100644 --- a/UnitTest/TestBase.cs +++ b/UnitTest/TestBase.cs @@ -1,14 +1,6 @@ -using Netch; - -namespace UnitTest +namespace UnitTest { public class TestBase { - protected TestBase() - { -#if DEBUG - Global.Testing = true; -#endif - } } } \ No newline at end of file