Refactor tracing

This commit is contained in:
Christian Kratky
2017-10-20 23:30:58 +02:00
parent fe88b7551c
commit 11ade8c3df
36 changed files with 497 additions and 321 deletions
@@ -1,6 +1,7 @@
using System;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Client;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Serializer;
namespace MQTTnet.Implementations
@@ -13,12 +14,14 @@ namespace MQTTnet.Implementations
if (options is MqttClientTcpOptions tcpOptions)
{
return new MqttChannelCommunicationAdapter(new MqttTcpChannel(tcpOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion });
var trace = new MqttNetTrace();
return new MqttChannelCommunicationAdapter(new MqttTcpChannel(tcpOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace);
}
if (options is MqttClientWebSocketOptions webSocketOptions)
{
return new MqttChannelCommunicationAdapter(new MqttWebSocketChannel(webSocketOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion });
var trace = new MqttNetTrace();
return new MqttChannelCommunicationAdapter(new MqttWebSocketChannel(webSocketOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace);
}
throw new NotSupportedException();
@@ -10,8 +10,14 @@ namespace MQTTnet.Implementations
{
public class MqttServerAdapter : IMqttServerAdapter, IDisposable
{
private readonly MqttNetTrace _trace;
private StreamSocketListener _defaultEndpointSocket;
public MqttServerAdapter(MqttNetTrace trace)
{
_trace = trace ?? throw new ArgumentNullException(nameof(trace));
}
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
public async Task StartAsync(MqttServerOptions options)
@@ -50,12 +56,12 @@ namespace MQTTnet.Implementations
{
try
{
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new MqttPacketSerializer());
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new MqttPacketSerializer(), _trace);
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter));
}
catch (Exception exception)
{
MqttNetTrace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint.");
_trace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint.");
}
}
}
@@ -1,4 +1,5 @@
using MQTTnet.Core.Client;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.ManagedClient;
using MQTTnet.Implementations;
@@ -6,14 +7,14 @@ namespace MQTTnet
{
public class MqttClientFactory : IMqttClientFactory
{
public IMqttClient CreateMqttClient()
public IMqttClient CreateMqttClient(IMqttNetTraceHandler traceHandler = null)
{
return new MqttClient(new MqttCommunicationAdapterFactory());
return new MqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler));
}
public ManagedMqttClient CreateManagedMqttClient()
public ManagedMqttClient CreateManagedMqttClient(IMqttNetTraceHandler traceHandler = null)
{
return new ManagedMqttClient(new MqttCommunicationAdapterFactory());
return new ManagedMqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler));
}
}
}
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Server;
using MQTTnet.Implementations;
@@ -8,11 +9,12 @@ namespace MQTTnet
{
public class MqttServerFactory : IMqttServerFactory
{
public IMqttServer CreateMqttServer(MqttServerOptions options)
public IMqttServer CreateMqttServer(MqttServerOptions options, IMqttNetTraceHandler traceHandler = null)
{
if (options == null) throw new ArgumentNullException(nameof(options));
return new MqttServer(options, new List<IMqttServerAdapter> { new MqttServerAdapter() });
var trace = new MqttNetTrace(traceHandler);
return new MqttServer(options, new List<IMqttServerAdapter> { new MqttServerAdapter(trace) }, trace);
}
}
}