Các mức độ log trong thiết kế phần mềm

Trong bất kỳ một hệ thống phần mềm nào thì việc xử lý các event log xảy ra trong quá trình xây dựng cũng như triển khai hệ thống là vô cùng quan trọng. Việc ghi log giúp ta có thể sớm nhận ra và giải quyết các vấn đề lỗi phát sinh khi hệ thống chạy.
Với mỗi event log sẽ đi kèm với những mức độ khác nhau tùy thuộc vào mức độ nghiêm trọng của nó xảy ra đối với toàn hệ thống. Bài này sẽ giúp các bạn tìm hiểu về các mức độ của log và làm thế nào để chọn đúng mức độ cho mỗi dòng log mình viết ra.



Thông thường người ta sẽ chia log ra làm 6 mức. Các mức độ này bao gồm


Trace

Only when I would be "tracing" the code and trying to find one part of a function specifically.
For information that's typically valuable only for debugging.
These messages may contain sensitive application data and so shouldn't be enabled in a production environment.
Disabled by default.

Mức này thường để trace các phần trong một hàm lớn. Dùng trong việc debugging để có thêm nhiều thông tin liên quan đến lỗi.

Debug

For information that may be useful in development and debugging.
Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.).
Example: Entering method Configure with flag set to true.
Enable Debug level logs in production only when troubleshooting, due to the high volume of logs.

Mức này thường dùng khi muốn ghi nhận các thông tin đi vào hàm

Information

For tracking the general flow of the app. These logs typically have some long-term value.
Generally useful information to log (service start/stop, configuration assumptions, etc). Info I want to always have available but usually don't care about under normal circumstances. This is my out-of-the-box config level
Example: Request received for path /api/todo

Mức này được dùng khi muốn track flow chung của toàn hệ thống

Warning

For abnormal or unexpected events in the app flow.
These may include errors or other conditions that don't cause the app to stop but might need to be investigated.
Handled exceptions are a common place to use the Warning log level.
Anything that can potentially cause application oddities, but for which I am automatically recovering. (Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.)
Example: FileNotFoundException for file quotes.txt.

Mức này dùng khi muốn cảnh báo các sự kiện không mong muốn chưa đến mức nghiêm trọng để tạo thành lỗi nhưng cần kiểm tra lại ngay khi có ghi nhận log này

Error

For errors and exceptions that cannot be handled.
These messages indicate a failure in the current activity or operation (such as the current HTTP request), not an app-wide failure.
Any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved (in my apps) for incorrect connection strings, missing services, etc.
Example log message: Cannot insert record due to duplicate key violation.

Mức này dùng khi ghi nhận lỗi hoặc exception xảy ra trong hệ thống

Critical

For failures that require immediate attention.
Any error that is forcing a shutdown of the service or application to prevent data loss (or further data loss). I reserve these only for the most heinous errors and situations where there is guaranteed to have been data corruption or loss.
Examples: data loss scenarios, out of disk space.

Mức này dùng khi có lỗi cực kỳ nghiêm trọng xảy ra. Đây là mức cảnh báo cao nhất.

Trong các thư viện C# hỗ trợ việc ghi log hầu như đều cung cấp và tuân theo các mức độ này. Các thư viện có thể kể đến như

Serilog
NLog
Apache log4net
Elmah


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