Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: Connect, Disconnect and TLS metadata #122

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Source/HiveMQtt/Client/HiveMQClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public async Task<ConnectResult> ConnectAsync()
{
this.connectState = ConnectState.Connecting;

Logger.Info("Connecting to broker at {0}:{1}", this.Options.Host, this.Options.Port);

// Fire the corresponding event
this.BeforeConnectEventLauncher(this.Options);

Expand Down Expand Up @@ -129,6 +131,8 @@ public async Task<bool> DisconnectAsync(DisconnectOptions? options = null)

options ??= new DisconnectOptions();

Logger.Info("Disconnecting from broker at {0}:{1}", this.Options.Host, this.Options.Port);

// Fire the corresponding event
this.BeforeDisconnectEventLauncher();

Expand Down
18 changes: 18 additions & 0 deletions Source/HiveMQtt/Client/HiveMQClientSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ private async Task<bool> CreateTLSConnectionAsync(Stream stream)
Logger.Trace("Authenticating TLS connection");
this.stream = new SslStream(stream);
await ((SslStream)this.stream).AuthenticateAsClientAsync(tlsOptions).ConfigureAwait(false);

Logger.Info($"Connected via TLS: {((SslStream)this.stream).IsEncrypted}");
Logger.Debug($"Cipher Algorithm: {((SslStream)this.stream).CipherAlgorithm}");
Logger.Debug($"Cipher Strength: {((SslStream)this.stream).CipherStrength}");
Logger.Debug($"Hash Algorithm: {((SslStream)this.stream).HashAlgorithm}");
Logger.Debug($"Hash Strength: {((SslStream)this.stream).HashStrength}");
Logger.Debug($"Key Exchange Algorithm: {((SslStream)this.stream).KeyExchangeAlgorithm}");
Logger.Debug($"Key Exchange Strength: {((SslStream)this.stream).KeyExchangeStrength}");

var remoteCertificate = ((SslStream)this.stream).RemoteCertificate;
if (remoteCertificate != null)
{
Logger.Info($"Remote Certificate Subject: {remoteCertificate.Subject}");
Logger.Info($"Remote Certificate Issuer: {remoteCertificate.Issuer}");
Logger.Info($"Remote Certificate Serial Number: {remoteCertificate.GetSerialNumberString()}");
}

Logger.Info($"TLS Protocol: {((SslStream)this.stream).SslProtocol}");
return true;
}
catch (Exception e)
Expand Down
6 changes: 5 additions & 1 deletion Source/HiveMQtt/Client/HiveMQClientTrafficProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ private Task<bool> TrafficInflowProcessorAsync(CancellationToken cancellationTok
{
if (decodedPacket is MalformedPacket)
{
Logger.Warn($"Malformed packet received. Disconnecting.");
Logger.Debug($"Malformed packet received: {decodedPacket}");

var opts = new DisconnectOptions
{
ReasonCode = DisconnectReasonCode.MalformedPacket,
Expand Down Expand Up @@ -307,6 +310,7 @@ private Task<bool> ReceivedPacketsProcessorAsync(CancellationToken cancellationT
break;
case DisconnectPacket disconnectPacket:
Logger.Trace($"<-- Disconnect id={disconnectPacket.PacketIdentifier}");
Logger.Warn($"Disconnect received: {disconnectPacket.DisconnectReasonCode} {disconnectPacket.Properties.ReasonString}");
this.OnDisconnectReceivedEventLauncher(disconnectPacket);
break;
case PingRespPacket pingRespPacket:
Expand Down Expand Up @@ -338,7 +342,7 @@ private Task<bool> ReceivedPacketsProcessorAsync(CancellationToken cancellationT
break;
default:
Logger.Trace("<-- Unknown");
Logger.Error($"Unknown packet received: {packet}");
Logger.Error($"Unrecognized packet received. Will discard. {packet}");
break;
} // switch (packet)
}
Expand Down
Loading