How to use ILogger.BeginScope in Blazor Server for the entire activity? #55949
Replies: 2 comments 1 reply
-
Depending on the logger you could access scoped services by the use of async-locals initialized within For example see https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#access-server-side-blazor-services-from-a-different-di-scope Using Serilog for example you then would implement an And you really should use |
Beta Was this translation helpful? Give feedback.
-
Shame this conversation went cold.. I too am trying to enrich my logs. I haven't gotten as fat using dynamic data but 'BeginScope' in CreateInboundActivityHandler(Func<CircuitInboundActivityContext seems to work. I am miffed as to why it doesn't also work if I use connection open, all of my understanding says it should if I scope the returned disposable as a field, but alas it doesn't. If anyone knows why or if CreateInboundActivityHandler(Func<CircuitInboundActivityContext is a good / bad place to do this id appreciate it |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In a standard request/response app with AspNet.Core, it was easy to add informations in the request context so that every single log entry could have access to them automatically. For exemple, if I need that all my log entries have the FormId that he is presently editing, I just had to create a new Middleware that use ILogger.BeginScope to achieve that and register the middleware at the beginning of the pipeline.
How can we achieve the same thing in Blazor? The only way i've found is with NET 8 and the new CreateInboundActivityHandler that you can override in the CircuitHandler. Is it the way to go? Because depending of the object that you inject, the information inside is not always up-to-date. For exemple, NavigationManager.Uri doesn't have the new value yet and it return the uri where you come from and not the current one.
For exemple,
Beta Was this translation helpful? Give feedback.
All reactions