Reputation:
After reading this article, it becomes clear that AsyncLocal context at the beginning of an async method will always flow through to its continuations.
Logical call context data flows with ExecutionContext. This means that it’s not affected by ConfigureAwait(continueOnCapturedContext: false); you can’t “opt-out” of the logical call context. So the logical call context at the beginning of an async method will always flow through to its continuations.
I use AsyncLocal with Serilog in My Blazor Server Middliware like this:
using (LogContext.PushProperty("CustomProp", propValue))
{
await _next(context);
}
And I understand, that each HTTP request will have its own AsyncLocal context. But Blazor Server uses SignalR (and in particular WebSocket) to communicate between client and server. Can I be sure that each WebSocket connection will have its own AsyncLocal Context?
Upvotes: 0
Views: 491
Reputation: 1629
Assuming you have configured serilog properly in your startup or program.cs then you can safely assume it will have its own. Serilog is suppose to be injected into whatever razor page you are using and that using a transient or scoped interface (don't remember which), but both are unique to each session that is established.
TL;DR
Transient objects are always different; a new instance is provided to every controller and every service.
Scoped objects are the same within a request, but different across different requests.
Singleton objects are the same for every object and every request.
You should have something to the affect of:
services.AddSingleton(Log.Logger);
If that is the case then yes each one will be unique per signalr websocketed session.
If you could post how you setup your serilog that would be more helpful as the AsyncLocal really doesn't matter as you should be injecting the serilog into the razor page.
Upvotes: 1