diff --git a/GameFrameX.AppHost/Program.cs b/GameFrameX.AppHost/Program.cs index ec65313d..2c02b15d 100644 --- a/GameFrameX.AppHost/Program.cs +++ b/GameFrameX.AppHost/Program.cs @@ -138,31 +138,7 @@ private static async Task Main(string[] args) /// 启动选项实例 / Startup options instance private static void ConfigureLogOptions(StartupOptions startupOptions) { - var properties = typeof(StartupOptions).GetProperties(); - foreach (var property in properties) - { - var grafanaLokiLabelTagAttribute = property.GetCustomAttribute(); - if (grafanaLokiLabelTagAttribute == null) - { - continue; - } - - if (property.Name.Equals(nameof(StartupOptions.ServerType), StringComparison.Ordinal)) - { - continue; - } - - var value = property.GetValue(startupOptions)?.ToString(); - if (string.IsNullOrEmpty(value)) - { - continue; - } - - if (!LogOptions.Default.GrafanaLokiLabels.TryAdd(property.Name, value)) - { - LogHelper.Warning(LocalizationService.GetString(Keys.StartUp.GrafanaLokiLabelExists, property.Name)); - } - } + ApplyGrafanaLokiLabels(startupOptions); LogOptions.Default.IsConsole = startupOptions.LogIsConsole; LogOptions.Default.IsGrafanaLoki = startupOptions.LogIsGrafanaLoki; @@ -206,6 +182,42 @@ private static void ConfigureLogOptions(StartupOptions startupOptions) LogOptions.Default.LogTagName = logTypeParts.Count > 0 ? string.Join("_", logTypeParts) : null; } + /// + /// 根据启动选项上的 GrafanaLokiLabelTagAttribute 标记,收集并写入日志的 Grafana Loki 标签。 + /// + /// + /// Collects Grafana Loki labels from GrafanaLokiLabelTagAttribute markers on startup options and writes them to log options. Extracted from ConfigureLogOptions to keep cognitive complexity under the Sonar S3776 threshold. + /// + /// 启动选项实例 / Startup options instance + private static void ApplyGrafanaLokiLabels(StartupOptions startupOptions) + { + var properties = typeof(StartupOptions).GetProperties(); + foreach (var property in properties) + { + var grafanaLokiLabelTagAttribute = property.GetCustomAttribute(); + if (grafanaLokiLabelTagAttribute == null) + { + continue; + } + + if (property.Name.Equals(nameof(StartupOptions.ServerType), StringComparison.Ordinal)) + { + continue; + } + + var value = property.GetValue(startupOptions)?.ToString(); + if (string.IsNullOrEmpty(value)) + { + continue; + } + + if (!LogOptions.Default.GrafanaLokiLabels.TryAdd(property.Name, value)) + { + LogHelper.Warning(LocalizationService.GetString(Keys.StartUp.GrafanaLokiLabelExists, property.Name)); + } + } + } + /// /// 根据服务器类型参数解析多个服务器类型。 ///