added back MqttTrace consuming api

This commit is contained in:
Jan Eggers
2017-10-23 12:42:31 +02:00
parent d10f29bbc6
commit 7e6f4453ad
8 changed files with 192 additions and 2 deletions
+49
View File
@@ -0,0 +1,49 @@
using System;
using Microsoft.Extensions.Logging;
namespace MQTTnet.Core.Diagnostics
{
public class MqttNetLogger : ILogger
{
private readonly string _categoryName;
private readonly MqttNetTrace _mqttNetTrace;
public MqttNetLogger(string categoryName, MqttNetTrace mqttNetTrace)
{
_categoryName = categoryName;
_mqttNetTrace = mqttNetTrace;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
var message = formatter(state, exception);
var traceMessage = new MqttNetTraceMessage(DateTime.Now, Environment.CurrentManagedThreadId, _categoryName, logLevel, message, exception);
_mqttNetTrace.Publish(traceMessage);
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
//not supported: async local requires netstandard1.3
//for implementation see https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Console/ConsoleLogScope.cs
public IDisposable BeginScope<TState>(TState state)
{
return new DisposableScope();
}
private class DisposableScope : IDisposable
{
public void Dispose()
{
}
}
}
}