Serilog Enrichers provide a way to add additional context to log events. They allow you to capture and include relevant information that helps in troubleshooting, analysis, and auditing. Enrichers can be used to add static properties or dynamically calculated values to log events.
- Property Enrichers: These enrichers add fixed properties to every log event, such as application version, environment details, or user information.
- Thread Enrichers: They capture thread-specific information, such as thread ID, and include it in the log events.
- Correlation ID Enrichers: Useful for distributed systems, these enrichers associate a unique ID with a series of related log events, aiding in tracing a request across different components.
- Exception Enrichers: Automatically capture exception details and include them in log events for better debugging.
- Log Context Enrichers: These enrichers store contextual data for the duration of a specific scope, such as a specific HTTP request, allowing you to understand the flow of operations.
- Custom Enrichers: You can create your own enrichers tailored to your application’s requirements, adding any information that aids in better log analysis.
By employing these enrichers, you can provide comprehensive context to your log entries, improving the quality of information available for troubleshooting, analysis, and monitoring.”
Property Enrichment
- The
WithProperty
enricher adds static properties to every log event. Examples of static properties could include the application name, environment, version, etc.
We have already covered this example in this article. Where we tried saving the custom properties in the database.
Serilog Sql Sink, Log Custom Properties
Thread Enrichment
- Captures and includes the thread ID in each log event. Useful for multi-threaded applications to track log messages by thread.
- We need to install an additional NuGet package Serilog.Enrichers.Thread to utilize this Enrich
Process Enrichment
- Adds the process ID and process name to log events. Helps identify which process generated a particular log entry.
- We need to install an additional NuGet package Serilog.Enrichers.Process to utilize this Enrich
Once we run the application and check the Seq server logs. We can find the ProcessId and ProcessName properties which are sent from the above enrichers.
Environment Enrichment
- Includes the machine name and user name in log events.
- Useful for distinguishing log entries from different environments or users.
We have already covered this example in this article. Where we tried saving the MachineName in the database.
Serilog Sql Sink, Log Custom Properties
Custom Enrichment
- Serilog also allows you to create custom enrichers to capture and include application-specific information.
- You can implement the
ILogEventEnricher
interface to define your custom enriched.
using Serilog.Core; using Serilog.Events; namespace DK.Serilog.Demo.Infrastructure { public class ApiVersionEnrichment : ILogEventEnricher { public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { var customProperty = new LogEventProperty("API-Version", new ScalarValue("V2")); logEvent.AddPropertyIfAbsent(customProperty); } } }
We have implemented a custom enricher called ApiVersionEnrichment
that adds an “API-Version” property to the log events. It sets the value of the property to “V2”. This custom enricher can be used to enrich log events with the API version information.
To use this ApiVersionEnrichment
enricher in Serilog, you can modify the Serilog configuration as follows:
By adding this custom enricher to the Serilog configuration, the log events will include the “API-Version” property with the value “V2
“.
This can be useful, for example, when you want to differentiate log events based on the API version they originated from or to track the usage of different API versions in your logs.
Once we run the application and check the Seq server logs. We can find the API-Version property which is sent from the custom enricher.
Client Enricher
To log the client IP and UserAgent information, you can use the Serilog.Enrichers.HttpContext package, which provides enrichers specifically designed for web applications.
Install the NuGet package Serilog.Enrichers.HttpContext and then configure the enrichers.
In this example, the Serilog.Enrichers.HttpContext
package is used to enrich logs with client IP and UserAgent. The enrichers WithClientIp()
and WithClientAgent()
extract the client IP address and UserAgent information from the current HTTP context and include them in the log events.
Conclusion
Enrichers in Serilog enhance log events by adding additional contextual information. Whether it’s information about the environment, exception details, or custom properties, enrichers help you include relevant details to improve the understanding and analysis of log events.