← Back to list

Serilog — Structured Logging Framework

Serilog is a Structured logging framework

Vijay Krishna (Aryan) · 2020-06-08 09:58 · 0 claps · 1.8 min read
#net-core-3 #serilog #database-logging #console-logging #file-logging
Open on Medium ↗

Serilog — Structured Logging Framework

Serilog is a Structured logging framework

Serilog Sinks are places to write log entries like Text Files, SQL/ NoSQL database, Splunk/ Seq (log-hosting platforms)

Serilog Log Levels

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.

Note — Default Level: if no MinimumLevel is specified, then Information level events and higher will be processed.

Implementation

Default Serilog logging table is

[embed]Default — SerilogLoggingData

We will customize the above table and rather than using Standard Columns, we will use Custom Columns. Add below table in Database “SerilogDB”

[embed]Custom — SerilogLoggingData

Create a .NET Core Worker Service (name — WorkerServiceWithSerilog, Location — C:\ASPDOTNETCore) using Visual Studio 2019 (.NET Core 3.0+)

Install below Nuget Packages:

1. Serilog.AspNetCore
2. Serilog.Sinks.Console
3. Serilog.Sinks.File
4. Serilog.Sinks.Email
5. Serilog.Sinks.MSSqlServer
6. Microsoft.Extensions.Hosting.WindowsServices

WorkerServiceWithSerilog\WorkerServiceWithSerilog\appsettings.json

[embed]appsettings.json

WorkerServiceWithSerilog\WorkerServiceWithSerilog\Program.cs

[embed]Program.cs

WorkerServiceWithSerilog\WorkerServiceWithSerilog\Worker.cs

[embed]Worker.cs

WorkerServiceWithSerilog\WorkerServiceWithSerilog\Logging\CustomLogger.cs

[embed]CustomLogger.cs

WorkerServiceWithSerilog\WorkerServiceWithSerilog\Installers\LoggerServiceInstaller.cs

[embed]LoggerServiceInstaller.cs

WorkerServiceWithSerilog\WorkerServiceWithSerilog\Installers\WorkerServiceWithSerilogInstaller.cs

[embed]WorkerServiceWithSerilogInstaller.cs

WorkerServiceWithSerilog\WorkerServiceWithSerilog\WorkerServiceWithSerilogProcess.cs

[embed]WorkerServiceWithSerilogProcess.cs

Run the application and check for log in DB by executing below query

Console Logs

Console Logs

C:\Logs\MyWorkerService20200424.log

2020-04-30T15:04:27.761Z [INF] Custom Logger - LogInformation
Custom Logger - LogInformation
2020-04-30T15:04:27.858Z [WRN] Custom Logger - LogWarning
Custom Logger- LogWarning
2020-04-30T15:04:27.861Z [ERR] Custom Logger - LogError
Custom Logger- LogError
2020-04-30T15:04:27.872Z [FTL] Custom Logger - LogError
Custom Logger - LogFatal
2020-04-30T15:04:28.037Z [ERR] Attempted to divide by zero.
at WorkerServiceWithSerilog.WorkerServiceWithSerilogProcess.ExecuteMyWorkerServiceProcess() in C:\RnD\ASPDOTNETCoreTraining\WorkerServiceWithSerilog\WorkerServiceWithSerilog\WorkerServiceWithSerilogProcess.cs:line 62
Some string

SELECT * FROM [SerilogDB].[dbo].[SerilogLoggingDataTable] ORDER BY 5 DESC

Database Log

Database Log

Let me know with a comment if you tried it or anything, really. Bye!


메타데이터
post_id
14505051a1c1
slug
serilog-structured-logging-framework-14505051a1c1
url
https://medium.com/@vkg.arya/serilog-structured-logging-framework-14505051a1c1
canonical_url
https://medium.com/@vkg.arya/serilog-structured-logging-framework-14505051a1c1
author_url
https://medium.com/@vkg.arya
status
ok
fetched_at
2026-08-21 09:51:39