protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { try { if (_debugWriter != null) { DebugLoggerExtensions.Log(this, "Stopping logging."); _debugWriter.Close(); } } catch { } } disposedValue = true; } }
public DebugLogger(string Topic, int CalledFromStackLevel = 1, bool LogStackToDetermineCalledFromStackLevel = false) { try { bool DebugLogOn = ConfigurationManager.AppSettings["EnableDebugLog"] == "ON"; _debugWriter = null; if (DebugLogOn) { string LogDirectory = (ConfigurationManager.AppSettings["DebugLogDirectory"] ?? "").Trim(); if (!Directory.Exists(LogDirectory)) { Directory.CreateDirectory(LogDirectory); } string guid = Guid.NewGuid().ToString(); if (string.IsNullOrWhiteSpace(Topic)) { Topic = "DebugLog"; } Topic = Topic.Trim() + "_"; FileName = Path.Combine(LogDirectory, Topic + guid + ".txt"); _debugWriter = new StreamWriter(File.Open(FileName, FileMode.Create)); DebugLoggerExtensions.Log(this, "Starting logging..."); if (LogStackToDetermineCalledFromStackLevel) { int i = 0; foreach (var frame in new StackTrace().GetFrames()) { DebugLoggerExtensions.Log(this, "Stack Frame " + i + " = " + GetStackMethod(frame)); i++; } } DebugLoggerExtensions.Log(this, "Called from " + GetStackMethod(new StackTrace().GetFrame(CalledFromStackLevel)) + "."); } } catch { } }