« Asp.net core 9 » : différence entre les versions
Apparence
Aucun résumé des modifications |
|||
Ligne 6 : | Ligne 6 : | ||
= Changes = | = Changes = | ||
* [[Swashbuckle]] is no longer actively maintained and has been remove from .NET 9 templates. [[Asp.net_core_9_web_api#OpenAPI_/_Swagger|OpenAPI in ASP.NET Core 9 Web API]] | * [[Swashbuckle]] is no longer actively maintained and has been remove from .NET 9 templates. [[Asp.net_core_9_web_api#OpenAPI_/_Swagger|OpenAPI in ASP.NET Core 9 Web API]] | ||
= [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-8.0 Log] = | |||
{{info | By default, an app is logging into {{boxx|Console}}, {{boxx|Debug}}, {{boxx|EventSource}}, {{boxx|EventLog}} (only when running on Windows).<br> | |||
This include the linux journal for production and the VS code DEBUG CONSOLE for development.}} | |||
<kode lang='cs'> | |||
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"); | |||
} | |||
} | |||
</kode> | |||
<filebox fn='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 | |||
} | |||
} | |||
</filebox> | |||
<filebox fn='Program.cs'> | |||
builder.Logging.ClearProviders(); // remove all the providers: no more logs | |||
</filebox> | |||
{{warn | 1=The Log methods are [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0#no-asynchronous-logger-methods synchronous].<br> | |||
There is no file log provider.}} | |||
== [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging#built-in-logging-providers Built-in logging providers] == | |||
{| class="wikitable wtp wtmono1" | |||
|- | |||
| Console || | |||
* VS code "DEBUG CONSOLE" tab | |||
* In the console window when the app is run with {{boxx|dotnet run}} | |||
|- | |||
| Debug || Debug window when debugging in Visual Studio<br />On Linux {{boxx|journal}} or {{boxx|/var/log/message}} or {{boxx|/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 | |||
|} | |||
=== [https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter Console] === | |||
<filebox fn='appsettings.json'> | |||
{ | |||
"Logging": { | |||
"LogLevel": { | |||
"Default": "Error" // default log level | |||
} | |||
}, | |||
"Console": { | |||
"LogLevel": { | |||
"Default": "Information" // overwrite log level | |||
}, | |||
"FormatterName": "json" // log in JSON format | |||
} | |||
} | |||
</filebox> | |||
=== journal === | |||
<pre> | |||
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 | |||
</pre> | |||
=== apache log === | |||
* {{boxx|access.log}} is well filled by each query | |||
* {{boxx|error.log}} is never filled |
Version du 18 janvier 2025 à 18:07
Links
Changes
- Swashbuckle is no longer actively maintained and has been remove from .NET 9 templates. OpenAPI in ASP.NET Core 9 Web API
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 |
|
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