@@ -18,7 +18,7 @@ namespace GameFrameX.Foundation.Logger;
1818/// </summary>
1919public static class LogHandler
2020{
21- private static bool _isInitSerilogDiagnosis = false ;
21+ private static bool _isInitSerilogDiagnosis ;
2222
2323 /// <summary>
2424 /// 启用 Serilog 的自动诊断
@@ -54,6 +54,16 @@ public static LoggerConfiguration CreateLoggerConfiguration()
5454 . MinimumLevel . Override ( "Microsoft.AspNetCore" , LogEventLevel . Warning ) ;
5555 }
5656
57+ /// <summary>
58+ /// 控制台输出模板,用于格式化控制台日志输出。
59+ /// </summary>
60+ const string ConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}][{LogType}-{TagName}]{Message:lj}{NewLine}{Exception}" ;
61+
62+ /// <summary>
63+ /// 文件输出模板,用于格式化文件日志输出。
64+ /// </summary>
65+ const string FileOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}][{LogType}-{TagName}] {Message:lj}{NewLine}{Exception}" ;
66+
5767 /// <summary>
5868 /// 启动并配置日志系统
5969 /// </summary>
@@ -68,9 +78,20 @@ public static ILogger Create(LogOptions logOptions, bool isDefault = true, Actio
6878 SerilogDiagnosis ( ) ;
6979 try
7080 {
71- // 日志文件存储的路径
81+ // 文件名
7282 var logFileName = $ "{ ( logOptions . LogType ?? AppDomain . CurrentDomain . FriendlyName ) . ToLower ( ) } _.log";
83+ // 日志文件存储的路径
7384 var logSavePath = logOptions . LogSavePath ?? "./logs/" ;
85+ if ( ! logSavePath . EndsWithFast ( "/" ) )
86+ {
87+ logSavePath += "/" ;
88+ }
89+
90+ if ( ! string . IsNullOrEmpty ( logOptions . LogTagName ) )
91+ {
92+ logSavePath += logOptions . LogTagName + "/" ;
93+ }
94+
7495 // 计算最终日志文件路径
7596 var logPath = Path . Combine ( logSavePath , logFileName ) ;
7697 // 兼容可能的层级目录:始终创建文件所在的目录
@@ -80,23 +101,15 @@ public static ILogger Create(LogOptions logOptions, bool isDefault = true, Actio
80101 Directory . CreateDirectory ( logFolderPath ) ;
81102 }
82103
83-
84- // Console.WriteLine("the following is the log configuration information");
85104 if ( isDefault )
86105 {
87106 LogHelper . ShowOption ( "log configuration information" , logOptions ) ;
88107 }
89108
90- // Console.WriteLine("╔═════════════════════════════════════════════════════════╗");
91- // Console.WriteLine(logOptions);
92- // Console.WriteLine("╚═════════════════════════════════════════════════════════╝");
93- Console . WriteLine ( ) ;
94- var logger = CreateLoggerConfiguration ( ) . Enrich . WithProperty ( "AppType" , logOptions . LogType ?? AppDomain . CurrentDomain . FriendlyName ) ;
95- if ( ! string . IsNullOrEmpty ( logOptions . LogTagName ) )
96- {
97- logger . Enrich . WithProperty ( "TagName" , logOptions . LogTagName ?? "" ) ;
98- }
99-
109+ var logger = CreateLoggerConfiguration ( ) ;
110+ logger . Enrich . WithProperty ( "LogType" , logOptions . LogType ) ;
111+ logger . Enrich . WithProperty ( "FriendlyName" , AppDomain . CurrentDomain . FriendlyName ) ;
112+ logger . Enrich . WithProperty ( "TagName" , logOptions . LogTagName ?? "" ) ;
100113
101114 if ( logOptions . IsGrafanaLoki )
102115 {
@@ -156,15 +169,14 @@ public static ILogger Create(LogOptions logOptions, bool isDefault = true, Actio
156169 }
157170
158171 configurationAction ? . Invoke ( logger ) ;
159-
160- string consoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}][{TagName}]{Message:lj}{NewLine}{Exception}" ;
161- string fileOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}][{FriendlyName}] {Message:lj}{NewLine}{Exception}" ;
172+ string consoleOutputTemplate = ConsoleOutputTemplate ;
162173
163174 if ( logOptions . ConsoleOutputTemplate . IsNotNullOrEmptyOrWhiteSpace ( ) )
164175 {
165176 consoleOutputTemplate = logOptions . ConsoleOutputTemplate ;
166177 }
167178
179+ string fileOutputTemplate = FileOutputTemplate ;
168180 if ( logOptions . FileOutputTemplate . IsNotNullOrEmptyOrWhiteSpace ( ) )
169181 {
170182 fileOutputTemplate = logOptions . FileOutputTemplate ;
@@ -225,15 +237,15 @@ public static ILogger Create(LogOptions logOptions, bool isDefault = true, Actio
225237 var serilog = logger . CreateLogger ( ) ;
226238 if ( isDefault )
227239 {
228- Serilog . Log . Logger = serilog ;
240+ Log . Logger = serilog ;
229241 LogHelper . SetLogger ( serilog ) ;
230242 }
231243
232244 return serilog ;
233245 }
234246 catch ( Exception e )
235247 {
236- Serilog . Log . Error ( $ "配置日志系统过程中发生错误,异常:{ e } ") ;
248+ Log . Error ( $ "配置日志系统过程中发生错误,异常:{ e } ") ;
237249 throw ;
238250 }
239251 }
0 commit comments