mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
Refactor tracing
This commit is contained in:
@@ -10,18 +10,20 @@ namespace MQTTnet.Core.Server
|
||||
{
|
||||
public sealed class MqttServer : IMqttServer
|
||||
{
|
||||
private readonly MqttNetTrace _trace;
|
||||
private readonly MqttClientSessionsManager _clientSessionsManager;
|
||||
private readonly ICollection<IMqttServerAdapter> _adapters;
|
||||
private readonly MqttServerOptions _options;
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
public MqttServer(MqttServerOptions options, ICollection<IMqttServerAdapter> adapters)
|
||||
public MqttServer(MqttServerOptions options, ICollection<IMqttServerAdapter> adapters, MqttNetTrace trace)
|
||||
{
|
||||
_trace = trace ?? throw new ArgumentNullException(nameof(trace));
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
_adapters = adapters ?? throw new ArgumentNullException(nameof(adapters));
|
||||
|
||||
_clientSessionsManager = new MqttClientSessionsManager(options);
|
||||
_clientSessionsManager = new MqttClientSessionsManager(options, trace);
|
||||
_clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e);
|
||||
_clientSessionsManager.ClientConnected += OnClientConnected;
|
||||
_clientSessionsManager.ClientDisconnected += OnClientDisconnected;
|
||||
@@ -57,7 +59,7 @@ namespace MQTTnet.Core.Server
|
||||
await adapter.StartAsync(_options);
|
||||
}
|
||||
|
||||
MqttNetTrace.Information(nameof(MqttServer), "Started.");
|
||||
_trace.Information(nameof(MqttServer), "Started.");
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
@@ -74,7 +76,7 @@ namespace MQTTnet.Core.Server
|
||||
|
||||
_clientSessionsManager.Clear();
|
||||
|
||||
MqttNetTrace.Information(nameof(MqttServer), "Stopped.");
|
||||
_trace.Information(nameof(MqttServer), "Stopped.");
|
||||
}
|
||||
|
||||
private void OnClientAccepted(object sender, MqttServerAdapterClientAcceptedEventArgs eventArgs)
|
||||
@@ -84,13 +86,13 @@ namespace MQTTnet.Core.Server
|
||||
|
||||
private void OnClientConnected(object sender, MqttClientConnectedEventArgs eventArgs)
|
||||
{
|
||||
MqttNetTrace.Information(nameof(MqttServer), "Client '{0}': Connected.", eventArgs.Client.ClientId);
|
||||
_trace.Information(nameof(MqttServer), "Client '{0}': Connected.", eventArgs.Client.ClientId);
|
||||
ClientConnected?.Invoke(this, eventArgs);
|
||||
}
|
||||
|
||||
private void OnClientDisconnected(object sender, MqttClientDisconnectedEventArgs eventArgs)
|
||||
{
|
||||
MqttNetTrace.Information(nameof(MqttServer), "Client '{0}': Disconnected.", eventArgs.Client.ClientId);
|
||||
_trace.Information(nameof(MqttServer), "Client '{0}': Disconnected.", eventArgs.Client.ClientId);
|
||||
ClientDisconnected?.Invoke(this, eventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user