Fix structured log diagnostics registration

This commit is contained in:
Sipke Schoorstra 2026-05-17 08:42:23 +02:00 committed by GitHub
parent efe4700bd1
commit 263bb18099
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 63 additions and 3 deletions

View file

@ -21,9 +21,9 @@ public static class RelationalStructuredLogsServiceCollectionExtensions
services.TryAddSingleton<RelationalStructuredLogStore>();
services.TryAddSingleton<StructuredLogWriteBuffer>();
services.TryAddSingleton<StructuredLogRetentionService>();
services.AddSingleton<IStructuredLogStore>(sp => sp.GetRequiredService<StructuredLogWriteBuffer>());
services.AddSingleton<IStructuredLogWriteBuffer>(sp => sp.GetRequiredService<StructuredLogWriteBuffer>());
services.AddSingleton<IStructuredLogStorageDiagnostics>(sp => sp.GetRequiredService<StructuredLogWriteBuffer>());
services.Replace(ServiceDescriptor.Singleton<IStructuredLogStore>(sp => sp.GetRequiredService<StructuredLogWriteBuffer>()));
services.Replace(ServiceDescriptor.Singleton<IStructuredLogWriteBuffer>(sp => sp.GetRequiredService<StructuredLogWriteBuffer>()));
services.TryAddEnumerable(ServiceDescriptor.Singleton<IStructuredLogStorageDiagnostics, StructuredLogWriteBufferStorageDiagnostics>());
services.AddHostedService(sp => sp.GetRequiredService<StructuredLogWriteBuffer>());
return services;

View file

@ -0,0 +1,9 @@
using Elsa.Diagnostics.StructuredLogs.Contracts;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts;
namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services;
public class StructuredLogWriteBufferStorageDiagnostics(IStructuredLogWriteBuffer writeBuffer) : IStructuredLogStorageDiagnostics
{
public long DroppedWriteCount => writeBuffer.DroppedWriteCount;
}

View file

@ -0,0 +1,51 @@
using System.Data.Common;
using Elsa.Diagnostics.StructuredLogs.Contracts;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Extensions;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.UnitTests;
public class RelationalStructuredLogServiceCollectionExtensionsTests
{
[Fact]
public async Task AddRelationalStructuredLogPersistence_WhenCalledTwice_DoesNotDuplicateRelationalRegistrations()
{
var services = new ServiceCollection();
services.AddSingleton<IRelationalStructuredLogConnectionFactory, FakeConnectionFactory>();
services.AddSingleton<IRelationalStructuredLogDialect, FakeDialect>();
services.AddRelationalStructuredLogPersistence();
var diagnosticsCount = Count<IStructuredLogStorageDiagnostics>(services);
var storeCount = Count<IStructuredLogStore>(services);
var writeBufferCount = Count<IStructuredLogWriteBuffer>(services);
services.AddRelationalStructuredLogPersistence();
Assert.Equal(diagnosticsCount, Count<IStructuredLogStorageDiagnostics>(services));
Assert.Equal(storeCount, Count<IStructuredLogStore>(services));
Assert.Equal(writeBufferCount, Count<IStructuredLogWriteBuffer>(services));
await using var serviceProvider = services.BuildServiceProvider();
Assert.NotNull(serviceProvider.GetRequiredService<IStructuredLogStorageDiagnostics>());
}
private static int Count<T>(IEnumerable<ServiceDescriptor> services) => services.Count(x => x.ServiceType == typeof(T));
private class FakeConnectionFactory : IRelationalStructuredLogConnectionFactory
{
public ValueTask<DbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
}
private class FakeDialect : IRelationalStructuredLogDialect
{
public string ProviderName => "Fake";
public string ParameterPrefix => "@";
public string QuoteIdentifier(string identifier) => identifier;
public string ApplyLimit(string sql, int limit) => sql;
public string ApplyOffset(string sql, int offset) => sql;
}
}