mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
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()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
} |