Skip to content

Commit 5bb4b49

Browse files
committed
refactor: 更新未配置 Key 逻辑提高性能
1 parent c843c00 commit 5bb4b49

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

src/components/BootstrapBlazor.JuHeIpLocatorProvider/JuHeIpLocatorProvider.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,15 @@ private JuHeIpLocatorOptions GetOptions()
5353
{
5454
var options = juHeIpLocatorOptions.Value;
5555

56-
try
56+
if (string.IsNullOrEmpty(options.Key))
5757
{
58-
if (string.IsNullOrEmpty(options.Key))
59-
{
60-
throw new InvalidOperationException($"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key");
61-
}
62-
if (string.IsNullOrEmpty(options.Url))
63-
{
64-
options.Url = Url;
65-
}
58+
LastError = $"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key";
59+
Log(LastError);
60+
6661
}
67-
catch (Exception ex)
62+
if (string.IsNullOrEmpty(options.Url))
6863
{
69-
logger.LogError(ex, "{GetOptions} failed", nameof(GetOptions));
64+
options.Url = Url;
7065
}
7166
return options;
7267
}
@@ -83,8 +78,17 @@ private JuHeIpLocatorOptions GetOptions()
8378
var result = await client.GetFromJsonAsync<JuHeLocationResult>(url, token);
8479
if (result != null && result.ErrorCode != 0)
8580
{
86-
logger.LogError("ErrorCode: {ErrorCode} Reason: {Reason}", result.ErrorCode, result.Reason);
81+
LastError = $"ErrorCode: {result.ErrorCode} Reason: {result.Reason}";
82+
Log(LastError);
8783
}
8884
return result?.ToString();
8985
}
86+
87+
private void Log(string? message)
88+
{
89+
if (logger.IsEnabled(LogLevel.Error))
90+
{
91+
logger.LogError("{message}", message);
92+
}
93+
}
9094
}

0 commit comments

Comments
 (0)