feat: switching to logging via OpenTelemetry

This commit is contained in:
2025-08-05 15:47:35 +03:00
parent 802acad570
commit 46046d589e
5 changed files with 47 additions and 32 deletions

View File

@@ -24,15 +24,16 @@ public class LoggingRequest
public string? LogFilePath { get; set; }
/// <summary>
/// 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.
/// </summary>
public string? ApiKeySeq { get; set; }
public string? OpenTelemetryEndpoint { get; set; }
/// <summary>
/// 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 <see cref="ApiKeySeq"/> 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.
/// </summary>
public string? ApiServerSeq { get; set; }
public string? OpenTelemetryServiceName { get; set; }
}

View File

@@ -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<string, object>
{
["service.name"] = generalConfig.OpenTelemetryServiceName,
["deployment.environment"] = context.HostingEnvironment.EnvironmentName
};
});
configuration
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)

View File

@@ -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);
}
}

View File

@@ -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<string, object>
{
["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;

View File

@@ -53,7 +53,7 @@
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.8" />
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.58" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="9.0.3" />