如何在ASP.NET Core中向控制台写入内容?

13 浏览
0 Comments

如何在ASP.NET Core中向控制台写入内容?

我已经尝试使用System.Diagnostics.Debug.WriteLine,如这里建议的那样,但是当我在Startup.ConfigureServices方法的顶部放置WriteLine调用并使用dotnet run -c debug在本地运行ASP.NET应用程序时,控制台没有输出任何内容。

出于其他原因,我不能简单地使用ILogger接口(尝试调试一些Application Insights初始化器的日志记录,因此无法获取ILogger实例)。

使用.NET Core 3.1.201。复现步骤:

  1. 打开控制台,进入空目录并键入dotnet new web
  2. 打开生成的项目,在Startup.ConfigureServices方法中添加System.Diagnostics.Debug.WriteLine("TEST TEST TEST");
  3. 打开appsettings.development.json文件,并将所有日志级别设置为Debug
  4. 运行dotnet run -c debug并查看控制台输出;没有TEST TEST TEST字符串:

PS C:\Users\ahelwer\source\prototype\test> dotnet run -c debug
dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
      Hosting starting
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[2]
      Failed to locate the development https certificate at '(null)'.
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[0]
      Using development certificate: CN=localhost (Thumbprint: 9C942CB86D28BD34EC038AC614E1D069646F30E7)
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[0]
      Loaded hosting startup assembly test
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\ahelwer\source\prototype\test
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
      Hosting started

appsettings.development.json的内容:

{

"Logging": {

"LogLevel": {

"Default": "Debug",

"Microsoft": "Debug",

"Microsoft.Hosting.Lifetime": "Debug"

}

}

}

0