Skip to content

Commit

Permalink
Extended logging
Browse files Browse the repository at this point in the history
  • Loading branch information
valnoxy committed Nov 2, 2024
1 parent 29e3fd1 commit 6f9b980
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions GoAwayEdge/Common/Debugging/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Backported from: https://git.heydu.net/valnoxy/xorieos/-/blob/main/srv03rtm/base/ntsetup/winnt32/modernsetup/common/logging.cs
*/

using System.Diagnostics;
using System.IO;
using System.Reflection;

Expand Down Expand Up @@ -51,10 +52,23 @@ public static void Log(string message, LogLevel level = LogLevel.INFO)
{
if (string.IsNullOrEmpty(_logFile))
throw new Exception("Logging class not initialized!");
using var writer = new StreamWriter(_logFile, true);

var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {level} - {message}";
writer.WriteLine(logMessage);
// Get calling method information
var stackTrace = new StackTrace();
var callingMethod = stackTrace.GetFrame(1)?.GetMethod();
var callingClass = callingMethod?.DeclaringType?.FullName ?? "UnknownClass";
var methodName = callingMethod?.Name ?? "UnknownMethod";

// Construct the log message with calling class and method
var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {level} - {callingClass}.{methodName} - {message}";

using (var fileStream = new FileStream(_logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
using (var writer = new StreamWriter(fileStream))
{
writer.WriteLine(logMessage);
}

// Output log message to Debug console
System.Diagnostics.Debug.WriteLine(logMessage);
}

Expand All @@ -72,4 +86,4 @@ private static void DeleteOldLogFiles()
}
}
}
}
}

0 comments on commit 6f9b980

Please sign in to comment.