.Net 6 Console application inject ILogger

Andrzej 26 Reputation points
2022-11-09T21:50:48.543+00:00

Hi,
I'm writing simple console application and I have solution and two project in first project I've Program.cs and CallerRestHttp in second project I've few class where I have generic implementation for HttpClientFactory.
What I want to achieve, my goal is inject in class ( second project) ILogger<MyClass> but all the time I have null value and I have no idea what to change.
Can you give me some example, my code looks like:

Program.cs
var builder = new HostBuilder()
.ConfigureServices((host, services) =>
{
services.AddHttpClient();
services.AddTransient<CallerRestHttp>();
services.AddLogging(config =>
{
config.AddDebug();
config.AddConsole();
}).AddTransient<BaseRestClientHttp>();
}).UseConsoleLifetime();

In CallerRestHttp class I've methods to invoke specific method in my 'RestClientService' but my RestClientService class invoke method Get,POST etc. from BaseRestClientHttp class where I have generic implementation and in this class I want to use my ILogger but inject thorugh constructor all the time show me null value.
How can I resolve this problem ?

Thanks for any advice

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful