我正在关注这个教程:https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/
并相应地安装了软件包,但日志没有在任何地方打印。
这是我的代码:
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddTransient<IFoo, Foo>(s =>
{
return new Foo()})
.BuildServiceProvider();
//configure console logging
serviceProvider
.GetService<ILoggerFactory>()
.AddConsole(LogLevel.Debug);
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<Program>();
logger.LogError("Starting application");
结果控制台日志记录提供程序不会像在 net-core-1.x 版本中那样立即将消息刷新到控制台。它似乎在不同的线程上运行。请参阅此网页了解信息:https://github.com/aspnet/Logging/issues/631
您可以在末尾添加Main功能。
serviceProvider.Dispose();
或者你可以添加.AddDebug()
serviceProvider
.GetService<ILoggerFactory>()
.AddConsole(LogLevel.Debug)
.AddDebug();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)