📒Logging Implementation
Last updated
Last updated
Logging plays a crucial role in monitoring the health and behavior of an application. In FoodServiceAPI, logging has been implemented using the Serilog library, ensuring that important information, warnings, and errors are captured efficiently. Below are the key points regarding the logging implementation:
The Serilog library is utilized for logging, offering robust capabilities and flexibility in log management.
Console: Logs are written to the console, providing real-time visibility during application runtime.
File: Log files are generated in the Logs
folder with a rolling interval of a day, ensuring organized and manageable log storage.
Serilog Seq Server: Logs are ingested into a Serilog Seq server (http://localhost:5341/
), facilitating centralized log management and dashboard support for better visibility and analysis.
Minimum Log Level
The default log level is set to "Information".
Specific log levels for Microsoft-related components are also set to "Information".
Log Enrichment
Enrichment properties include:
FromLogContext
WithMachineName
WithThreadId
These properties provide additional context to logs.
To streamline the setup process, code to start the Docker container for the Serilog Seq server is added in Program.cs
. This code ensures that the Seq server is available for logging, executing only once during the initial deployment if no server is already available.
Running the logging feature with Serilog Seq Server requires Docker to be installed and running on the host machine. Ensure Docker is properly configured and accessible for seamless integration with the logging setup. While Docker is not crucial for running the application, without it, the application will not have all the logging functionalities, such as centralized log management and enhanced dashboard support provided by the Seq server.
Logging request and response data can sometimes expose sensitive information. To mitigate this risk, the following practices are implemented:
Instead of using middlewares for logging request/response, use IAsyncActionFilter
.
Use the NuGet package Destructurama.Attributed to exclude specific properties from being logged.
Attribute Usage: Apply the [NotLogged]
attribute on class properties that should not be logged.
In this example, the Password
property will not be logged, ensuring that sensitive details are not exposed.
Logging serves as a critical component in maintaining the health and performance of FoodServiceAPI, enabling efficient troubleshooting and proactive monitoring of the application's behavior. By integrating Serilog, setting up proper log destinations, configuring log levels, enriching logs with context, and securely handling sensitive data, we ensure a robust and efficient logging system.