From dd27344d9721eac1ac170616c9944e9cfa97a8aa Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 25 Aug 2021 11:19:11 +0200 Subject: [PATCH] Logging fixes. --- .../ApplicationInsightsPlugin.cs | 8 ++-- .../Squidex.Extensions/APM/Otlp/OtlpPlugin.cs | 8 ++-- .../StackdriverExceptionHandler.cs | 43 ++++++++++++++----- .../APM/Stackdriver/StackdriverPlugin.cs | 29 ++++++++----- .../Squidex.Extensions.csproj | 2 +- .../Squidex.Infrastructure.csproj | 4 +- backend/src/Squidex/Squidex.csproj | 2 +- 7 files changed, 64 insertions(+), 32 deletions(-) diff --git a/backend/extensions/Squidex.Extensions/APM/ApplicationInsights/ApplicationInsightsPlugin.cs b/backend/extensions/Squidex.Extensions/APM/ApplicationInsights/ApplicationInsightsPlugin.cs index 4ee785de1..0aa0dac45 100644 --- a/backend/extensions/Squidex.Extensions/APM/ApplicationInsights/ApplicationInsightsPlugin.cs +++ b/backend/extensions/Squidex.Extensions/APM/ApplicationInsights/ApplicationInsightsPlugin.cs @@ -16,16 +16,16 @@ namespace Squidex.Extensions.APM.ApplicationInsights { public void ConfigureServices(IServiceCollection services, IConfiguration config) { - services.AddOpenTelemetryTracing(builder => + if (config.GetValue("logging:applicationInsights:enabled")) { - if (config.GetValue("logging:applicationInsights:enabled")) + services.AddOpenTelemetryTracing(builder => { builder.AddAzureMonitorTraceExporter(options => { config.GetSection("logging:applicationInsights").Bind(options); }); - } - }); + }); + } } } } diff --git a/backend/extensions/Squidex.Extensions/APM/Otlp/OtlpPlugin.cs b/backend/extensions/Squidex.Extensions/APM/Otlp/OtlpPlugin.cs index caf4ae0ff..64584dd1b 100644 --- a/backend/extensions/Squidex.Extensions/APM/Otlp/OtlpPlugin.cs +++ b/backend/extensions/Squidex.Extensions/APM/Otlp/OtlpPlugin.cs @@ -17,9 +17,9 @@ namespace Squidex.Extensions.APM.Datadog { public void ConfigureServices(IServiceCollection services, IConfiguration config) { - services.AddOpenTelemetryTracing(builder => + if (config.GetValue("logging:otlp:enabled")) { - if (config.GetValue("logging:otlp:enabled")) + services.AddOpenTelemetryTracing(builder => { // See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); @@ -28,8 +28,8 @@ namespace Squidex.Extensions.APM.Datadog { config.GetSection("logging:otlp").Bind(options); }); - } - }); + }); + } } } } diff --git a/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverExceptionHandler.cs b/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverExceptionHandler.cs index bec3acbc0..7356a0911 100644 --- a/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverExceptionHandler.cs +++ b/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverExceptionHandler.cs @@ -6,33 +6,56 @@ // ========================================================================== using System; -using Google.Cloud.Diagnostics.AspNetCore; +using Google.Cloud.Diagnostics.Common; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Squidex.Infrastructure; using Squidex.Log; namespace Squidex.Extensions.APM.Stackdriver { - internal class StackdriverExceptionHandler : ILogAppender + internal sealed class StackdriverExceptionHandler : ILogAppender { - private readonly DefaultHttpContext fallbackContext = new DefaultHttpContext(); - private readonly IExceptionLogger logger; - private readonly IHttpContextAccessor httpContextAccessor; + private readonly IContextExceptionLogger logger; + private readonly HttpContextWrapper httpContextWrapper; - public StackdriverExceptionHandler(IExceptionLogger logger, IHttpContextAccessor httpContextAccessor) + public sealed class HttpContextWrapper : IContextWrapper + { + private readonly IHttpContextAccessor httpContextAccessor; + + internal HttpContextWrapper(IHttpContextAccessor httpContextAccessor) + { + this.httpContextAccessor = httpContextAccessor; + } + + public string GetHttpMethod() + { + return httpContextAccessor.HttpContext?.Request?.Method?.ToString(); + } + + public string GetUri() + { + return httpContextAccessor.HttpContext?.Request?.GetDisplayUrl(); + } + + public string GetUserAgent() + { + return httpContextAccessor.HttpContext?.Request?.Headers["User-Agent"].ToString(); + } + } + + public StackdriverExceptionHandler(IContextExceptionLogger logger, IHttpContextAccessor httpContextAccessor) { this.logger = logger; - this.httpContextAccessor = httpContextAccessor; + httpContextWrapper = new HttpContextWrapper(httpContextAccessor); } public void Append(IObjectWriter writer, SemanticLogLevel logLevel, Exception exception) { if (exception != null && exception is not DomainException) { - var httpContext = httpContextAccessor.HttpContext; - - logger.Log(exception, httpContext ?? fallbackContext); + logger.Log(exception, httpContextWrapper); } } } diff --git a/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverPlugin.cs b/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverPlugin.cs index c4bf69c28..ba11b2a53 100644 --- a/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverPlugin.cs +++ b/backend/extensions/Squidex.Extensions/APM/Stackdriver/StackdriverPlugin.cs @@ -5,6 +5,8 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Reflection; +using Google.Cloud.Diagnostics.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Trace; @@ -17,21 +19,28 @@ namespace Squidex.Extensions.APM.Stackdriver { public void ConfigureServices(IServiceCollection services, IConfiguration config) { - services.AddOpenTelemetryTracing(builder => + var projectId = config.GetValue("logging:stackdriver:projectId"); + var projectName = config.GetValue("logging:stackdriver:projectName") ?? "Squidex"; + + var isEnabled = config.GetValue("logging:stackdriver:enabled"); + + if (isEnabled && !string.IsNullOrWhiteSpace(projectId)) { - if (config.GetValue("logging:stackdriver:enabled")) + services.AddOpenTelemetryTracing(builder => { - var projectId = config.GetValue("logging:stackdriver:projectId"); - builder.UseStackdriverExporter(projectId); + }); + + services.AddSingleton(); + + services.AddSingleton(); - services.AddSingleton(); + var version = Assembly.GetEntryAssembly().GetName().Version?.ToString(); - services.AddSingleton(); - } - }); + services.AddSingleton(c => ContextExceptionLogger.Create(projectId, projectName, version, null)); + } } } } diff --git a/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj b/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj index 47a5e967b..b38c43a62 100644 --- a/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj +++ b/backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj @@ -14,7 +14,7 @@ - + diff --git a/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj b/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj index 8695875e0..5f6afd114 100644 --- a/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj +++ b/backend/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj @@ -27,8 +27,8 @@ - - + + diff --git a/backend/src/Squidex/Squidex.csproj b/backend/src/Squidex/Squidex.csproj index 31c6742ad..7f91d0610 100644 --- a/backend/src/Squidex/Squidex.csproj +++ b/backend/src/Squidex/Squidex.csproj @@ -71,7 +71,7 @@ - +