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

本文关键字:Web API 不起作用 NET ASP Diagnostic Debug WriteLine System | 更新日期: 2023-09-27 18:30:41

环境

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

缺少System.Diagnostics.Debug的输出

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

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

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

Microsoft.Extensions.Logging.Debug.DebugLogger中缺失

我也尝试了Microsoft.Extensions.Logging.Debug包提供的调试记录器,但没有运气。我想这是有道理的,因为它是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");
} 

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

你必须添加一个 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在核心 CLR 5.0 中不可用。

使用Microsoft.Extensions.Logging.Console.ConsoleLogger

最初,我尝试了在 ASP.NET 5 中将消息记录到控制台的推荐方法是新的 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 除了配置特定记录器时提供的日志级别外,还使用ILoggerFactory.MinimumLevel(例如 loggerFactory.AddConsole(LogLevel.Debug) )。如此处所述

最低级别指示低于指定最小值的任何日志 甚至不会被伐木者看到,无论他们多么努力。

此值默认为 1.0.0-rc1-update1 中高于 Debug Verbose 的值。因此,除非您将ILoggerFactory.MinimumLevel设置为 LogLevel.Debug ,否则您将看不到任何调试输出。请参阅此处报告的类似问题。

似乎有点混乱,对吧?好消息是 ASP.NET 5 团队已经修复了这个"问题"并删除了 1.0.0-rc2 中的任何属性ILoggerFactory.MinimumLevel

如果您仍在使用 1.0.0-rc1-update1,则需要按如下方式配置日志记录:

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
}