如何在ASP.NET Core中向控制台写入内容?
如何在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。复现步骤:
- 打开控制台,进入空目录并键入
dotnet new web
- 打开生成的项目,在
Startup.ConfigureServices
方法中添加System.Diagnostics.Debug.WriteLine("TEST TEST TEST");
- 打开
appsettings.development.json
文件,并将所有日志级别设置为Debug
- 运行
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"
}
}
}