diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 01f285a0ce44c..7b432e9144cce 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -180,12 +180,8 @@ internal Bot(string botName) { // Initialize SteamClient = new SteamClient(); - if (Program.GlobalConfig.Debug && !Debugging.NetHookAlreadyInitialized) { + if (Program.GlobalConfig.Debug && !Debugging.NetHookAlreadyInitialized && Directory.Exists(Program.DebugDirectory)) { try { - if (Directory.Exists(Program.DebugDirectory)) { - Directory.Delete(Program.DebugDirectory, true); - } - Directory.CreateDirectory(Program.DebugDirectory); SteamClient.DebugNetworkListener = new NetHookNetworkListener(Program.DebugDirectory); Debugging.NetHookAlreadyInitialized = true; } catch (Exception e) { diff --git a/ArchiSteamFarm/Debugging.cs b/ArchiSteamFarm/Debugging.cs index 66ca8929185da..b5500c9272b62 100644 --- a/ArchiSteamFarm/Debugging.cs +++ b/ArchiSteamFarm/Debugging.cs @@ -22,6 +22,10 @@ limitations under the License. */ +using SteamKit2; +using System; +using System.IO; + namespace ArchiSteamFarm { internal static class Debugging { #if DEBUG @@ -33,5 +37,27 @@ internal static class Debugging { internal static bool IsReleaseBuild => !IsDebugBuild; internal static bool NetHookAlreadyInitialized { get; set; } = false; + + internal sealed class DebugListener : IDebugListener { + private readonly string FilePath; + + internal DebugListener(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + return; + } + + FilePath = filePath; + } + + public void WriteLine(string category, string msg) { + lock (FilePath) { + try { + File.AppendAllText(FilePath, category + " | " + msg + Environment.NewLine); + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + } + } } } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index f480b523fae56..3e048bcce689f 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -438,6 +438,18 @@ private static void Main(string[] args) { } } + // If debugging is on, we prepare debug directory prior to running + if (GlobalConfig.Debug) { + if (Directory.Exists(Program.DebugDirectory)) { + Directory.Delete(Program.DebugDirectory, true); + Thread.Sleep(1000); // Dirty workaround giving Windows some time to sync + } + Directory.CreateDirectory(Program.DebugDirectory); + + SteamKit2.DebugLog.AddListener(new Debugging.DebugListener(Path.Combine(Program.DebugDirectory, "debug.txt"))); + SteamKit2.DebugLog.Enabled = true; + } + // Parse args ParseArgs(args);