C# 如何从.net Framework初始化.net Core ILogger或ILogger Factory,并将其注入.net Core内置类库中的构造函数

C# 如何从.net Framework初始化.net Core ILogger或ILogger Factory,并将其注入.net Core内置类库中的构造函数,c#,.net,C#,.net,我正在使用一个使用.NETCore构建的类库。该库只有一个公共类,其构造函数接受ILogger工厂,另一个重载构造函数接受ILogger。但我在使用.NETFramework4.7构建的控制台应用程序中使用这个库。我的控制台应用程序使用Log4Net进行日志记录。但是我需要注入ILogger或ILogger工厂的实例,以便类库根据我的log.config设置记录错误 我试了如下 ILogger logger=新建LoggerFactory().CreateLogger(); var conten

我正在使用一个使用.NETCore构建的类库。该库只有一个公共类,其构造函数接受ILogger工厂,另一个重载构造函数接受ILogger。但我在使用.NETFramework4.7构建的控制台应用程序中使用这个库。我的控制台应用程序使用Log4Net进行日志记录。但是我需要注入ILogger或ILogger工厂的实例,以便类库根据我的log.config设置记录错误

我试了如下

ILogger logger=新建LoggerFactory().CreateLogger(); var contentManager=新的contentManager(记录器)

但是我不知道如何将Log4Net实例传递给LoggerFactory

private static ILog _log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
   ILogger logger = new LoggerFactory().CreateLogger<ContentManager>();
   var contentManager = new ContentManager(logger);
   contentManager.Run();
}
private static ILog\u log=LogManager.GetLogger(typeof(Program));
静态void Main(字符串[]参数)
{
ILogger logger=新建LoggerFactory().CreateLogger();
var contentManager=新的contentManager(记录器);
contentManager.Run();
}
我没有发现任何日志记录,ContentManager也没有抛出任何异常。我知道ILogger实例从我的控制台应用程序中不知道任何关于Log4Net的信息,我需要如何传递Log4Net实例?需要这方面的帮助。

请尝试帮助委派到Log4Net。我不熟悉Log4Net,但我认为您会理解这个示例:

class MyLoggerAdapter : Library.ILogger 
{
    private readonly ILog _delegation;

    public MyLoggerAdapter(ILog delegation)
    {
        _delegation = delegation;
    }

    public void ILogger.Debug(string msg, params object[] p)
    {
        _delegation.Debug(msg, p); 
    }
}
然后在代码中可以执行以下操作:

private static ILog _log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
   MyLoggerAdapter logAdapter = new MyLoggerAdapter(_log);
   var contentManager = new ContentManager(logAdapter);
   contentManager.Run();
}

库是否也使用Log4Net?是的。库使用Log4Net。是否有可以传递给库对象的ILogger的工作实例?否。我没有ILogger的工作实例,因为ILogger附带了Microsoft.Extensions.Logging(.NET Core)。但我认为我需要在我的.NETFramework控制台应用程序上实现ILogger。看来没有直截了当的办法。