Skip to content

Instantly share code, notes, and snippets.

@wullemsb
Created April 17, 2023 16:30
internal class UserEnricher : ILogEventEnricher
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string UserPropertyName = "User";
private readonly IUserFactory _userFactory;
public UserEnricher(IUserFactory userFactory)
{
this._userFactory = userFactory;
}
/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
//We don't cache this property as the value can change
var property = propertyFactory.CreateProperty(UserPropertyName, GetCurrentUser());
logEvent.AddPropertyIfAbsent(property);
}
private string GetCurrentUser()
{
var currentUser = _userFactory.GetCurrentUser();
return currentUser.Name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment