Serilog AspNetCore 8
Install Serilog
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.MSSqlServer
Configuration
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer"],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=.;Database=Store;Trusted_Connection=true;TrustServerCertificate=true;",
"tableName": "Log",
"autoCreateSqlTable": true
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "WebApp"
}
}
Configuration in Programin.cs
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration).CreateLogger();
builder.Services.AddSerilog();
Logging
ILogger<NameController& _logger;
public NameController(ILogger<NameController> logger){
_logger = logger;
}
public IActionResult Index(){
_logger.LogInformation("Login info", "log name");
return View(list);
}
All Level Loging
| Level | Usage |
| Verbose | Verbose is the noisiest level, rarely (if ever) enabled for a production app. |
| Debug | Debug is used for internal system events that are not necessarily observable from the outside, but useful when determining how something happened. |
| Information | Information events describe things happening in the system that correspond to its responsibilities and functions. Generally these are the observable actions the system can perform. |
| Warning | When service is degraded, endangered, or may be behaving outside of its expected parameters, Warning level events are used. |
| Error | When functionality is unavailable or expectations broken, an Error event is used. |
| Fatal | The most critical level, Fatal events demand immediate attention. |