1using System; 2using System.Diagnostics; 3using System.IO; 4 5namespace WindowCapture.Helpers 6{ 7 public static class Logger 8 { 9 private static readonly object logLock = new object(); 10 11 public static void Log(string category, string message) 12 { 13 Debug.WriteLine(category + ": " + message); 14 try 15 { 16 string dir = Path.Combine( 17 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 18 "WindowCapture"); 19 string logPath = Path.Combine(dir, category.ToLowerInvariant() + "_log.txt"); 20 // Serialize writes: concurrent File.AppendAllText from the hook thread + worker 21 // threads previously threw sharing-violation IOExceptions and silently dropped lines. 22 lock (logLock) 23 { 24 if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); 25 File.AppendAllText(logPath, DateTime.Now.ToString("HH:mm:ss") + " " + message + "\r\n"); 26 } 27 } 28 catch { } 29 } 30 } 31}