diff --git a/src/EFCore.Relational/Diagnostics/ConnectionCreatingEventData.cs b/src/EFCore.Relational/Diagnostics/ConnectionCreatingEventData.cs index 685beb61c3d..74ec2bdd92b 100644 --- a/src/EFCore.Relational/Diagnostics/ConnectionCreatingEventData.cs +++ b/src/EFCore.Relational/Diagnostics/ConnectionCreatingEventData.cs @@ -17,20 +17,28 @@ public class ConnectionCreatingEventData : DbContextEventData /// The event definition. /// A delegate that generates a log message for this event. /// The currently being used, to null if not known. + /// The connection string for the new connection, if known. /// A correlation ID that identifies the instance being used. /// The start time of this event. public ConnectionCreatingEventData( EventDefinitionBase eventDefinition, Func messageGenerator, DbContext? context, + string? connectionString, Guid connectionId, DateTimeOffset startTime) : base(eventDefinition, messageGenerator, context) { + ConnectionString = connectionString; ConnectionId = connectionId; StartTime = startTime; } + /// + /// The connection string for the new connection, if known. + /// + public virtual string? ConnectionString { get; } + /// /// A correlation ID that identifies the instance being used. /// diff --git a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs index f261d19024b..5777768e8a0 100644 --- a/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs +++ b/src/EFCore.Relational/Diagnostics/Internal/RelationalConnectionDiagnosticsLogger.cs @@ -922,6 +922,7 @@ public virtual InterceptionResult ConnectionCreating( var eventData = BroadcastConnectionCreating( connection.Context, + connection.ConnectionString, connection.ConnectionId, startTime, definition, @@ -939,6 +940,7 @@ public virtual InterceptionResult ConnectionCreating( private ConnectionCreatingEventData BroadcastConnectionCreating( DbContext? context, + string? connectionString, Guid connectionId, DateTimeOffset startTime, EventDefinition definition, @@ -949,6 +951,7 @@ private ConnectionCreatingEventData BroadcastConnectionCreating( definition, ConnectionCreating, context, + connectionString, connectionId, startTime); diff --git a/test/EFCore.Relational.Specification.Tests/ConnectionInterceptionTestBase.cs b/test/EFCore.Relational.Specification.Tests/ConnectionInterceptionTestBase.cs index 6e842f214f4..ca53ff8619b 100644 --- a/test/EFCore.Relational.Specification.Tests/ConnectionInterceptionTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/ConnectionInterceptionTestBase.cs @@ -237,6 +237,7 @@ public virtual async Task Intercept_connection_creation_passively(bool async) Assert.False(interceptor.DisposedCalled); Assert.Same(context, interceptor.Context); Assert.Same(connection, interceptor.Connection); + Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? ""); if (async) { @@ -284,6 +285,7 @@ public virtual async Task Intercept_connection_to_override_creation(bool async) Assert.False(interceptor.DisposedCalled); Assert.Same(context, interceptor.Context); Assert.Same(connection, interceptor.Connection); + Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? ""); if (async) { @@ -331,6 +333,7 @@ public virtual async Task Intercept_connection_to_override_connection_after_crea Assert.False(interceptor.DisposedCalled); Assert.Same(context, interceptor.Context); Assert.Same(connection, interceptor.Connection); + Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? ""); if (async) { @@ -374,6 +377,7 @@ public virtual async Task Intercept_connection_to_suppress_dispose(bool async) Assert.False(interceptor.DisposedCalled); Assert.Same(context, interceptor.Context); Assert.Same(connection, interceptor.Connection); + Assert.Equal(connection.ConnectionString, interceptor.ConnectionString ?? ""); if (async) { @@ -455,6 +459,7 @@ public virtual async Task Intercept_connection_creation_with_multiple_intercepto protected class ConnectionCreationInterceptor : IDbConnectionInterceptor { public DbContext? Context { get; set; } + public string? ConnectionString { get; set; } public DbConnection? Connection { get; set; } public Guid ConnectionId { get; set; } public DateTimeOffset StartTime { get; set; } @@ -474,6 +479,7 @@ public virtual InterceptionResult ConnectionCreating( Assert.NotEqual(default, eventData.StartTime); Context = eventData.Context; + ConnectionString = eventData.ConnectionString; ConnectionId = eventData.ConnectionId; StartTime = eventData.StartTime; CreatingCalled = true; @@ -618,7 +624,7 @@ public ConnectionStringContext(Func Interceptors { get; } = new List(); + public List Interceptors { get; } = new(); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => _configureProvider(optionsBuilder).AddInterceptors(Interceptors); diff --git a/test/EFCore.SqlServer.FunctionalTests/ConnectionInterceptionSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/ConnectionInterceptionSqlServerTest.cs index 1014ea6e610..dc34876cf0b 100644 --- a/test/EFCore.SqlServer.FunctionalTests/ConnectionInterceptionSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/ConnectionInterceptionSqlServerTest.cs @@ -83,6 +83,24 @@ protected override bool ShouldSubscribeToDiagnosticListener } } + public class ConnectionInterceptionWithConnectionStringSqlServerTest + : ConnectionInterceptionSqlServerTestBase, IClassFixture + { + public ConnectionInterceptionWithConnectionStringSqlServerTest(InterceptionSqlServerFixture fixture) + : base(fixture) + { + } + + public class InterceptionSqlServerFixture : InterceptionSqlServerFixtureBase + { + protected override bool ShouldSubscribeToDiagnosticListener + => false; + } + + protected override DbContextOptionsBuilder ConfigureProvider(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseSqlServer("Database=Dummy"); + } + public class ConnectionInterceptionWithDiagnosticsSqlServerTest : ConnectionInterceptionSqlServerTestBase, IClassFixture