EmfSeriLogLogging 1.0.9
dotnet add package EmfSeriLogLogging --version 1.0.9
NuGet\Install-Package EmfSeriLogLogging -Version 1.0.9
<PackageReference Include="EmfSeriLogLogging" Version="1.0.9" />
<PackageVersion Include="EmfSeriLogLogging" Version="1.0.9" />
<PackageReference Include="EmfSeriLogLogging" />
paket add EmfSeriLogLogging --version 1.0.9
#r "nuget: EmfSeriLogLogging, 1.0.9"
#:package EmfSeriLogLogging@1.0.9
#addin nuget:?package=EmfSeriLogLogging&version=1.0.9
#tool nuget:?package=EmfSeriLogLogging&version=1.0.9
EmfSeriLogLogging
Author / Yazar: Mehmet SEYİTOĞLU
Company / Şirket: EMF BİLGİSAYAR YAZILIM YÖNETİM VE DANIŞMANLIK HİZMETLERİ
Target Framework: .NET 10
License / Lisans: MIT
🇹🇷 Türkçe Dokümantasyon
Genel Bakış
EmfSeriLogLogging, .NET Core uygulamaları için Serilog kütüphanesi ile çoklu log hedefine yapılandırılmış loglama desteği sunan bir kütüphanedir. Console, Dosya (JSON) ve MSSQL Server olmak üzere üç farklı hedefe aynı anda log yazmayı sağlar.
Ne İşe Yarar?
- ✅ Console Log — Uygulama konsoluna renkli log çıktısı
- ✅ File Log — Günlük döndürülen JSON formatında dosya logu
- ✅ MSSQL Log — SQL Server veritabanına yapılandırılmış log kaydı
- ✅ Otomatik
Logstablosu oluşturma (autoCreateSqlTable) - ✅ Kullanıcı adı/e-postası ile log zenginleştirme (
UserNameEnricher) - ✅ Makine adı ile log zenginleştirme (
MachineName) - ✅ Yapılandırılabilir log seviyesi
- ✅ Yapılandırılabilir log dosya yolu
- ✅ Tek satır kod ile tüm loglama altyapısını kurma
Kurulum
dotnet add package EmfSeriLogLogging
appsettings.json Konfigürasyonu
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=MyApp;Trusted_Connection=True;TrustServerCertificate=True"
},
"Logging": {
"LogFilePath": "logs/myapp.json",
"LogLevel": {
"Default": "Information"
}
}
}
DI Kaydı ve Yapılandırma
// Program.cs
// Tek satırda tüm loglama altyapısını kur
Logger.ConfigureLogging(builder.Services, builder.Configuration);
Özelleştirilmiş bölüm adları ile:
Logger.ConfigureLogging(
builder.Services,
builder.Configuration,
connectionStringSection: "ConnectionStrings:LogDatabase",
logFilePathSection: "Logging:CustomLogPath",
logLevelSection: "Logging:LogLevel:Default"
);
Detaylı Kullanım
Otomatik SQL Tablosu
Kütüphane, MSSQL Server'da Logs tablosunu otomatik olarak oluşturur. Tablo şu sütunları içerir:
| Sütun | Tür | Açıklama |
|---|---|---|
Id |
int | Otomatik artan birincil anahtar |
Message |
nvarchar(max) | Log mesajı |
MessageTemplate |
nvarchar(max) | Log mesaj şablonu |
Level |
nvarchar(128) | Log seviyesi (Information, Warning, Error...) |
TimeStamp |
datetime | Log zaman damgası |
Exception |
nvarchar(max) | Hata detayları |
Properties |
nvarchar(max) | Ek özellikler (XML) |
UserName |
nvarchar(128) | Giriş yapan kullanıcının adı/e-postası |
MachineName |
nvarchar(128) | Uygulama çalıştığı makine adı |
Kullanıcı Bilgisi Zenginleştirme
UserNameEnricher, HttpContext üzerinden oturum açmış kullanıcının bilgisini otomatik olarak her log kaydına ekler:
- Önce
ClaimTypes.Emaildeğerini arar - Bulunamazsa
ClaimTypes.Namedeğerini arar - Hiçbiri bulunamazsa
"Anonymous"olarak kaydeder
Controller/Servis İçinde Loglama
public class ProductController : ControllerBase
{
private readonly ILogger<ProductController> _logger;
public ProductController(ILogger<ProductController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult GetProducts()
{
_logger.LogInformation("Ürünler listeleniyor...");
try
{
// iş mantığı...
_logger.LogInformation("Ürünler başarıyla listelendi. Toplam: {Count}", products.Count);
return Ok(products);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ürünler listelenirken hata oluştu");
return StatusCode(500);
}
}
[HttpPost]
public IActionResult CreateProduct(ProductDto dto)
{
_logger.LogInformation("Yeni ürün oluşturuluyor: {ProductName}", dto.Name);
// ...
_logger.LogWarning("Ürün fiyatı çok düşük: {Price}", dto.Price);
}
}
Log Seviyeleri
| Seviye | Kullanım Alanı |
|---|---|
Verbose |
(Trace) En detaylı log — genelde geliştirme ortamında |
Debug |
Hata ayıklama bilgileri |
Information |
Genel uygulama bilgileri |
Warning |
Dikkat edilmesi gereken durumlar |
Error |
Hata durumları |
Fatal |
Kritik hatalar — uygulama çökmesi |
Log Çıktı Hedefleri
| Hedef | Format | Döndürme Politikası |
|---|---|---|
| Console | Renkli metin | - |
| File | JSON | Günlük (Daily rolling) |
| MSSQL | Yapılandırılmış tablo | Süresiz (veritabanında kalıcı) |
🇬🇧 English Documentation
Overview
EmfSeriLogLogging is a logging library for .NET Core applications that provides structured logging support via the Serilog library with multiple log sinks. It simultaneously writes logs to Console, File (JSON), and MSSQL Server.
What Does It Do?
- ✅ Console Log — Colorful output to the application console
- ✅ File Log — Daily rolling JSON formatted file log
- ✅ MSSQL Log — Structured log records to SQL Server database
- ✅ Automatic
Logstable creation (autoCreateSqlTable) - ✅ Log enrichment with user name/email (
UserNameEnricher) - ✅ Log enrichment with machine name (
MachineName) - ✅ Configurable log level
- ✅ Configurable log file path
- ✅ Setup entire logging infrastructure with a single line of code
Installation
dotnet add package EmfSeriLogLogging
appsettings.json Configuration
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=MyApp;Trusted_Connection=True;TrustServerCertificate=True"
},
"Logging": {
"LogFilePath": "logs/myapp.json",
"LogLevel": {
"Default": "Information"
}
}
}
DI Registration and Configuration
// Program.cs
// Setup entire logging infrastructure in a single line
Logger.ConfigureLogging(builder.Services, builder.Configuration);
With custom section names:
Logger.ConfigureLogging(
builder.Services,
builder.Configuration,
connectionStringSection: "ConnectionStrings:LogDatabase",
logFilePathSection: "Logging:CustomLogPath",
logLevelSection: "Logging:LogLevel:Default"
);
Detailed Usage
Automatic SQL Table
The library automatically creates the Logs table in MSSQL Server. The table includes the following columns:
| Column | Type | Description |
|---|---|---|
Id |
int | Auto-incrementing primary key |
Message |
nvarchar(max) | Log message |
MessageTemplate |
nvarchar(max) | Log message template |
Level |
nvarchar(128) | Log level (Information, Warning, Error...) |
TimeStamp |
datetime | Log timestamp |
Exception |
nvarchar(max) | Exception details |
Properties |
nvarchar(max) | Additional properties (XML) |
UserName |
nvarchar(128) | Authenticated user's name/email |
MachineName |
nvarchar(128) | Machine name where the app runs |
User Information Enrichment
UserNameEnricher automatically adds the authenticated user's information from HttpContext to every log entry:
- First looks for
ClaimTypes.Email - If not found, looks for
ClaimTypes.Name - If neither is found, records as
"Anonymous"
Logging in Controllers/Services
public class ProductController : ControllerBase
{
private readonly ILogger<ProductController> _logger;
public ProductController(ILogger<ProductController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult GetProducts()
{
_logger.LogInformation("Listing products...");
try
{
// business logic...
_logger.LogInformation("Products listed successfully. Total: {Count}", products.Count);
return Ok(products);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while listing products");
return StatusCode(500);
}
}
[HttpPost]
public IActionResult CreateProduct(ProductDto dto)
{
_logger.LogInformation("Creating new product: {ProductName}", dto.Name);
// ...
_logger.LogWarning("Product price is too low: {Price}", dto.Price);
}
}
Log Levels
| Level | Usage |
|---|---|
Verbose |
(Trace) Most detailed log — typically in development |
Debug |
Debugging information |
Information |
General application information |
Warning |
Situations requiring attention |
Error |
Error conditions |
Fatal |
Critical errors — application crashes |
Log Output Sinks
| Sink | Format | Rolling Policy |
|---|---|---|
| Console | Colored text | - |
| File | JSON | Daily rolling |
| MSSQL | Structured table | Permanent (persisted in database) |
API Reference / API Referansı
| Class | Description (EN) | Açıklama (TR) |
|---|---|---|
Logger (static) |
Static class to configure Serilog logging | Serilog loglamayı yapılandırmak için statik sınıf |
Logger.ConfigureLogging() |
Sets up Console, File and MSSQL sinks | Console, File ve MSSQL hedeflerini yapılandırır |
UserNameEnricher |
Log enricher for user identity | Kullanıcı kimliği için log zenginleştirici |
Method Signature / Metot İmzası
public static void ConfigureLogging(
IServiceCollection services,
IConfiguration configuration,
string connectionStringSection = "ConnectionStrings:DefaultConnection",
string logFilePathSection = "Logging:LogFilePath",
string logLevelSection = "Logging:LogLevel:Default"
)
| Parameter | Default | Description (EN) | Açıklama (TR) |
|---|---|---|---|
services |
- | DI service collection | DI servis koleksiyonu |
configuration |
- | Application configuration | Uygulama yapılandırması |
connectionStringSection |
"ConnectionStrings:DefaultConnection" |
Config section for connection string | Connection string yapılandırma bölümü |
logFilePathSection |
"Logging:LogFilePath" |
Config section for log file path | Log dosya yolu yapılandırma bölümü |
logLevelSection |
"Logging:LogLevel:Default" |
Config section for minimum log level | Minimum log seviyesi yapılandırma bölümü |
Dependencies / Bağımlılıklar
Serilog(4.3.1)Serilog.AspNetCore(10.0.0)Serilog.Sinks.Console(6.1.1)Serilog.Sinks.File(7.0.0)Serilog.Sinks.MSSqlServer(9.0.3)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Serilog (>= 4.3.1)
- Serilog.AspNetCore (>= 10.0.0)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
- Serilog.Sinks.MSSqlServer (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
SeriLog Kütüphanesi Entegrasyonu ile FileLog, ConsoleLog, MsSqlLog.