diff --git a/src/CoreTests/Internal/Sessions/EventTracingConnectionLifetimeTests.cs b/src/CoreTests/Internal/Sessions/EventTracingConnectionLifetimeTests.cs index 74485b5af8..bb36c4ef4e 100644 --- a/src/CoreTests/Internal/Sessions/EventTracingConnectionLifetimeTests.cs +++ b/src/CoreTests/Internal/Sessions/EventTracingConnectionLifetimeTests.cs @@ -15,6 +15,7 @@ using System.Threading; using System.Threading.Tasks; using JasperFx.Descriptors; +using Marten; using Marten.Services; using Weasel.Postgresql; using Xunit; @@ -714,6 +715,65 @@ public async Task ExecuteBatchPagesAsync_Ensure_The_Correct_Event_And_Tags_Are_E .ExecuteBatchPagesAsync(_batchPages, _exceptions, CancellationToken.None); } + [Fact] + public async Task query_session_with_EventTracingConnectionLifetime_uses_storeOptions_logger() + { + var logger = new BatchSuccessRecordingLogger(); + + var store = DocumentStore.For(options => + { + options.Connection(ConnectionSource.ConnectionString); + options.OpenTelemetry.TrackConnections = TrackLevel.Normal; + + options.Logger(logger); + }); + + using var listener = new ActivityListener + { + ShouldListenTo = activitySource => activitySource.Name == "Marten", + Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData, + ActivityStarted = _ => { }, + ActivityStopped = _ => { } + }; + + ActivitySource.AddActivityListener(listener); + + await using var session = store.QuerySession(); + + await session.Events.QueryAllRawEvents().FirstOrDefaultAsync(); + + logger.LastBatch.ShouldNotBeNull(); + } + public class BatchSuccessRecordingLogger: IMartenLogger, IMartenSessionLogger + { + public NpgsqlBatch LastBatch; + + public void LogFailure(Exception ex, string message) { } + + public void RecordSavedChanges(IDocumentSession session, IChangeSet commit) { } + + public void OnBeforeExecute(NpgsqlCommand command) { } + + public void OnBeforeExecute(NpgsqlBatch batch) { } + + public void LogSuccess(NpgsqlCommand command) { } + + public void LogFailure(NpgsqlCommand command, Exception ex) { } + + public void LogSuccess(NpgsqlBatch batch) + { + LastBatch = batch; + } + + public void LogFailure(NpgsqlBatch batch, Exception ex) { } + + public IMartenSessionLogger StartSession(IQuerySession session) + { + return this; + } + + public void SchemaChange(string sql) { } + } } } diff --git a/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs b/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs index b695ccafd5..6d16aa7f9c 100644 --- a/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs +++ b/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs @@ -37,9 +37,9 @@ public EventTracingConnectionLifetime(IConnectionLifetime innerConnectionLifetim throw new ArgumentException("The tenant id cannot be null, an empty string or whitespace.", nameof(tenantId)); } + InnerConnectionLifetime = innerConnectionLifetime; Logger = innerConnectionLifetime.Logger; CommandTimeout = innerConnectionLifetime.CommandTimeout; - InnerConnectionLifetime = innerConnectionLifetime; _telemetryOptions = telemetryOptions; var currentActivity = Activity.Current ?? null; @@ -71,7 +71,7 @@ public void Dispose() InnerConnectionLifetime.Dispose(); } - public IMartenSessionLogger Logger { get; set; } + public IMartenSessionLogger Logger { get => InnerConnectionLifetime.Logger; set => InnerConnectionLifetime.Logger = value; } public int CommandTimeout { get; } public int Execute(NpgsqlCommand cmd) {