Aller au contenu

Asp.net core 9

De Banane Atomic

Links

Changes

Log

By default, an app is logging into Console, Debug, EventSource, EventLog (only when running on Windows).
This include the linux journal for production and the VS code DEBUG CONSOLE for development.
public class MyClass(ILogger<MyClass> logger)
{
    public void MyMethod()
    {
        // to optimize the performances, test if the log level is enabled
        if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Debug");
        logger.LogTrace("Trace");
        logger.LogInformation("Info");
        logger.LogWarning("Warn");
        logger.LogError("Error");
        logger.LogCritical("Fatal");
    }
}
appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",       // log level par défaut
      "Microsoft": "Warning",         // pour les loggers commençant par Microsoft
      "MyNamespace.MyClass": "Debug"  // pour les loggers de MyClass
    }
  }
Program.cs
builder.Logging.ClearProviders();  // remove all the providers: no more logs
The Log methods are synchronous.
There is no file log provider.

Built-in logging providers

Console
  • VS code "DEBUG CONSOLE" tab
  • In the console window when the app is run with dotnet run
Debug Debug window when debugging in Visual Studio
On Linux journal or /var/log/message or /var/log/syslog
EventSource Event Tracing for Windows (ETW)
EventLog Windows Event Log
TraceSource System.Diagnostics.TraceSource libraries and providers ()
Azure App Service text files in an Azure App Service app's file system and to blob storage in an Azure Storage account

Console

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Error"  // default log level
    }
  },
  "Console": {
    "LogLevel": {
      "Default": "Information"  // overwrite log level
    },
    "FormatterName": "json"  // log in JSON format
  }
}

journal

mar 19 17:53:10 hostname logtest[29321]: fail: LogTest.Controllers.ItemController[0]
mar 19 17:53:10 hostname logtest[29321]:       Log message

mar 19 17:58:27 hostname logtest[29321]: fail: Microsoft.AspNetCore.Server.Kestrel[13]
mar 19 17:58:27 hostname logtest[29321]:       Connection id "...", Request id "...": An unhandled exception was thrown by the application.
mar 19 17:58:27 hostname logtest[29321]: System.Exception: exception message
mar 19 17:58:27 hostname logtest[29321]:    at LogTest.Controllers.ItemController.ThrowException() in /dev-path/LogTest/Controllers/ItemController.cs:line 41

apache log

  • access.log is well filled by each query
  • error.log is never filled