System.Diagnostic.Debug.WriteLine 在 ASP.NET 5 Web API 中不起作用

System.Diagnostic.Debug.WriteLine doesn't work in ASP.NET 5 Web API

环境

我正在使用 ASP.NET 5 1.0.0-rc1-update1 和 Visual Studio Mac 上的代码编写 Web API。我使用 Yeoman generator-aspnet 构建了默认 Web API 应用程序。我正在通过默认 web 命令使用 DNX Mono 运行 应用程序。

缺少来自 System.Diagnostics.Debug

的输出

我想将调试输出记录到终端或 Visual Studio 代码输出。我尝试使用 System.Diagnostics.Debug class 这样做,但上面的代码产生零输出。

System.Disagnostics.Debug.WriteLine("This is your message");

我错过了什么?我是否需要在某处声明 DEBUG 符号才能查看调试输出?

缺少 Microsoft.Extensions.Logging.Debug.DebugLogger

我也试过 Microsoft.Extensions.Logging.Debug 包提供的 DebugLogger,但没有成功。我想这是有道理的,因为它是 System.Diagnostics.Debug 之上的包装器。我的 Startup.cs 配置如下:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory)
{
    loggerFactory.AddDebug(LogLevel.Debug);

    // Configuring other middleware
}

我在我的控制器中创建ILogger如下:

public ProductsController(ILoggerFactory logFactory)
{
    _logger = logFactory.CreateLogger(nameof(ProductsController));
}

并使用ILogger如下:

public IActionResult Ping()
{
    _logger.LogDebug("Debug message");
} 

您必须添加一个 TraceListener,

TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);

此外,在 Visual Studio 项目属性中设置 DEBUG 标志

虽然@Martin 的回答完全解决了这个问题,但我想详细说明 ASP.NET 5 中的日志记录 Debug 输出。

使用System.Diagnostics.Debug

System.Diagnostics.Debug.Listeners 默认情况下只有 System.Diagnostics.DefaultTraceListener 似乎不会写入控制台。如果您像@Martin 在他的回答中建议的那样添加一个控制台侦听器,System.Diagnostics.DebugMicrosoft.Extensions.Logging.Debug.DebugLogger 消息将开始显示在控制台中。为了使后者起作用,您还需要为构建指定 DEBUG 符号。

值得注意的是 System.Diagnostics.Debug.Listeners 属性 和 System.Diagnostics.TextWriterTraceListener 在 Core CLR 5.0 中不可用。

使用Microsoft.Extensions.Logging.Console.ConsoleLogger

最初,我尝试了 the recommended way to log messages to console in ASP.NET 5 is the new Microsoft.Extensions.Logging API。我的 loggerFactory 配置如下:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(LogLevel.Debug);

    // Configuring other middleware
}

并且我尝试使用 _logger.LogDebug("Debug message"); 记录调试消息。它没有用,导致我改为尝试 System.Diagnostics.Debug

问题是 ASP.NET 5 v1.0.0-rc1-update1 除了在配置特定记录器时提供的日志级别(例如 loggerFactory.AddConsole(LogLevel.Debug))之外还使用 ILoggerFactory.MinimumLevel。如上所述 here

The minimum level indicates that any logs below the specified minimum will not even be seen by loggers, no matter how hard they try.

这个值defaults to Verbose which in 1.0.0-rc1-update1 is higher that Debug. Thus, unless you set ILoggerFactory.MinimumLevel to LogLevel.Debug, you won't see any debug output. See a similar issues reported here

似乎有点混乱,对吧?好消息是 ASP.NET 5 团队已经在 1.0.0-rc2 中修复了这个 "issue" 和 removed ILoggerFactory.MinimumLevel 属性

如果你还在使用1.0.0-rc1-update1,那么你需要配置logging如下:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory)
{
    // This line can be removed if you are using 1.0.0-rc2 or higher
    loggerFactory.MinimumLevel = LogLevel.Debug;
    loggerFactory.AddConsole(LogLevel.Debug);

    // Configuring other middleware
}