netcore没有记录到控制台 [英] netcore not logging to console

查看:526
本文介绍了netcore没有记录到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试项目,可以使用Microsoft Logging Extensions软件包尝试用NetCore 2.0进行日志记录.

I have a simple test project to experiment with logging with NetCore 2.0 using the Microsoft Logging Extensions package.

我遇到的问题是,当我第一次运行我的应用程序时,它会按预期记录信息性消息.我的怪异行为是随后的运行根本不会产生任何消息.

The problem I'm having is that when I run my app the very first time, it logs informational messages as expected. The weird behavior I'm having though is that subsequent runs won't produce any messages at all.

我的项目针对Net Core 2.0框架,并安装了以下NuGet软件包:

My project targets the Net Core 2.0 framework, and has the following NuGet packages installed:

  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Abstractions
  • Microsoft.Extensions.Logging.Console
  • Microsoft.Extensions.Logging.Debug

以下是我的示例代码,我正在尝试使用以下代码进行记录:

Below is my sample code I'm trying to get logging working with:

using System;

namespace LoggingNetCore2
{
    using Microsoft.Extensions.Logging;

    class Program
    {
        static void Main(string[] args)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddConsole();
            loggerFactory.AddDebug(); // <-- why is this needed for console logging?

            var logger = loggerFactory.CreateLogger(typeof(Program));
            logger.LogInformation("Hello, World!");
            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogWarning("warning");
            logger.LogCritical("critical");
            logger.LogError("errrrr");

            //using (logger.BeginScope("MyMessages"))
            //{
            //    logger.LogInformation("Beginning Operation...");
            //    logger.LogInformation("Doing something cool... please wait.");
            //    logger.LogInformation("Completed successfully.");
            //}
        }
    }
}

运行上述命令时,控制台窗口上没有任何输出.有什么想法想到会发生什么事吗?

I get no output on the console window when I run the above. Any ideas that spring to mind on what could be going on?

我尝试过的事情:

  • 添加了 Microsoft.Extensions.Logging.Abstractions 程序包,因为我隐约记得过去需要在NetCore 1.0应用程序中执行此操作.
  • 卸载/重新安装了上述软件包.
  • 我什至尝试通过dotnet.exe命令从命令行运行应用程序,但无济于事.
  • 如果我降级到NetCore框架1.1(以及将NuGet程序包降级到1.1版本),一切将按预期进行.
  • Added the Microsoft.Extensions.Logging.Abstractions package, as I vaguely remember needing to do that in NetCore 1.0 apps in the past.
  • Uninstalled / reinstalled the above packages.
  • I've even tried running the app from the command line via the dotnet.exe command, but to no avail.
  • If I downgrade to NetCore framework 1.1 (and the NuGet packages to the 1.1 versions), things work as expected.

编辑:如果将调试日志记录提供程序添加到组合中,则现在会在控制台窗口中显示日志.

I got logs showing now in the console window if I add the debug logging provider into the mix.

在NetCore 1.x应用程序中,只需添加控制台提供程序就足够了.

In a NetCore 1.x app, simply adding the console provider was enough.

编辑#2:结果表明,控制台日志记录提供程序不会像net-core-1.x版本那样立即将消息刷新到控制台.它似乎在其他线程上运行.有关信息,请参见此网页: https://github.com/aspnet/Logging/issues/631

Edit #2: 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

推荐答案

@ ajawad987,您是对的. Dispose()有效.

@ajawad987, you're right. The Dispose() works.

public class Program
{
    public static void Main(string[] args)
    {
        var services = new ServiceCollection()
            .AddLogging(config => config.AddConsole())
            .BuildServiceProvider();

        services.GetRequiredService<ILogger<Program>>()
            .LogCritical("Hello");

        ((IDisposable) services)?.Dispose();
    }
}

这篇关于netcore没有记录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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