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; } }