【发布时间】:2019-04-04 06:28:50
【问题描述】:
我对 net core 2.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);
//...
}
}
和 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, ???);
//...
}
我应该怎么做才能在我的Bot 课程中获得Microsoft.Extensions.Logging.ILogger<Bot>?在程序类中,我只有NLog.ILogger。 “部分” DI 的最佳实践是什么?我想将字符串 botToken、WebProxy httpProxy 传递给 Bot 类中的构造函数,但希望 ILogger<Bot> 记录器自动解析。
有可能吗?
我有一个想法将IOptions<ApplicationMode> appSettings 传递给 Bot 类,但这将是一个脏代码,不是吗?
【问题讨论】:
-
这条线不对:
Microsoft.Extensions.Logging.ILogger<Bot> _logger; -
只使用
ILogger类型的字段 -
@Galina 参考此评论可能对您有所帮助。 stackoverflow.com/a/51266541/3595964
标签: c# .net-core asp.net-core-2.0 nlog