14
14
* Backported from: https://git.heydu.net/valnoxy/xorieos/-/blob/main/srv03rtm/base/ntsetup/winnt32/modernsetup/common/logging.cs
15
15
*/
16
16
17
+ using System . Diagnostics ;
17
18
using System . IO ;
18
19
using System . Reflection ;
19
20
@@ -51,10 +52,23 @@ public static void Log(string message, LogLevel level = LogLevel.INFO)
51
52
{
52
53
if ( string . IsNullOrEmpty ( _logFile ) )
53
54
throw new Exception ( "Logging class not initialized!" ) ;
54
- using var writer = new StreamWriter ( _logFile , true ) ;
55
55
56
- var logMessage = $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss} - { level } - { message } ";
57
- writer . WriteLine ( logMessage ) ;
56
+ // Get calling method information
57
+ var stackTrace = new StackTrace ( ) ;
58
+ var callingMethod = stackTrace . GetFrame ( 1 ) ? . GetMethod ( ) ;
59
+ var callingClass = callingMethod ? . DeclaringType ? . FullName ?? "UnknownClass" ;
60
+ var methodName = callingMethod ? . Name ?? "UnknownMethod" ;
61
+
62
+ // Construct the log message with calling class and method
63
+ var logMessage = $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss} - { level } - { callingClass } .{ methodName } - { message } ";
64
+
65
+ using ( var fileStream = new FileStream ( _logFile , FileMode . Append , FileAccess . Write , FileShare . ReadWrite ) )
66
+ using ( var writer = new StreamWriter ( fileStream ) )
67
+ {
68
+ writer . WriteLine ( logMessage ) ;
69
+ }
70
+
71
+ // Output log message to Debug console
58
72
System . Diagnostics . Debug . WriteLine ( logMessage ) ;
59
73
}
60
74
@@ -72,4 +86,4 @@ private static void DeleteOldLogFiles()
72
86
}
73
87
}
74
88
}
75
- }
89
+ }
0 commit comments