Làm được gì với Serilog?

Serilog là một thư viện ghi log cực kỳ mạnh mẽ, linh hoạt, dễ cài đặt và tương thích với nhiều nền tảng khác nhau.


Nuget
https://www.nuget.org/profiles/serilog

Serilog core package (Chứa các class của thư viện)
https://www.nuget.org/packages/Serilog

Github
https://github.com/serilog

Chúng ta có thể cấu hình Serilog ghi ra file, hiển thị ra console, debug, lưu vào database, ...

Khi làm việc thì ta cần cấu hình Sinks để Serilog biết ghi log xuống đâu. Tùy vào mục đích của ta mà có thể kết hợp các Sinks khác nhau. Chẳng hạn

Ghi log ra màn hình Debug trong Visual Studio
https://www.nuget.org/packages/Serilog.Sinks.Debug

Ghi log ra màn hình CMD Terminal khi chạy ứng dụng bằng dòng lệnh
https://www.nuget.org/packages/Serilog.Sinks.Console

Ghi log ra và lưu xuống file
https://www.nuget.org/packages/Serilog.Sinks.File

Cấu hình Serliog kết hợp với ASP .NET Core project
https://www.nuget.org/packages/Serilog.AspNetCore

Ghi log lên server Seq
https://www.nuget.org/packages/Serilog.Sinks.Seq

Ghi log vào Fluentd
https://www.nuget.org/packages/Serilog.Sinks.Http

Ghi log lên server HTTP (Logstash, ...)
https://www.nuget.org/packages/Serilog.Sinks.Fluentd

Ghi log lên server GrayLog
https://www.nuget.org/packages/Serilog.Sinks.Graylog

Ghi log lên server Splunk
https://www.nuget.org/packages/Serilog.Sinks.Splunk

Ghi và lưu log xuống SQL Server
https://www.nuget.org/packages/Serilog.Sinks.MSSqlServer

Ghi và lưu log xuống MongoDB
https://www.nuget.org/packages/Serilog.Sinks.MongoDB

Ghi và lưu log xuống PostgreSQL
https://www.nuget.org/packages/Serilog.Sinks.PostgreSQL

Ghi log và lưu vào Ravendb
https://www.nuget.org/packages/Serilog.Sinks.RavenDB

Ghi log và lưu vào CouchDB
https://www.nuget.org/packages/Serilog.Sinks.CouchDB

Ghi và lưu log xuống Azure Analytics
https://www.nuget.org/packages/Serilog.Sinks.AzureAnalytics

Ghi và lưu log xuống Azure Event Hub
https://www.nuget.org/packages/Serilog.Sinks.AzureEventHub

Ghi và lưu log xuống Azure Table Storage
https://www.nuget.org/packages/Serilog.Sinks.AzureTableStorage

Ghi và lưu log xuống Azure DocumentDB
https://www.nuget.org/packages/Serilog.Sinks.AzureDocumentDb

Ghi log vào Server https://elmah.io
https://www.nuget.org/packages/Serilog.Sinks.ElmahIO

Ghi log vào Server https://www.datadoghq.com
https://www.nuget.org/packages/Serilog.Sinks.Datadog.Logs

Ghi log vào Azure ApplicationInsights
https://www.nuget.org/packages/Serilog.Sinks.ApplicationInsights

Ghi log vào ElasticSearch
https://www.nuget.org/packages/Serilog.Sinks.ElasticSearch

Ghi log vào SignalR connection
https://www.nuget.org/packages/Serilog.Sinks.SignalR/

Ghi log và gửi thông qua email
https://www.nuget.org/packages/Serilog.Sinks.Email

Ghi log vào Windows Event log
https://www.nuget.org/packages/Serilog.Sinks.EventLog

Ghi log và lưu vào AWS CloudWatch
https://www.nuget.org/packages/Serilog.Sinks.AwsCloudWatch

Ghi log và lưu vào Loggly
https://www.nuget.org/packages/Serilog.Sinks.Loggly

Ghi log và lưu vào logentries.com
https://www.nuget.org/packages/Serilog.Sinks.Logentries

Ghi log và lưu vào Raygun.io
https://www.nuget.org/packages/Serilog.Sinks.Raygun

Gửi log tới Slack
https://www.nuget.org/packages/Serilog.Sinks.Slack


Gửi log tới Discord
https://www.nuget.org/packages/CXuesong.Uel.Serilog.Sinks.Discord

Ngoài ra còn rất nhiều Sinks khác hỗ trợ Serilog

Tham khảo thêm
https://github.com/serilog/serilog/wiki/Provided-Sinks

Serilog cũng hỗ trợ việc thêm các thuộc tính vào log thông qua các Enrichers

https://github.com/serilog/serilog-enrichers-environment
WithMachineName()

https://github.com/serilog/serilog-enrichers-process
WithProcessId()
WithProcessName()

https://github.com/serilog/serilog-enrichers-thread
WithThreadId()
WithThreadName()

https://github.com/IharYakimush/serilog-enrichers-asp-net-core-http-context
FromLogContext()

https://github.com/TinyBlueRobots/Serilog.Enrichers.AssemblyName
WithAssemblyName()
WithAssemblyVersion()

https://github.com/JoshSchreuder/serilog-enrichers-memory
WithMemoryUsage()

https://github.com/ekmsystems/serilog-enrichers-correlation-id
WithCorrelationId()




Dưới đây là ví dụ đơn giản sử dụng Serilog

1. Tạo mới một ứng dụng Console Application project
dotnet new console -o SerilogTest
cd SerilogTest

2. Cài đặt core Serilog package và các Sinks package (Ở đây ghi log ra màn hình console và file)
dotnet add package Serilog
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.File

3. Chỉnh sửa Program.cs dùng các hàm của Serilog. Tham khảo file dưới đây


4. Chạy chương trình
dotnet run


Các bạn tham khảo code mẫu ở đây.


Tiếp tục cập nhật ...