Skip to content

Commit cb26bef

Browse files
Fix CA1873 warnings in Abstractions and remove redundant packages
Co-authored-by: waldekmastykarz <[email protected]>
1 parent f3a3517 commit cb26bef

File tree

13 files changed

+341
-122
lines changed

13 files changed

+341
-122
lines changed

DevProxy.Abstractions/Data/MSGraphDb.cs

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public async Task<int> GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke
4444
var appFolder = ProxyUtils.AppFolder;
4545
if (string.IsNullOrEmpty(appFolder))
4646
{
47-
_logger.LogError("App folder {AppFolder} not found", appFolder);
47+
if (_logger.IsEnabled(LogLevel.Error))
48+
{
49+
_logger.LogError("App folder {AppFolder} not found", appFolder);
50+
}
4851
return 1;
4952
}
5053

@@ -54,42 +57,63 @@ public async Task<int> GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke
5457
var modifiedToday = IsModifiedToday(dbFileInfo);
5558
if (modifiedToday && skipIfUpdatedToday)
5659
{
57-
_logger.LogInformation("Microsoft Graph database has already been updated today");
60+
if (_logger.IsEnabled(LogLevel.Information))
61+
{
62+
_logger.LogInformation("Microsoft Graph database has already been updated today");
63+
}
5864
return 1;
5965
}
6066

6167
var (isApiModified, hasErrors) = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken);
6268

6369
if (hasErrors)
6470
{
65-
_logger.LogWarning("Unable to update Microsoft Graph database");
71+
if (_logger.IsEnabled(LogLevel.Warning))
72+
{
73+
_logger.LogWarning("Unable to update Microsoft Graph database");
74+
}
6675
return 1;
6776
}
6877

6978
if (!isApiModified)
7079
{
7180
UpdateLastWriteTime(dbFileInfo);
72-
_logger.LogDebug("Updated the last-write-time attribute of Microsoft Graph database {File}", dbFileInfo);
73-
_logger.LogInformation("Microsoft Graph database is already up-to-date");
81+
if (_logger.IsEnabled(LogLevel.Debug))
82+
{
83+
_logger.LogDebug("Updated the last-write-time attribute of Microsoft Graph database {File}", dbFileInfo);
84+
}
85+
if (_logger.IsEnabled(LogLevel.Information))
86+
{
87+
_logger.LogInformation("Microsoft Graph database is already up-to-date");
88+
}
7489
return 1;
7590
}
7691

7792
await LoadOpenAPIFilesAsync(appFolder, cancellationToken);
7893
if (_openApiDocuments.Count < 1)
7994
{
80-
_logger.LogDebug("No OpenAPI files found or couldn't load them");
95+
if (_logger.IsEnabled(LogLevel.Debug))
96+
{
97+
_logger.LogDebug("No OpenAPI files found or couldn't load them");
98+
}
8199
return 1;
82100
}
83101

84102
await CreateDbAsync(cancellationToken);
85103
await FillDataAsync(cancellationToken);
86104

87-
_logger.LogInformation("Microsoft Graph database is successfully updated");
105+
if (_logger.IsEnabled(LogLevel.Information))
106+
{
107+
_logger.LogInformation("Microsoft Graph database is successfully updated");
108+
}
88109
return 0;
89110
}
90111
catch (Exception ex)
91112
{
92-
_logger.LogError(ex, "Error generating Microsoft Graph database");
113+
if (_logger.IsEnabled(LogLevel.Error))
114+
{
115+
_logger.LogError(ex, "Error generating Microsoft Graph database");
116+
}
93117
return 1;
94118
}
95119
}
@@ -100,20 +124,32 @@ public async Task<int> GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke
100124

