I have a logging section in json
settings:
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
I need to register it in Startup
so to be able to use in handlers.
So far I have tried various options but none of them work:
Option 1
builder.Services.AddApplicationInsightsTelemetry(); // returns error: IServicesCollection does not contain a definition for AddApplicationInsightsTelemetry
Option 1 Update
I added Microsoft.ApplicationInsights.AspNetCore
to the project so AddApplicationInsightsTelemetry
is now available but still ILogger
gives null
.
Option 2
builder.Services.AddLogging(logging =>
{
logging.AddConfiguration(hostingContext.GetSection("logging"));
}); // ILogger is null in handler
but can't find any information on my particular case
How to register ILogger
in Startup
that will keep using settings from my app json?
This is the handler I am using:
public class ExampleHandler : IRequestHandler<ExampleQuery, bool>
{
private readonly ILogger _log;
public ExampleHandler(ILogger log)
{
_log = log;
}
public async Task<bool> Handle(ExampleQuery request, CancellationToken cancellationToken)
{
// _log is null here;
return true;
}
}
Install Microsoft.ApplicationInsights.AspNetCore
nuget package
Resolve typed logger:
public class ExampleHandler : IRequestHandler<ExampleQuery, bool>
{
private readonly ILogger<ExampleHandler> _log;
public ExampleHandler(ILogger<ExampleHandler> log)
{
_log = log;
}
...
}