C
C#13mo ago
velreine

❔ Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

I'm having this exception thrown at run-time even though builder.Services.AddHttpContextAccessor() was invoked, anyone who knows what might be at fault here? LMK if more details are needed.
19 Replies
JakenVeina
JakenVeina13mo ago
gonna have to assume you're not, in fact, calling .AddHttpContextAccessor() not where it's needed, anyway on a side note, you're writing your own ILoggerProvider? And, what, it's logging stuff TO the HttpContext? That seems really bizarre. Actually, now that I think about it, this is nonsense regardless, and that could benthe source of the issue your logger provider is going to be a singleton, while the accessor is scoped
velreine
velreine13mo ago
Hi Thanks for responding, yeah so turns out I was not realising i was using two different builders. This works
velreine
velreine13mo ago
Some of this code is just junk, I'm just testing stuff out
jcotton42
jcotton4213mo ago
@velreine are you doing this so you can add HTTP context stuff to your logs?
JakenVeina
JakenVeina13mo ago
that's my question as well
jcotton42
jcotton4213mo ago
b/c the proper solution there is middleware
JakenVeina
JakenVeina13mo ago
cause that's the exact opposite of what an ILoggerProvider does
jcotton42
jcotton4213mo ago
also, what's WebLogger?
JakenVeina
JakenVeina13mo ago
presumably an ILogger
jcotton42
jcotton4213mo ago
well yes but I'm curious what it does like what does it log, and where does it log to?
velreine
velreine13mo ago
I ultimately want to create a Razor page (atleast that's what I've landed on now) that displays different profiling data collected in the request => response flow. For now I wanna "log"/"collect" all database calls
JakenVeina
JakenVeina13mo ago
the ILogger impmementation that goes together with WebLoggerProvider yeah, you want to GENERATE log events ILoggers and ILoggerProviders RECEIVE log events
velreine
velreine13mo ago
I've just landed on this because the Npgsql package seems to hook into this log
JakenVeina
JakenVeina13mo ago
EF has its own mechanisms for generating appropriate logs controlled via configuration
jcotton42
jcotton4213mo ago
and if you're not using EF, and whatever you're using doesn't have native logging, put that logging in your data access stuff
velreine
velreine13mo ago
I am using EF I'm trying to combine EF and HotChocolate GraphQL in a dynamic way
JakenVeina
JakenVeina13mo ago
the way the logging system is setup to work is that code that DOES stuff that should be logged pulls an ILogger and invokes it to generate log events ILoggerProviders represent log SINKS that receive those events and configuration lets you control which sinks receive which logs based on category and severity and whatever else chosing which logs get generated is up to the system that generates them, and EF has its own configuration settings to control what gets logged and when the logging system also defines the concept of "scopes" which lets you add data to logs, even if you're not generating them so, if you want to log a request ID, so you can group up all logs for a request, you can do that easily
using(var requestScope = _logger.BeginScope(...))
{
...
}
using(var requestScope = _logger.BeginScope(...))
{
...
}
whatever data you pass to .BeginScope() will be attached to ALL logs that go through the logging system, on the same logical thread, until the scope is disposed
jcotton42
jcotton4213mo ago
real example of this (it's a discord bot using a MediatR pipeline, but the same idea applies in asp.net apps) https://github.com/jcotton42/woolly/blob/main/src/Woolly/Infrastructure/RequestIdLoggingBehavior.cs
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts