登录.Net核心控制台应用程序不起作用 [英] Logging in .Net core console application not working

查看:81
本文介绍了登录.Net核心控制台应用程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程: https:/ /andrewlock.net/using-dependency-injection-in-a-net-core-console-application/

并相应地安装了软件包,但日志为

and accordingly installed the packages but log is not getting printed anywhere.

这是我的代码:

  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

Turns out the console logging provider doesn't immediately flush the messages to the console like it did in the net-core-1.x versions. It appears to run on a different thread. See this web page for info: https://github.com/aspnet/Logging/issues/631

您可以在 Main 函数的末尾添加。

You can add at the end of the Main function.

serviceProvider.Dispose();

或者您可以添加 .AddDebug()

            serviceProvider
            .GetService<ILoggerFactory>()
            .AddConsole(LogLevel.Debug)
            .AddDebug();

这篇关于登录.Net核心控制台应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