NLogAdapter.cs•2.69 kB
/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
using System;
using Microsoft.Extensions.Logging;
using NLog;
namespace com.IvanMurzak.Unity.MCP.Server
{
internal class NLogAdapter : Microsoft.Extensions.Logging.ILogger
{
private readonly Logger _nLogLogger;
public NLogAdapter(string categoryName)
{
_nLogLogger = LogManager.GetLogger(categoryName);
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
=> null;
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
=> _nLogLogger.IsEnabled(ConvertLogLevel(logLevel));
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (formatter == null) return;
var nLogLevel = ConvertLogLevel(logLevel);
if (!_nLogLogger.IsEnabled(nLogLevel)) return;
var message = formatter(state, exception);
_nLogLogger.Log(nLogLevel, exception, message);
}
private static NLog.LogLevel ConvertLogLevel(Microsoft.Extensions.Logging.LogLevel logLevel)
{
return logLevel switch
{
Microsoft.Extensions.Logging.LogLevel.Trace => NLog.LogLevel.Trace,
Microsoft.Extensions.Logging.LogLevel.Debug => NLog.LogLevel.Debug,
Microsoft.Extensions.Logging.LogLevel.Information => NLog.LogLevel.Info,
Microsoft.Extensions.Logging.LogLevel.Warning => NLog.LogLevel.Warn,
Microsoft.Extensions.Logging.LogLevel.Error => NLog.LogLevel.Error,
Microsoft.Extensions.Logging.LogLevel.Critical => NLog.LogLevel.Fatal,
Microsoft.Extensions.Logging.LogLevel.None => NLog.LogLevel.Off,
_ => NLog.LogLevel.Info
};
}
}
}