101125
private async Task CreateDbAsync(CancellationToken cancellationToken)
102126
{
103-
_logger.LogInformation("Creating database...");
127+
if (_logger.IsEnabled(LogLevel.Information))
128+
{
129+
_logger.LogInformation("Creating database...");
130+
}
104131

105-
_logger.LogDebug("Dropping endpoints table...");
132+
if (_logger.IsEnabled(LogLevel.Debug))
133+
{
134+
_logger.LogDebug("Dropping endpoints table...");
135+
}
106136
var dropTable = Connection.CreateCommand();
107137
dropTable.CommandText = "DROP TABLE IF EXISTS endpoints";
108138
_ = await dropTable.ExecuteNonQueryAsync(cancellationToken);
109139

110-
_logger.LogDebug("Creating endpoints table...");
140+
if (_logger.IsEnabled(LogLevel.Debug))
141+
{
142+
_logger.LogDebug("Creating endpoints table...");
143+
}
111144
var createTable = Connection.CreateCommand();
112145
// when you change the schema, increase the db version number in ProxyUtils
113146
createTable.CommandText = "CREATE TABLE IF NOT EXISTS endpoints (path TEXT, graphVersion TEXT, hasSelect BOOLEAN)";
114147
_ = await createTable.ExecuteNonQueryAsync(cancellationToken);
115148

116-
_logger.LogDebug("Creating index on endpoints and version...");
149+
if (_logger.IsEnabled(LogLevel.Debug))
150+
{
151+
_logger.LogDebug("Creating index on endpoints and version...");
152+
}
117153
// Add an index on the path and graphVersion columns
118154
var createIndex = Connection.CreateCommand();
119155
createIndex.CommandText = "CREATE INDEX IF NOT EXISTS idx_endpoints_path_version ON endpoints (path, graphVersion)";
@@ -122,7 +158,10 @@ private async Task CreateDbAsync(CancellationToken cancellationToken)
122158

123159
private async Task FillDataAsync(CancellationToken cancellationToken)
124160
{
125-
_logger.LogInformation("Filling database...");
161+
if (_logger.IsEnabled(LogLevel.Information))
162+
{
163+
_logger.LogInformation("Filling database...");
164+
}
126165

127166
SetDbJournaling(false);
128167

@@ -137,7 +176,10 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
137176
var graphVersion = openApiDocument.Key;
138177
var document = openApiDocument.Value;
139178

140-
_logger.LogDebug("Filling database for {GraphVersion}...", graphVersion);
179+
if (_logger.IsEnabled(LogLevel.Debug))
180+
{
181+
_logger.LogDebug("Filling database for {GraphVersion}...", graphVersion);
182+
}
141183

142184
var insertEndpoint = Connection.CreateCommand();
143185
insertEndpoint.CommandText = "INSERT INTO endpoints (path, graphVersion, hasSelect) VALUES (@path, @graphVersion, @hasSelect)";
@@ -150,20 +192,29 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
150192
{
151193
cancellationToken.ThrowIfCancellationRequested();
152194

153-
_logger.LogTrace("Endpoint {GraphVersion}{Key}...", graphVersion, path.Key);
195+
if (_logger.IsEnabled(LogLevel.Trace))
196+
{
197+
_logger.LogTrace("Endpoint {GraphVersion}{Key}...", graphVersion, path.Key);
198+
}
154199

155200
// Get the GET operation for this path
156201
var getOperation = path.Value.Operations.FirstOrDefault(o => o.Key == OperationType.Get).Value;
157202
if (getOperation == null)
158203
{
159-
_logger.LogTrace("No GET operation found for {GraphVersion}{Key}", graphVersion, path.Key);
204+
if (_logger.IsEnabled(LogLevel.Trace))
205+
{
206+
_logger.LogTrace("No GET operation found for {GraphVersion}{Key}", graphVersion, path.Key);
207+
}
160208
continue;
161209
}
162210

163211
// Check if the GET operation has a $select parameter
164212
var hasSelect = getOperation.Parameters.Any(p => p.Name == "$select");
165213

166-
_logger.LogTrace("Inserting endpoint {GraphVersion}{Key} with hasSelect={HasSelect}...", graphVersion, path.Key, hasSelect);
214+
if (_logger.IsEnabled(LogLevel.Trace))
215+
{
216+
_logger.LogTrace("Inserting endpoint {GraphVersion}{Key} with hasSelect={HasSelect}...", graphVersion, path.Key, hasSelect);
217+
}
167218
pathParam.Value = path.Key;
168219
graphVersionParam.Value = graphVersion;
169220
hasSelectParam.Value = hasSelect;
@@ -176,12 +227,18 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
176227

177228
SetDbJournaling(true);
178229

179-
_logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i);
230+
if (_logger.IsEnabled(LogLevel.Information))
231+
{
232+
_logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i);
233+
}
180234
}
181235

