【发布时间】: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.WriteLine。DebugLogger本质上是一个包装器,对吧?
标签: c# debugging logging asp.net-core dnx