From 0ebcf32608fe4207e9f54f53389b3d4f40c4e506 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 22 Mar 2022 21:20:57 +0100 Subject: [PATCH] Even better logs. --- .../Apps/Templates/StringLogger.cs | 23 ++++++++++++++----- .../Templates/TemplateCommandMiddleware.cs | 12 ++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/StringLogger.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/StringLogger.cs index 1d0ae5e145..4f61b8bbfe 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/StringLogger.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/StringLogger.cs @@ -6,23 +6,25 @@ // ========================================================================== using System.Globalization; -using Microsoft.Extensions.Logging; using Squidex.CLI.Commands.Implementation; using Squidex.Infrastructure; -using ILog = Microsoft.Extensions.Logging.ILogger; +using Squidex.Log; namespace Squidex.Domain.Apps.Entities.Apps.Templates { - public sealed class StringLogger : CLI.Commands.Implementation.ILogger, ILogLine + public sealed class StringLogger : ILogger, ILogLine { private const int MaxActionLength = 40; - private readonly ILog log; + private readonly ISemanticLog log; + private readonly string template; private readonly List lines = new List(); private readonly List errors = new List(); private string startedLine = string.Empty; - public StringLogger(ILog log) + public StringLogger(string template, ISemanticLog log) { + this.template = template; + this.log = log; } @@ -30,7 +32,16 @@ public void Dispose() { var mesage = string.Join('\n', lines); - log.LogInformation("CLI executed with full logs {steps}.", mesage); + log.LogInformation(w => w + .WriteProperty("message", $"CLI executed or template {template}.") + .WriteProperty("template", template) + .WriteArray("steps", a => + { + foreach (var line in lines) + { + a.WriteValue(line); + } + })); if (errors.Count > 0) { diff --git a/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/TemplateCommandMiddleware.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/TemplateCommandMiddleware.cs index efd00b199c..be9a60503b 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/TemplateCommandMiddleware.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/Templates/TemplateCommandMiddleware.cs @@ -21,6 +21,7 @@ using Squidex.Domain.Apps.Core; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Infrastructure.Commands; +using Squidex.Log; namespace Squidex.Domain.Apps.Entities.Apps.Templates { @@ -28,13 +29,14 @@ public sealed class TemplateCommandMiddleware : ICommandMiddleware { private readonly TemplatesClient templatesClient; private readonly IUrlGenerator urlGenerator; - private readonly ILogger log; + private readonly ISemanticLog log; public TemplateCommandMiddleware(TemplatesClient templatesClient, IUrlGenerator urlGenerator, - ILogger log) + ISemanticLog log) { this.templatesClient = templatesClient; this.urlGenerator = urlGenerator; + this.log = log; } @@ -59,11 +61,13 @@ private async Task ApplyTemplateAsync(IAppEntity app, string? template) if (string.IsNullOrEmpty(repository)) { - log.LogWarning("Cannot find template {template}.", template); + log.LogWarning(w => w + .WriteProperty("message", "Template not found.") + .WriteProperty("template", template)); return; } - using (var cliLog = new StringLogger(log)) + using (var cliLog = new StringLogger(template, log)) { var session = CreateSession(app);