From 46046d589ec509b15987f3c74b07c250567fa63f Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 5 Aug 2025 15:47:35 +0300 Subject: [PATCH] feat: switching to logging via OpenTelemetry --- .../Requests/Configuration/LoggingRequest.cs | 15 ++++---- .../Core/Startup/LoggerConfiguration.cs | 17 ++++++--- .../Model/GeneralSettings/LogSettings.cs | 10 +++--- .../SetupConfiguration/SetupController.cs | 35 +++++++++++-------- Endpoint/Endpoint.csproj | 2 +- 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/ApiDto/Requests/Configuration/LoggingRequest.cs b/ApiDto/Requests/Configuration/LoggingRequest.cs index 3108f8a..541b5c1 100644 --- a/ApiDto/Requests/Configuration/LoggingRequest.cs +++ b/ApiDto/Requests/Configuration/LoggingRequest.cs @@ -24,15 +24,16 @@ public class LoggingRequest public string? LogFilePath { get; set; } /// - /// Gets or sets the API key for integrating with Seq, a log aggregation service. - /// If provided, logs will be sent to a Seq server using this API key. + /// Gets or sets the endpoint URL for the OpenTelemetry Collector. + /// This property specifies the OTLP endpoint to which logs will be sent. /// - public string? ApiKeySeq { get; set; } + public string? OpenTelemetryEndpoint { get; set; } /// - /// Gets or sets the server URL for the Seq logging service. - /// This property specifies the Seq server endpoint to which logs will be sent. - /// If is provided, logs will be sent to this server. + /// Gets or sets the logical service name used for OpenTelemetry logging. + /// This name will be attached to log entries as the "service.name" resource attribute, + /// allowing logs to be grouped and filtered by service in backends like Loki or Grafana. /// - public string? ApiServerSeq { get; set; } + public string? OpenTelemetryServiceName { get; set; } + } \ No newline at end of file diff --git a/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs b/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs index 636c650..aadb912 100644 --- a/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs +++ b/Endpoint/Configuration/Core/Startup/LoggerConfiguration.cs @@ -8,7 +8,7 @@ using Serilog.Context; using Serilog.Events; using Serilog.Filters; using Serilog.Formatting.Compact; -using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -46,9 +46,18 @@ public static class LoggerConfiguration rollingInterval: RollingInterval.Day); } - if (generalConfig != null && !string.IsNullOrEmpty(generalConfig.ApiServerSeq) && - Uri.TryCreate(generalConfig.ApiServerSeq, UriKind.Absolute, out var _)) - configuration.WriteTo.Seq(generalConfig.ApiServerSeq, apiKey: generalConfig.ApiKeySeq); + if (!string.IsNullOrEmpty(generalConfig?.OpenTelemetryEndpoint) + && !string.IsNullOrEmpty(generalConfig.OpenTelemetryServiceName)) + configuration.WriteTo.OpenTelemetry(options => + { + options.Endpoint = generalConfig.OpenTelemetryEndpoint; + options.Protocol = Serilog.Sinks.OpenTelemetry.OtlpProtocol.Grpc; + options.ResourceAttributes = new Dictionary + { + ["service.name"] = generalConfig.OpenTelemetryServiceName, + ["deployment.environment"] = context.HostingEnvironment.EnvironmentName + }; + }); configuration .MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning) diff --git a/Endpoint/Configuration/Model/GeneralSettings/LogSettings.cs b/Endpoint/Configuration/Model/GeneralSettings/LogSettings.cs index c5fafe0..b1d1c26 100644 --- a/Endpoint/Configuration/Model/GeneralSettings/LogSettings.cs +++ b/Endpoint/Configuration/Model/GeneralSettings/LogSettings.cs @@ -9,13 +9,13 @@ public class LogSettings : IIsConfigured public bool EnableLogToFile { get; set; } public string? LogFilePath { get; set; } public string? LogFileName { get; set; } - public string? ApiKeySeq { get; set; } - public string? ApiServerSeq { get; set; } + public string? OpenTelemetryEndpoint { get; set; } + public string? OpenTelemetryServiceName { get; set; } public bool IsConfigured() { - return !EnableLogToFile || - !string.IsNullOrEmpty(LogFilePath) && - !string.IsNullOrEmpty(LogFileName); + return !EnableLogToFile + || !string.IsNullOrEmpty(LogFilePath) + && !string.IsNullOrEmpty(LogFileName); } } \ No newline at end of file diff --git a/Endpoint/Controllers/SetupConfiguration/SetupController.cs b/Endpoint/Controllers/SetupConfiguration/SetupController.cs index 4477a0c..d154504 100644 --- a/Endpoint/Controllers/SetupConfiguration/SetupController.cs +++ b/Endpoint/Controllers/SetupConfiguration/SetupController.cs @@ -23,6 +23,7 @@ using Mirea.Api.Security.Services; using MySqlConnector; using Npgsql; using Serilog; +using Serilog.Sinks.OpenTelemetry; using StackExchange.Redis; using System; using System.Collections.Generic; @@ -434,26 +435,32 @@ public class SetupController( } }; - if (!string.IsNullOrEmpty(request?.ApiServerSeq)) + if (!string.IsNullOrEmpty(request?.OpenTelemetryEndpoint) + && !string.IsNullOrEmpty(request.OpenTelemetryServiceName)) { - settings.ApiServerSeq = request.ApiServerSeq; - settings.ApiKeySeq = request.ApiKeySeq; + settings.OpenTelemetryEndpoint = request.OpenTelemetryEndpoint; + settings.OpenTelemetryServiceName = request.OpenTelemetryServiceName; try { - Log.Logger = new LoggerConfiguration() - .WriteTo.Seq(settings.ApiServerSeq, apiKey: settings.ApiKeySeq) + using var innerLogger = new LoggerConfiguration() + .WriteTo.OpenTelemetry(options => + { + options.Endpoint = settings.OpenTelemetryEndpoint; + options.Protocol = OtlpProtocol.Grpc; + options.ResourceAttributes = new Dictionary + { + ["service.name"] = settings.OpenTelemetryServiceName, + ["deployment.environment"] = "test" + }; + }) .CreateLogger(); - Log.Warning("Testing configuration Seq."); + innerLogger.Warning("🚀 Testing OpenTelemetry log delivery."); } - catch + catch (Exception ex) { - // ignoring - } - finally - { - Log.CloseAndFlush(); + Console.WriteLine("Error sending log to OTEL Collector: " + ex.Message); } } @@ -476,9 +483,7 @@ public class SetupController( { EnableLogToFile = settings.EnableLogToFile, LogFileName = settings.LogFileName, - LogFilePath = settings.LogFilePath, - ApiKeySeq = settings.ApiKeySeq, - ApiServerSeq = settings.ApiServerSeq + LogFilePath = settings.LogFilePath }); return true; diff --git a/Endpoint/Endpoint.csproj b/Endpoint/Endpoint.csproj index 9cf929c..f5bea67 100644 --- a/Endpoint/Endpoint.csproj +++ b/Endpoint/Endpoint.csproj @@ -53,7 +53,7 @@ - +