182236
private async Task<(bool isApiUpdated, bool hasErrors)> UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken)
183237
{
184-
_logger.LogInformation("Checking for updated OpenAPI files...");
238+
if (_logger.IsEnabled(LogLevel.Information))
239+
{
240+
_logger.LogInformation("Checking for updated OpenAPI files...");
241+
}
185242

186243
var isApiUpdated = false;
187244
var hasErrors = false;
@@ -191,23 +248,35 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
191248
try
192249
{
193250
var yamlFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiYamlFileName(version)));
194-
_logger.LogDebug("Checking for updated OpenAPI file {File}...", yamlFile);
251+
if (_logger.IsEnabled(LogLevel.Debug))
252+
{
253+
_logger.LogDebug("Checking for updated OpenAPI file {File}...", yamlFile);
254+
}
195255
if (IsModifiedToday(yamlFile))
196256
{
197-
_logger.LogInformation("File {File} has already been updated today", yamlFile);
257+
if (_logger.IsEnabled(LogLevel.Information))
258+
{
259+
_logger.LogInformation("File {File} has already been updated today", yamlFile);
260+
}
198261
continue;
199262
}
200263

201264
var url = GetOpenApiSpecUrl(version);
202-
_logger.LogInformation("Downloading OpenAPI file from {Url}...", url);
265+
if (_logger.IsEnabled(LogLevel.Information))
266+
{
267+
_logger.LogInformation("Downloading OpenAPI file from {Url}...", url);
268+
}
203269

204270
var etagFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiEtagFileName(version)));
205271
isApiUpdated |= await DownloadOpenAPIFileAsync(url, yamlFile, etagFile, cancellationToken);
206272
}
207273
catch (Exception ex)
208274
{
209275
hasErrors = true;
210-
_logger.LogError(ex, "Error updating OpenAPI files");
276+
if (_logger.IsEnabled(LogLevel.Error))
277+
{
278+
_logger.LogError(ex, "Error updating OpenAPI files");
279+
}
211280
}
212281
}
213282
return (isApiUpdated, hasErrors);
@@ -232,7 +301,10 @@ private async Task<bool> DownloadOpenAPIFileAsync(string url, FileInfo yamlFile,
232301
if (response.StatusCode == HttpStatusCode.NotModified)
233302
{
234303
UpdateLastWriteTime(yamlFile);
235-
_logger.LogDebug("File {File} already up-to-date. Updated the last-write-time attribute", yamlFile);
304+
if (_logger.IsEnabled(LogLevel.Debug))
305+
{
306+
_logger.LogDebug("File {File} already up-to-date. Updated the last-write-time attribute", yamlFile);
307+
}
236308
return false;
237309
}
238310

@@ -252,23 +324,35 @@ private async Task<bool> DownloadOpenAPIFileAsync(string url, FileInfo yamlFile,
252324
etagFile.Delete();
253325
}
254326

255-
_logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, yamlFile);
327+
if (_logger.IsEnabled(LogLevel.Debug))
328+
{
329+
_logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, yamlFile);
330+
}
256331
return true;
257332
}
258333

