❔ 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
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
Hi Thanks for responding, yeah so turns out I was not realising i was using two different builders.
This works
Some of this code is just junk, I'm just testing stuff out
@velreine are you doing this so you can add HTTP context stuff to your logs?
that's my question as well
b/c the proper solution there is middleware
cause that's the exact opposite of what an ILoggerProvider does
also, what's WebLogger?
presumably an ILogger
well yes
but I'm curious what it does
like what does it log, and where does it log to?
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
the ILogger impmementation that goes together with WebLoggerProvider
yeah, you want to GENERATE log events
ILoggers and ILoggerProviders RECEIVE log events
I've just landed on this because the Npgsql package seems to hook into this log
EF has its own mechanisms for generating appropriate logs
controlled via configuration
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
I am using EF
I'm trying to combine EF and HotChocolate GraphQL in a dynamic way
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
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
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
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.