efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

RelationalConnectionDiagnosticsLogger.LogConnectionError doesn't log Server name when a connection cannot be opened

Open StasPerekrestov opened this issue 3 years ago • 0 comments

Hello,

When a connection string has a server name incorrectly specified, e.g. Server=incorrect_server_name_or_a_typo;Port=5432;Database=mydb;User Id=user1;....

Then EF fails to open a DB connection, and the following code is invoked: RelationalConnectionDiagnosticsLogger.LogConnectionError

private void LogConnectionError(
            IRelationalConnection connection,
            EventDefinition<string, string> definition)
        {
            if (ShouldLog(definition))
            {
                definition.Log(this, connection.DbConnection.Database, connection.DbConnection.DataSource);
            }
        }

The issue is that in this case connection.DbConnection.DataSource returns an empty string, and it results in the following An error occurred using the connection to database 'core-api' on server ''. error message. In other words, the message omits the server name from the connection string.

public sealed class NpgsqlConnection : DbConnection, ICloneable, IComponent
{
//...
 public override string DataSource => Connector?.Settings.DataSourceCached ?? string.Empty;
//...
}

// internal NpgsqlConnector? Connector { get; set; } - is null

Thank you.

StasPerekrestov avatar Aug 16 '22 18:08 StasPerekrestov