Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Server, Nlog ScopeContext - property propagation to all components and services

$
0
0

In my Blazor Server app I have LoggingCircuitHandler:

public sealed class LoggingCircuitHandler: CircuitHandler{    private readonly ILogger<LoggingCircuitHandler> _logger;    private IDisposable? _scope;    public LoggingCircuitHandler(ILogger<LoggingCircuitHandler> logger)    {        _logger = logger;    }    public override async Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken)    {        string login = "Anonymous";        int id = 0;        string guid = Guid.NewGuid();        Dictionary<string, string?> props = new Dictionary<string, string?>()        {            ["RequestUserLogin"] = login,            ["RequestUserId"] = id.ToString(),            ["RequestMethodGuid"] = guid        };        _scope = ScopeContext.PushProperties(props);        _logger.LogTrace(1, "Circuit {CircuitId} started for user {User}, Guid: {Guid}", circuit.Id, login, guid);    }    public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken)    {        _scope?.Dispose();        return Task.CompletedTask;    }}

Nlog Config:

${scopeproperty:item=RequestUserLogin:whenEmpty=user}(${scopeproperty:item=RequestUserId:whenEmpty=id}) | ${scopeproperty:item=RequestMethodGuid:whenEmpty=guid}

But these properties are not propagated correctly to the logger in razor components and services added in DI using e.g. AddScopped. In the logs I see only | user(id) | quid.

I want to set a scope for a given user in one place and add their data to each log during their session, I don't want to support retrieving user data and creating a new scope in each method...


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>