3

I've already tried using System.Diagnostics.Debug.WriteLine as suggested here, but nothing is written to console when I put a WriteLine call at the top of my Startup.ConfigureServices method then run the ASP.NET application locally with dotnet run -c debug.

For other reasons I can't just use the ILogger interface (trying to debug logging some Application Insights initializers so can't get an ILogger instance).

Using .NET Core 3.1.201. Steps to reproduce:

  1. Open console, move to empty directory and type dotnet new web
  2. Open generated project, add System.Diagnostics.Debug.WriteLine("TEST TEST TEST"); to the Startup.ConfigureServices method
  3. Open the appsettings.development.json file and set all log levels to Debug
  4. Run dotnet run -c debug and view console output; there will be no TEST TEST TEST string present:
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

Contents of appsettings.development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Debug",
      "Microsoft.Hosting.Lifetime": "Debug"
    }
  }
}
ahelwer
  • 1,441
  • 13
  • 29
  • Can you show the code? – krlosmederos Apr 14 '20 at 01:41
  • `public void ConfigureServices(IServiceCollection services){System.Diagnostics.Debug.WriteLine("TEST TEST TEST TEST");` etc. – ahelwer Apr 14 '20 at 01:43
  • WriteLine is the way. Your Configureservices method is not being called. – Gerardo Grignoli Apr 14 '20 at 01:48
  • It most certainly is. It contains a number of other authentication & authorization configuration methods which are definitely being added. – ahelwer Apr 14 '20 at 01:49
  • I added steps to reproduce. – ahelwer Apr 14 '20 at 01:57
  • Minimum log level are you using? In case that is information then debug not will be shown – krlosmederos Apr 14 '20 at 02:07
  • Set all log levels to debug (see edited post); same thing. – ahelwer Apr 14 '20 at 02:12
  • Did you try Console.WriteLine? What did you get get? Is switching to a more flexible logging system, such as [Serilog](https://serilog.net/) an option? – mason Apr 14 '20 at 02:28
  • 1
    @lex-li what do you mean by "the new logger"? `ILogger`? – ahelwer Apr 14 '20 at 15:16
  • @LexLi that's completely wrong. Not only does the doc you linked to say you can (optionally) attach a trace listener and not that one is required, but also its for .Net Framework; OP's question is relating to .Net Core. There is no guidance from MS to stop using the Debug class and use the 'new logger'. – Matt G Jun 03 '20 at 04:22

0 Answers0