Created
April 17, 2023 16:30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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