I have an enricher that needs to use the protected session storage to get the user info:
public class UserEnricher : ILogEventEnricher
{
private const string UsernamePropertyName = "Username";
private readonly ProtectedSessionStorage sessionStorage;
public UserEnricher (
ProtectedSessionStorage sessionStorage)
{
this.sessionStorage = sessionStorage;
}
public async void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var userName = await sessionStorage.GetItemAsync<string>("UsernameKey");
var userNameProperty = propertyFactory.CreateProperty(UsernamePropertyName, userName);
logEvent.AddPropertyIfAbsent(userNameProperty);
}
}
And the enricher is set up as a transient service and added to the serilog configuration on startup.
However I am getting:
Cannot resolve ‘Serilog.Enrichers.UserEnricher’ from root provider because it requires scoped service ‘Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedSessionStorage’.
Is accessing the session storage even possible in such a case?
2