Skip to content

Wrong NATS URI used for logging (and exceptions) when using address from OnConnectingAsync hook #996

@tormoder

Description

@tormoder

Observed behavior

When using the OnConncetAsync hook, exceptions and logs show the original URI (from NatsOpts) because ToString() returns the cached NatsUri _redacted value from before the modification.

This is especially confusing if connecting fails, because logs and error messages e.g. says the default localhost:4222 was used, and leads you to thinking that the address from the hook was not applied/used.

uri = uri with { Uri = new UriBuilder(uri.Uri) { Host = target.Host, Port = target.Port, }.Uri };

  • The with expression creates a new NatsUri record with only the Uri property changed.
  • _redacted field in NatsUri is only set in the constructor.
  • Using with, the _redacted field is shallow copied from the original instance.
  • ToString() returns the original URI string, not the modified one.

Expected behavior

When OnConnectingAsync changes the host/port, exceptions and logs should show the modified URI that was actually used for connection.

Server and client version

NATS Server: 2.11
NATS.NET: 2.6.6

Host environment

N/A

Steps to reproduce

  1. Set the OnConnectingAsync hook to return a host+port for which a connection will fail (but different from localhost:4222 or your NatsOpts value).
  2. Observe URI used for log statements in ConnectSocketAsync.
  3. Look at URI used in exception thrown when connection fails.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions