Programming C#

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

LevelUsage
VerboseVerbose is the noisiest level, rarely (if ever) enabled for a production app.
DebugDebug is used for internal system events that are not necessarily observable from the outside, but useful when determining how something happened.
InformationInformation events describe things happening in the system that correspond to its responsibilities and functions. Generally these are the observable actions the system can perform.
WarningWhen service is degraded, endangered, or may be behaving outside of its expected parameters, Warning level events are used.
ErrorWhen functionality is unavailable or expectations broken, an Error event is used.
FatalThe most critical level, Fatal events demand immediate attention.