feat: switching to logging via OpenTelemetry
This commit is contained in:
@@ -24,15 +24,16 @@ public class LoggingRequest
|
|||||||
public string? LogFilePath { get; set; }
|
public string? LogFilePath { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the API key for integrating with Seq, a log aggregation service.
|
/// Gets or sets the endpoint URL for the OpenTelemetry Collector.
|
||||||
/// If provided, logs will be sent to a Seq server using this API key.
|
/// This property specifies the OTLP endpoint to which logs will be sent.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? ApiKeySeq { get; set; }
|
public string? OpenTelemetryEndpoint { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the server URL for the Seq logging service.
|
/// Gets or sets the logical service name used for OpenTelemetry logging.
|
||||||
/// This property specifies the Seq server endpoint to which logs will be sent.
|
/// This name will be attached to log entries as the "service.name" resource attribute,
|
||||||
/// If <see cref="ApiKeySeq"/> is provided, logs will be sent to this server.
|
/// allowing logs to be grouped and filtered by service in backends like Loki or Grafana.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? ApiServerSeq { get; set; }
|
public string? OpenTelemetryServiceName { get; set; }
|
||||||
|
|
||||||
}
|
}
|
@@ -8,7 +8,7 @@ using Serilog.Context;
|
|||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Serilog.Filters;
|
using Serilog.Filters;
|
||||||
using Serilog.Formatting.Compact;
|
using Serilog.Formatting.Compact;
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
@@ -46,9 +46,18 @@ public static class LoggerConfiguration
|
|||||||
rollingInterval: RollingInterval.Day);
|
rollingInterval: RollingInterval.Day);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (generalConfig != null && !string.IsNullOrEmpty(generalConfig.ApiServerSeq) &&
|
if (!string.IsNullOrEmpty(generalConfig?.OpenTelemetryEndpoint)
|
||||||
Uri.TryCreate(generalConfig.ApiServerSeq, UriKind.Absolute, out var _))
|
&& !string.IsNullOrEmpty(generalConfig.OpenTelemetryServiceName))
|
||||||
configuration.WriteTo.Seq(generalConfig.ApiServerSeq, apiKey: generalConfig.ApiKeySeq);
|
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
|
configuration
|
||||||
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
||||||
|
@@ -9,13 +9,13 @@ public class LogSettings : IIsConfigured
|
|||||||
public bool EnableLogToFile { get; set; }
|
public bool EnableLogToFile { get; set; }
|
||||||
public string? LogFilePath { get; set; }
|
public string? LogFilePath { get; set; }
|
||||||
public string? LogFileName { get; set; }
|
public string? LogFileName { get; set; }
|
||||||
public string? ApiKeySeq { get; set; }
|
public string? OpenTelemetryEndpoint { get; set; }
|
||||||
public string? ApiServerSeq { get; set; }
|
public string? OpenTelemetryServiceName { get; set; }
|
||||||
|
|
||||||
public bool IsConfigured()
|
public bool IsConfigured()
|
||||||
{
|
{
|
||||||
return !EnableLogToFile ||
|
return !EnableLogToFile
|
||||||
!string.IsNullOrEmpty(LogFilePath) &&
|
|| !string.IsNullOrEmpty(LogFilePath)
|
||||||
!string.IsNullOrEmpty(LogFileName);
|
&& !string.IsNullOrEmpty(LogFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -23,6 +23,7 @@ using Mirea.Api.Security.Services;
|
|||||||
using MySqlConnector;
|
using MySqlConnector;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Sinks.OpenTelemetry;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
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.OpenTelemetryEndpoint = request.OpenTelemetryEndpoint;
|
||||||
settings.ApiKeySeq = request.ApiKeySeq;
|
settings.OpenTelemetryServiceName = request.OpenTelemetryServiceName;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Log.Logger = new LoggerConfiguration()
|
using var innerLogger = new LoggerConfiguration()
|
||||||
.WriteTo.Seq(settings.ApiServerSeq, apiKey: settings.ApiKeySeq)
|
.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();
|
.CreateLogger();
|
||||||
|
|
||||||
Log.Warning("Testing configuration Seq.");
|
innerLogger.Warning("🚀 Testing OpenTelemetry log delivery.");
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// ignoring
|
Console.WriteLine("Error sending log to OTEL Collector: " + ex.Message);
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Log.CloseAndFlush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,9 +483,7 @@ public class SetupController(
|
|||||||
{
|
{
|
||||||
EnableLogToFile = settings.EnableLogToFile,
|
EnableLogToFile = settings.EnableLogToFile,
|
||||||
LogFileName = settings.LogFileName,
|
LogFileName = settings.LogFileName,
|
||||||
LogFilePath = settings.LogFilePath,
|
LogFilePath = settings.LogFilePath
|
||||||
ApiKeySeq = settings.ApiKeySeq,
|
|
||||||
ApiServerSeq = settings.ApiServerSeq
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@@ -53,7 +53,7 @@
|
|||||||
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
|
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.8" />
|
<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="StackExchange.Redis" Version="2.8.58" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="9.0.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="9.0.3" />
|
||||||
|
Reference in New Issue
Block a user