提问人:malat 提问时间:11/9/2023 最后编辑:Denis Michealmalat 更新时间:11/9/2023 访问量:54
如何为本机库设置单例日志机制
How to setup a singleton log mechanism for native library
问:
我正在连接到一个 C 库,该库将日志消息调度到回调。
我很难理解如何在 c# () 中与记录器机制交互。using Microsoft.Extensions.Logging
与其考虑完整的 DI 设置(为了示例),不如假设用户以这种方式设置记录器:
% cat Program.cs
ServiceProvider serviceProvider = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder
.AddConsole()
)
.BuildServiceProvider();
var logger = serviceProvider.GetService<ILoggerFactory>()!.CreateLogger<Program>();
如何为我的本机库设置单例记录器机制?
作为参考,记录器实现如下:
public class NativeLoggerForwarder
{
private readonly ILogger _logger;
private readonly Delegate _logDelegate;
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private delegate void LogDelegate(int logLevel, [MarshalAs(UnmanagedType.LPUTF8Str)] string loggerName,
[MarshalAs(UnmanagedType.LPUTF8Str)] string logMessage);
[DllImport("NativeLibrary.dll")]
private static extern void configure_callback(IntPtr aCallback);
private static Delegate ConfigureListener(Action<int, string, string> logMessage)
{
var logDelegate = new LogDelegate(logMessage);
var ptr = Marshal.GetFunctionPointerForDelegate(logDelegate);
configure_callback(ptr);
return logDelegate;
}
private void LogMessage(int logLevel, string loggerName, string message)
{
_logger.Log((LogLevel)logLevel, $"{loggerName} - {message}");
}
public NativeLoggerForwarder(ILogger logger)
{
this._logger = logger;
this._logDelegate = ConfigureListener(this.LogMessage);
}
}
手动实例化将是(一旦记录器实例可用):
var manualInstantiation = new NativeLoggerForwarder(logger);
如何用自动单例 (DI) 替换上述内容?
答: 暂无答案
评论
[...]once a logger instance is available
services.AddSingleton(manualInstantiation);