【问题标题】:System.Diagnostic.Debug.WriteLine doesn't work in ASP.NET 5 Web APISystem.Diagnostic.Debug.WriteLine 在 ASP.NET 5 Web API 中不起作用
【发布时间】:2023-03-12 19:04:01
【问题描述】:

环境

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

缺少来自System.Diagnostics.Debug 的输出

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

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");
} 

【问题讨论】:

  • 只要您通过 logging api 编写消息,调试记录器就应该可以工作。这就是你在做的吗?
  • @VictorHurdugaci,不完全是。我尝试直接使用System.Diagnostics.Debug.WriteLineDebugLogger 本质上是一个包装器,对吧?

标签: c# debugging logging asp.net-core dnx


【解决方案1】:

虽然@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 将消息记录到 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))。如上所述here

最低级别表示低于指定最低级别的任何日志 伐木者无论多么努力都不会看到他们。

这个值defaults to Verbose 在 1.0.0-rc1-update1 中高于Debug。因此,除非您将ILoggerFactory.MinimumLevel 设置为LogLevel.Debug,否则您将看不到任何调试输出。查看here 报告的类似问题。

似乎有点混乱,对吧?好消息是 ASP.NET 5 团队已经在 1.0.0-rc2 中修复了这个“问题”和 removed 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
}

【讨论】:

    【解决方案2】:

    你必须添加一个 TraceListener,

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

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

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多