Skip to content

Commit 0668c09

Browse files
authored
Stabilize logger-based unit tests by enabling fake ILogger in affected suites (#1318)
* Add source-generated logging methods * Validate generated logging changes * Centralize logger messages per project * Remove redundant partial modifier after logging refactor * Extract DomainObject log message to centralized LogMessages * Centralize remaining ILogger log statements in infrastructure and web * Extend [LoggerMessage] source generation to all projects using ILogger * fix: enable logger in migrator tests * fix: enable fake loggers in entities warning-log tests --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent d112aec commit 0668c09

44 files changed

Lines changed: 382 additions & 72 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/extensions/Squidex.Extensions/Actions/Kafka/KafkaProducer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static void LogMessage(ILogger<KafkaProducer> log, LogMessage message)
104104

105105
private static void LogError(ILogger<KafkaProducer> log, Error error)
106106
{
107-
log.LogWarning("Kafka error with {code} and {reason}.", error.Code, error.Reason);
107+
LogMessages.LogKafkaError(log, error.Code, error.Reason);
108108
}
109109

110110
public async Task SendAsync(KafkaMessageRequest job,

backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public async Task EnhanceAsync(UploadAssetCommand command,
8484
}
8585
catch (Exception ex)
8686
{
87-
log.LogError(ex, "Failed to enrich asset.");
87+
LogMessages.LogFailedToEnrichAsset(log, ex);
8888
}
8989
}
9090

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ==========================================================================
2+
// Squidex Headless CMS
3+
// ==========================================================================
4+
// Copyright (c) Squidex UG (haftungsbeschraenkt)
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Squidex.Extensions;
11+
12+
internal static partial class LogMessages
13+
{
14+
[LoggerMessage(Level = LogLevel.Warning, Message = "Kafka error with {code} and {reason}.")]
15+
public static partial void LogKafkaError(ILogger logger, object code, string reason);
16+
17+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to enrich asset.")]
18+
public static partial void LogFailedToEnrichAsset(ILogger logger, Exception exception);
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ==========================================================================
2+
// Squidex Headless CMS
3+
// ==========================================================================
4+
// Copyright (c) Squidex UG (haftungsbeschraenkt)
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Squidex.Infrastructure.Counts;
11+
12+
internal static partial class LogMessages
13+
{
14+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to update count for collection {collection}.")]
15+
public static partial void LogFailedToUpdateCount(ILogger logger, string collection, Exception exception);
16+
}

backend/src/Squidex.Data.MongoDb/Infrastructure/Counts/MongoCountCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private async Task RefreshSilentAsync(string key, long cachedCount, Func<Cancell
5050
}
5151
catch (Exception ex)
5252
{
53-
log.LogError(ex, "Failed to update count for collection {collection}.", collectionName);
53+
LogMessages.LogFailedToUpdateCount(log, collectionName, ex);
5454
}
5555
}
5656

backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public IAsyncEnumerable<JobResult> CreateJobsAsync(Envelope<IEvent> @event, Rule
108108
CreateJobs(@event, context, states, ct)
109109
.Catch(ex =>
110110
{
111-
log.LogError(ex, "Failed to create rule job.");
111+
LogMessages.LogFailedToCreateRuleJob(log, ex);
112112

113113
return states.Select(state => JobResult.Skipped(state.Rule, SkipReason.Failed));
114114
});
@@ -237,7 +237,7 @@ private async IAsyncEnumerable<JobResult> CreateJobs(Envelope<IEvent> @event, Ru
237237
CreateTriggerJobs(typed, triggerHandler, rulesByTrigger, context, ct)
238238
.Catch(ex =>
239239
{
240-
log.LogError(ex, "Failed to create rule jobs from trigger.");
240+
LogMessages.LogFailedToCreateRuleJobsFromTrigger(log, ex);
241241

242242
return states.Select(state => JobResult.Skipped(state.Rule, SkipReason.Failed));
243243
});
@@ -263,7 +263,7 @@ private async IAsyncEnumerable<JobResult> CreateTriggerJobs(Envelope<AppEvent> @
263263
CreateEventJobs(@event, enrichedEvent, triggerHandler, states, context)
264264
.Catch(ex =>
265265
{
266-
log.LogError(ex, "Failed to create rule jobs from event.");
266+
LogMessages.LogFailedToCreateRuleJobsFromEvent(log, ex);
267267

268268
return states.Select(state =>
269269
new JobResult
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ==========================================================================
2+
// Squidex Headless CMS
3+
// ==========================================================================
4+
// Copyright (c) Squidex UG (haftungsbeschraenkt)
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Squidex.Domain.Apps.Core;
11+
12+
internal static partial class LogMessages
13+
{
14+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to create rule job.")]
15+
public static partial void LogFailedToCreateRuleJob(ILogger logger, Exception exception);
16+
17+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to create rule jobs from trigger.")]
18+
public static partial void LogFailedToCreateRuleJobsFromTrigger(ILogger logger, Exception exception);
19+
20+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to create rule jobs from event.")]
21+
public static partial void LogFailedToCreateRuleJobsFromEvent(ILogger logger, Exception exception);
22+
}

backend/src/Squidex.Domain.Apps.Entities/Assets/RecursiveDeleter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async Task PublishAsync(SquidexCommand command)
5858
}
5959
catch (Exception ex)
6060
{
61-
log.LogError(ex, "Failed to delete asset recursively.");
61+
LogMessages.LogFailedToDeleteAssetRecursively(log, ex);
6262
}
6363
}
6464

backend/src/Squidex.Domain.Apps.Entities/Backup/RestoreJob.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public async Task RunAsync(JobRunContext context,
107107
await context.LogAsync(" * Restore all objects like app, schemas and contents");
108108
await context.LogAsync(" * Complete the restore operation for all objects");
109109
await context.FlushAsync();
110-
log.LogInformation("Backup with job id {backupId} with from URL '{url}' started.", context.Job.Id, state.Url);
110+
LogMessages.LogRestoreJobStarted(log, context.Job.Id, state.Url);
111111

112112
state.Reader = await DownloadAsync(context, state, ct);
113113

@@ -147,7 +147,7 @@ public async Task RunAsync(JobRunContext context,
147147
await AssignContributorAsync(context, state);
148148
await context.LogAsync("Completed, Yeah!");
149149

150-
log.LogInformation("Backup with job id {backupId} from URL '{url}' completed.", context.Job.Id, state.Url);
150+
LogMessages.LogRestoreJobCompleted(log, context.Job.Id, state.Url);
151151
}
152152
catch (Exception ex)
153153
{
@@ -168,7 +168,7 @@ public async Task RunAsync(JobRunContext context,
168168

169169
await context.LogAsync(message);
170170

171-
log.LogError(ex, "Backup with job id {backupId} from URL '{url}' failed.", context.Job.Id, state.Url);
171+
LogMessages.LogRestoreJobFailed(log, context.Job.Id, state.Url, ex);
172172
throw;
173173
}
174174
finally
@@ -231,7 +231,7 @@ private async Task CleanupAsync(State state)
231231
}
232232
catch (Exception ex)
233233
{
234-
log.LogError(ex, "Failed to clean up restore.");
234+
LogMessages.LogFailedToCleanUpRestore(log, ex);
235235
}
236236
}
237237
}

backend/src/Squidex.Domain.Apps.Entities/Collaboration/CommentCollaborationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ValueTask OnDocumentLoadedAsync(DocumentLoadEvent @event)
116116
catch (Exception ex)
117117
{
118118
// We are in an extra task, so the exception would be probably swallowed.
119-
log.LogError(ex, "Failed to handle yjs event.");
119+
LogMessages.LogFailedToHandleYjsEvent(log, ex);
120120
throw;
121121
}
122122
});

0 commit comments

Comments
 (0)