114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
|
|
using Microsoft.Extensions.Logging;
|
|
using StellaOps.Plugin.Abstractions.Context;
|
|
|
|
namespace StellaOps.Plugin.Host.Context;
|
|
|
|
/// <summary>
|
|
/// Default implementation of IPluginLogger that wraps a Microsoft.Extensions.Logging logger.
|
|
/// </summary>
|
|
public sealed class PluginLogger : IPluginLogger
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly string _pluginId;
|
|
private readonly Dictionary<string, object> _properties;
|
|
|
|
/// <summary>
|
|
/// Creates a new plugin logger.
|
|
/// </summary>
|
|
/// <param name="logger">The underlying logger.</param>
|
|
/// <param name="pluginId">The plugin ID for context.</param>
|
|
public PluginLogger(ILogger logger, string pluginId)
|
|
: this(logger, pluginId, new Dictionary<string, object>())
|
|
{
|
|
}
|
|
|
|
private PluginLogger(ILogger logger, string pluginId, Dictionary<string, object> properties)
|
|
{
|
|
_logger = logger;
|
|
_pluginId = pluginId;
|
|
_properties = properties;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Log(LogLevel level, string message, params object[] args)
|
|
{
|
|
_logger.Log(level, message, args);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Log(LogLevel level, Exception exception, string message, params object[] args)
|
|
{
|
|
_logger.Log(level, exception, message, args);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IPluginLogger WithProperty(string name, object value)
|
|
{
|
|
var newProperties = new Dictionary<string, object>(_properties)
|
|
{
|
|
[name] = value
|
|
};
|
|
|
|
// Create a logger that includes the property in scope
|
|
return new PropertyScopedPluginLogger(_logger, _pluginId, newProperties);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IPluginLogger ForOperation(string operationName)
|
|
{
|
|
return WithProperty("Operation", operationName);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool IsEnabled(LogLevel level) => _logger.IsEnabled(level);
|
|
|
|
/// <summary>
|
|
/// Plugin logger with property scope support.
|
|
/// </summary>
|
|
private sealed class PropertyScopedPluginLogger : IPluginLogger
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly string _pluginId;
|
|
private readonly Dictionary<string, object> _properties;
|
|
|
|
public PropertyScopedPluginLogger(
|
|
ILogger logger,
|
|
string pluginId,
|
|
Dictionary<string, object> properties)
|
|
{
|
|
_logger = logger;
|
|
_pluginId = pluginId;
|
|
_properties = properties;
|
|
}
|
|
|
|
public void Log(LogLevel level, string message, params object[] args)
|
|
{
|
|
using var scope = _logger.BeginScope(_properties);
|
|
_logger.Log(level, message, args);
|
|
}
|
|
|
|
public void Log(LogLevel level, Exception exception, string message, params object[] args)
|
|
{
|
|
using var scope = _logger.BeginScope(_properties);
|
|
_logger.Log(level, exception, message, args);
|
|
}
|
|
|
|
public IPluginLogger WithProperty(string name, object value)
|
|
{
|
|
var newProperties = new Dictionary<string, object>(_properties)
|
|
{
|
|
[name] = value
|
|
};
|
|
return new PropertyScopedPluginLogger(_logger, _pluginId, newProperties);
|
|
}
|
|
|
|
public IPluginLogger ForOperation(string operationName)
|
|
{
|
|
return WithProperty("Operation", operationName);
|
|
}
|
|
|
|
public bool IsEnabled(LogLevel level) => _logger.IsEnabled(level);
|
|
}
|
|
}
|