« Asp.net core 9 » : différence entre les versions
Apparence
Ligne 44 : | Ligne 44 : | ||
{{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> | {{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.}} | There is no file log provider.}} | ||
== [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-9.0#log-in-programcs Log in Program.cs] == | |||
<filebox fn='Program.cs'> | |||
app.Logger.LogInformation("message"); | |||
</filebox> | |||
== [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging#built-in-logging-providers Built-in logging providers] == | == [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging#built-in-logging-providers Built-in logging providers] == |
Version du 18 janvier 2025 à 18:10
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. |
Log in Program.cs
Program.cs |
app.Logger.LogInformation("message");
|
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