Skip to content

Commit f147af7

Browse files
Acknowledge over-fixing issue in Abstractions
Co-authored-by: waldekmastykarz <[email protected]>
1 parent cb26bef commit f147af7

File tree

9 files changed

+83
-340
lines changed

9 files changed

+83
-340
lines changed

DevProxy.Abstractions/Data/MSGraphDb.cs

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

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

6761
var (isApiModified, hasErrors) = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken);
6862

6963
if (hasErrors)
7064
{
71-
if (_logger.IsEnabled(LogLevel.Warning))
72-
{
73-
_logger.LogWarning("Unable to update Microsoft Graph database");
74-
}
65+
_logger.LogWarning("Unable to update Microsoft Graph database");
7566
return 1;
7667
}
7768

7869
if (!isApiModified)
7970
{
8071
UpdateLastWriteTime(dbFileInfo);
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-
}
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");
8974
return 1;
9075
}
9176

9277
await LoadOpenAPIFilesAsync(appFolder, cancellationToken);
9378
if (_openApiDocuments.Count < 1)
9479
{
95-
if (_logger.IsEnabled(LogLevel.Debug))
96-
{
97-
_logger.LogDebug("No OpenAPI files found or couldn't load them");
98-
}
80+
_logger.LogDebug("No OpenAPI files found or couldn't load them");
9981
return 1;
10082
}
10183

10284
await CreateDbAsync(cancellationToken);
10385
await FillDataAsync(cancellationToken);
10486

105-
if (_logger.IsEnabled(LogLevel.Information))
106-
{
107-
_logger.LogInformation("Microsoft Graph database is successfully updated");
108-
}
87+
_logger.LogInformation("Microsoft Graph database is successfully updated");
10988
return 0;
11089
}
11190
catch (Exception ex)
11291
{
113-
if (_logger.IsEnabled(LogLevel.Error))
114-
{
115-
_logger.LogError(ex, "Error generating Microsoft Graph database");
116-
}
92+
_logger.LogError(ex, "Error generating Microsoft Graph database");
11793
return 1;
11894
}
11995
}
@@ -124,32 +100,20 @@ public async Task<int> GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke
124100

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

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

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

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

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

166127
SetDbJournaling(false);
167128

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

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

184142
var insertEndpoint = Connection.CreateCommand();
185143
insertEndpoint.CommandText = "INSERT INTO endpoints (path, graphVersion, hasSelect) VALUES (@path, @graphVersion, @hasSelect)";
@@ -192,29 +150,20 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
192150
{
193151
cancellationToken.ThrowIfCancellationRequested();
194152

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

200155
// Get the GET operation for this path
201156
var getOperation = path.Value.Operations.FirstOrDefault(o => o.Key == OperationType.Get).Value;
202157
if (getOperation == null)
203158
{
204-
if (_logger.IsEnabled(LogLevel.Trace))
205-
{
206-
_logger.LogTrace("No GET operation found for {GraphVersion}{Key}", graphVersion, path.Key);
207-
}
159+
_logger.LogTrace("No GET operation found for {GraphVersion}{Key}", graphVersion, path.Key);
208160
continue;
209161
}
210162

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

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

228177
SetDbJournaling(true);
229178

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

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

243186
var isApiUpdated = false;
244187
var hasErrors = false;
@@ -248,35 +191,23 @@ private async Task FillDataAsync(CancellationToken cancellationToken)
248191
try
249192
{
250193
var yamlFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiYamlFileName(version)));
251-
if (_logger.IsEnabled(LogLevel.Debug))
252-
{
253-
_logger.LogDebug("Checking for updated OpenAPI file {File}...", yamlFile);
254-
}
194+
_logger.LogDebug("Checking for updated OpenAPI file {File}...", yamlFile);
255195
if (IsModifiedToday(yamlFile))
256196
{
257-
if (_logger.IsEnabled(LogLevel.Information))
258-
{
259-
_logger.LogInformation("File {File} has already been updated today", yamlFile);
260-
}
197+
_logger.LogInformation("File {File} has already been updated today", yamlFile);
261198
continue;
262199
}
263200

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

270204
var etagFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiEtagFileName(version)));
271205
isApiUpdated |= await DownloadOpenAPIFileAsync(url, yamlFile, etagFile, cancellationToken);
272206
}
273207
catch (Exception ex)
274208
{
275209
hasErrors = true;
276-
if (_logger.IsEnabled(LogLevel.Error))
277-
{
278-
_logger.LogError(ex, "Error updating OpenAPI files");
279-
}
210+
_logger.LogError(ex, "Error updating OpenAPI files");
280211
}
281212
}
282213
return (isApiUpdated, hasErrors);
@@ -301,10 +232,7 @@ private async Task<bool> DownloadOpenAPIFileAsync(string url, FileInfo yamlFile,
301232
if (response.StatusCode == HttpStatusCode.NotModified)
302233
{
303234
UpdateLastWriteTime(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-
}
235+
_logger.LogDebug("File {File} already up-to-date. Updated the last-write-time attribute", yamlFile);
308236
return false;
309237
}
310238

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

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

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

341263
foreach (var version in graphVersions)
342264
{
343265
var filePath = Path.Combine(folder, GetGraphOpenApiYamlFileName(version));
344266
var file = new FileInfo(filePath);
345-
if (_logger.IsEnabled(LogLevel.Debug))
346-
{
347-
_logger.LogDebug("Loading OpenAPI file for {FilePath}...", filePath);
348-
}
267+
_logger.LogDebug("Loading OpenAPI file for {FilePath}...", filePath);
349268

350269
if (!file.Exists)
351270
{
352-
if (_logger.IsEnabled(LogLevel.Debug))
353-
{
354-
_logger.LogDebug("File {FilePath} does not exist", filePath);
355-
}
271+
_logger.LogDebug("File {FilePath} does not exist", filePath);
356272
continue;
357273
}
358274

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

365-
if (_logger.IsEnabled(LogLevel.Debug))
366-
{
367-
_logger.LogDebug("Added OpenAPI file {FilePath} for {Version}", filePath, version);
368-
}
281+
_logger.LogDebug("Added OpenAPI file {FilePath} for {Version}", filePath, version);
369282
}
370283
catch (Exception ex)
371284
{
372-
if (_logger.IsEnabled(LogLevel.Error))
373-
{
374-
_logger.LogError(ex, "Error loading OpenAPI file {FilePath}", filePath);
375-
}
285+
_logger.LogError(ex, "Error loading OpenAPI file {FilePath}", filePath);
376286
}
377287
}
378288
}

DevProxy.Abstractions/LanguageModel/BaseLanguageModelClient.cs

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

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

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

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

10498
var filePath = Path.Combine(ProxyUtils.AppFolder!, "prompts", promptFileName);
10599
if (!File.Exists(filePath))
106100
{
107101
throw new FileNotFoundException($"Prompt file '{filePath}' not found.");
108102
}
109103

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

116107
var prompty = Prompt.FromMarkdown(promptContents);
117108
if (prompty.Prepare(parameters) is not IEnumerable<ChatMessage> promptyMessages ||
118109
!promptyMessages.Any())
119110
{
120-
if (Logger.IsEnabled(LogLevel.Error))
121-
{
122-
Logger.LogError("No messages found in the prompt file: {FilePath}", filePath);
123-
}
111+
Logger.LogError("No messages found in the prompt file: {FilePath}", filePath);
124112
return (null, null);
125113
}
126114

0 commit comments

Comments
 (0)