Skip to content

Conversation

@tormoder
Copy link

Fixes #996.

@mtmk
Copy link
Member

mtmk commented Nov 10, 2025

Thanks @tormoder good catch! this is fixing some of the logging but exceptions are still coming through with the original URI since the fix is done inside ConnectSocketAsync(uri) call and try/catch blocks wrapping it are still using the old uri. Maybe it can return the uri? edit: that won't work if it's throwing

Copy link
Member

@mtmk mtmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @tormoder, I saw a couple of places exceptions were still reporting the original uri


if (_socketConnection == null)
{
var exception = new NatsException("can not connect uris: " + string.Join(",", uris.Select(x => x.ToString())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is still reporting the original uris

}
catch (Exception ex)
{
_logger.LogError(NatsLogEvents.Connection, ex, "Fail to connect NATS {Url}", uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still reporting the original

}

var connectionFactory = Opts.SocketConnectionFactory ?? (uri.IsWebSocket ? WebSocketFactory.Default : TcpFactory.Default);
_logger.LogInformation(NatsLogEvents.Connection, "Connect to NATS using {FactoryType} {Uri}", connectionFactory.GetType().Name, uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine

}
}

private async Task ConnectSocketAsync(NatsUri uri)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this could return the uri (actually that won't work with exception!)

Comment on lines 448 to 458
var target = (uri.Host, uri.Port);
if (OnConnectingAsync != null)
{
_logger.LogInformation(NatsLogEvents.Connection, "Invoke OnConnectingAsync before connecting to NATS {Uri}", uri);
target = await OnConnectingAsync(target).ConfigureAwait(false);
if (target.Host != uri.Host || target.Port != uri.Port)
{
uri = uri with { Uri = new UriBuilder(uri.Uri) { Host = target.Host, Port = target.Port, }.Uri };
var modifiedUri = new UriBuilder(uri.Uri) { Host = target.Host, Port = target.Port }.Uri;
uri = new NatsUri(modifiedUri.ToString(), uri.IsSeed, uri.Uri.Scheme);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this can be extracted into a method?

@tormoder
Copy link
Author

Thanks, I will take another look.

@tormoder
Copy link
Author

A bit fiddly, but did another attempt.

@mtmk
Copy link
Member

mtmk commented Nov 19, 2025

A bit fiddly, but did another attempt.

thanks @tormoder yes it looks a bit fiddly :) it seems to work though. I will have a closer look once the other PR is merged that fixes the tests as well.

@mtmk mtmk self-assigned this Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants