C# net-core2.0中具有NLog的DI

C# net-core2.0中具有NLog的DI,c#,.net-core,asp.net-core-2.0,nlog,C#,.net Core,Asp.net Core 2.0,Nlog,我对NetCore2.0上的NLog有点不了解 我想在我的控制台应用程序中创建一个具有构造函数的类 Bot.cs public class Bot : IBot { static TelegramBotClient _botClient; Microsoft.Extensions.Logging.ILogger<Bot> _logger; public Bot(string botToken, WebProxy httpProxy, ILogger<B

我对NetCore2.0上的NLog有点不了解 我想在我的控制台应用程序中创建一个具有构造函数的类

Bot.cs

public class Bot : IBot
{
    static TelegramBotClient _botClient;
    Microsoft.Extensions.Logging.ILogger<Bot> _logger;

    public Bot(string botToken, WebProxy httpProxy, ILogger<Bot> logger)
    {
        _logger = logger;
        _botClient = new TelegramBotClient(botToken, httpProxy);
        //...
    }
}
公共类Bot:IBot
{
静态电报botClient\u botClient;
Microsoft.Extensions.Logging.ILogger\u记录器;
公共Bot(字符串botToken、WebProxy httpProxy、ILogger记录器)
{
_记录器=记录器;
_botClient=新电报botClient(botToken,httpProxy);
//...
}
}
和Program.cs

public static async Task Main(string[] args)
{
     //...

    appModel = configuration.GetSection("ApplicationModel").Get<ApplicationModel>();
    var userName = appModel.ProxyConfiguration.UserName;
    var password = appModel.ProxyConfiguration.Password;

    var httpProxy = new WebProxy(appModel.ProxyConfiguration.Url, appModel.ProxyConfiguration.Port)
    {
        Credentials = credentials
    };
    NLog.ILogger Logger = LogManager.GetCurrentClassLogger();
    _botClient = new Bot(appModel.BotConfiguration.BotToken, httpProxy, ???);

    //...
}
公共静态异步任务主(字符串[]args)
{
//...
appModel=configuration.GetSection(“ApplicationModel”).Get();
var userName=appModel.ProxyConfiguration.userName;
var password=appModel.ProxyConfiguration.password;
var httpProxy=new WebProxy(appModel.ProxyConfiguration.Url,appModel.ProxyConfiguration.Port)
{
凭证=凭证
};
NLog.ILogger Logger=LogManager.GetCurrentClassLogger();
_botClient=新的Bot(appModel.BotConfiguration.BotToken,httpProxy,?);
//...
}
我应该怎么做才能在我的
Bot
类中获得
Microsoft.Extensions.Logging.ILogger
?在程序类中,我只有
NLog.ILogger
。“部分”DI的最佳实践是什么?我想将字符串botToken、WebProxy httpProxy传递给
Bot
类中的构造函数,但需要
ILogger
记录器自动解析

可能吗

我想把
IOptions
appSettings传递给Bot类,但这将是一个脏代码,不是吗?

NLog.ILogger Logger = LogManager.GetCurrentClassLogger();
返回类型为
NLog.Logger
的对象

另一方面,您的类需要类型为
Microsoft.Extensions.Logging.ILogger
的构造函数参数。因此,虽然这两种类型都被命名为ILogger,但实际上它们是完全不同的类型

一个答案是将类更改为使用
NLog.Logger
而不是
Microsoft.Extensions.Logging.ILogger
。然后你可以这样做:

NLog.ILogger logger = LogManager.GetCurrentClassLogger();
_botClient = new Bot(appModel.BotConfiguration.BotToken, httpProxy, logger);
那可能行得通。我唯一关心的是它使您的
Bot
类依赖于NLog。现在,它依赖于一个框架抽象,这是更好的,因为这意味着您可以使用NLog或替代其他一些记录器

因此,为了保持原样,您需要创建一个类,该类将
NLog.ILogger
改编为
Microsoft.Extensions.Logging.ILogger

(当我达到这一点时,我开始自己编写适配器。这时我意识到它几乎肯定已经存在了。)

幸运的是,NLog已经完成了这项工作,您可以将add作为NuGet包。他们提供这一点非常有意义,因为不是每个想要使用NLog的人都想在每个需要记录器的类中放置NLog特定的类型

他们将讨论如何在控制台应用程序中配置此功能


我从这个开始:

public class NLogger : ILogger
{
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        throw new NotImplementedException();
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        throw new NotImplementedException();
    }

    public IDisposable BeginScope<TState>(TState state)
    {
        throw new NotImplementedException();
    }
}
公共类NLogger:ILogger
{
公共无效日志(日志级别、日志级别、事件ID、事件ID、TState状态、异常、函数格式化程序)
{
抛出新的NotImplementedException();
}
公共布尔值已启用(日志级别日志级别)
{
抛出新的NotImplementedException();
}
公共IDisposable BeginScope(州)
{
抛出新的NotImplementedException();
}
}
…并想知道实施会是什么样子。结果是,它看起来像这样:


如果我认为我会想到这一点,那我就错了。

这一行是不对的:
Microsoft.Extensions.Logging.ILogger\u只需使用类型为
ILogger
@Galina的字段。请参阅此注释,它可能对您有所帮助。