259334
private async Task LoadOpenAPIFilesAsync(string folder, CancellationToken cancellationToken)
260335
{
261-
_logger.LogInformation("Loading OpenAPI files...");
336+
if (_logger.IsEnabled(LogLevel.Information))
337+
{
338+
_logger.LogInformation("Loading OpenAPI files...");
339+
}
262340

263341
foreach (var version in graphVersions)
264342
{
265343
var filePath = Path.Combine(folder, GetGraphOpenApiYamlFileName(version));
266344
var file = new FileInfo(filePath);
267-
_logger.LogDebug("Loading OpenAPI file for {FilePath}...", filePath);
345+
if (_logger.IsEnabled(LogLevel.Debug))
346+
{
347+
_logger.LogDebug("Loading OpenAPI file for {FilePath}...", filePath);
348+
}
268349

269350
if (!file.Exists)
270351
{
271-
_logger.LogDebug("File {FilePath} does not exist", filePath);
352+
if (_logger.IsEnabled(LogLevel.Debug))
353+
{
354+
_logger.LogDebug("File {FilePath} does not exist", filePath);
355+
}
272356
continue;
273357
}
274358

@@ -278,11 +362,17 @@ private async Task LoadOpenAPIFilesAsync(string folder, CancellationToken cancel
278362
var openApiDocument = await new OpenApiStreamReader().ReadAsync(fileStream, cancellationToken);
279363
_openApiDocuments[version] = openApiDocument.OpenApiDocument;
280364

281-
_logger.LogDebug("Added OpenAPI file {FilePath} for {Version}", filePath, version);
365+
if (_logger.IsEnabled(LogLevel.Debug))
366+
{
367+
_logger.LogDebug("Added OpenAPI file {FilePath} for {Version}", filePath, version);
368+
}
282369
}
283370
catch (Exception ex)
284371
{
285-
_logger.LogError(ex, "Error loading OpenAPI file {FilePath}", filePath);
372+
if (_logger.IsEnabled(LogLevel.Error))
373+
{
374+
_logger.LogError(ex, "Error loading OpenAPI file {FilePath}", filePath);
375+
}
286376
}
287377
}
288378
}

DevProxy.Abstractions/DevProxy.Abstractions.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
1111
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1212
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
13-
<NoWarn>CA1873</NoWarn>
1413
</PropertyGroup>
1514

1615
<ItemGroup>

DevProxy.Abstractions/LanguageModel/BaseLanguageModelClient.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public abstract class BaseLanguageModelClient(LanguageModelConfiguration configu
2424

2525
if (!promptFileName.EndsWith(".prompty", StringComparison.OrdinalIgnoreCase))
2626
{
27-
Logger.LogDebug("Prompt file name '{PromptFileName}' does not end with '.prompty'. Appending the extension.", promptFileName);
27+
if (Logger.IsEnabled(LogLevel.Debug))
28+
{
29+
Logger.LogDebug("Prompt file name '{PromptFileName}' does not end with '.prompty'. Appending the extension.", promptFileName);
30+
}
2831
promptFileName += ".prompty";
2932
}
3033

@@ -93,22 +96,31 @@ public async Task<bool> IsEnabledAsync(CancellationToken cancellationToken)
9396

9497
private (IEnumerable<ILanguageModelChatCompletionMessage>?, CompletionOptions?) LoadPrompt(string promptFileName, Dictionary<string, object> parameters)
9598
{
96-
Logger.LogDebug("Prompt file {PromptFileName} not in the cache. Loading...", promptFileName);
99+
if (Logger.IsEnabled(LogLevel.Debug))
100+
{
101+
Logger.LogDebug("Prompt file {PromptFileName} not in the cache. Loading...", promptFileName);
102+
}
97103

98104
var filePath = Path.Combine(ProxyUtils.AppFolder!, "prompts", promptFileName);
99105
if (!File.Exists(filePath))
100106
{
101107
throw new FileNotFoundException($"Prompt file '{filePath}' not found.");
102108
}
103109

104-
Logger.LogDebug("Loading prompt file: {FilePath}", filePath);
110+
if (Logger.IsEnabled(LogLevel.Debug))
111+
{
112+
Logger.LogDebug("Loading prompt file: {FilePath}", filePath);
113+
}
105114
var promptContents = File.ReadAllText(filePath);
106115

107116
var prompty = Prompt.FromMarkdown(promptContents);
108117
if (prompty.Prepare(parameters) is not IEnumerable<ChatMessage> promptyMessages ||
109118
!promptyMessages.Any())
110119
{
111-
Logger.LogError("No messages found in the prompt file: {FilePath}", filePath);
120+
if (Logger.IsEnabled(LogLevel.Error))
121+
{
122+
Logger.LogError("No messages found in the prompt file: {FilePath}", filePath);
123+
}
112124
return (null, null);
113125
}
114126

0 commit comments

Comments
 (0)