Reputation: 3283
I have an issue where some logging calls are not reaching App Insights, but others are
I added a controller to test this
public class LogController : ControllerBase
{
private readonly ILogger<BankingController> _logger;
private readonly IServiceProvider _services;
public LogController(ILogger<LogController> logger,
IServiceProvider services)
{
_logger = logger;
_services = services;
}
[HttpGet("test-logging")]
public async Task TestLogging()
{
_logger.Log(LogLevel.Information, "Test Logging controller - information manual");
_logger.LogTrace("Test Logging controller - trace");
_logger.LogInformation("Test Logging controller - information");
_logger.LogWarning("Test Logging controller - warning");
_logger.LogError("Test Logging controller - error");
_logger.LogCritical("Test Logging controller - critical");
var logger = _services.GetRequiredService<ILogger<LoggingController>>();
logger.Log(LogLevel.Information, "DIRECT Test Logging controller - information manual");
logger.LogTrace("DIRECT Test Logging controller - trace");
logger.LogInformation("DIRECT Test Logging controller - information");
logger.LogWarning("DIRECT Test Logging controller - warning");
logger.LogError("DIRECT Test Logging controller - error");
logger.LogCritical("DIRECT Test Logging controller - critical");
}
}
None of the logging calls here reach App Insights
However, in another part of the API, in program.cs we have a logging call that does reach App Insights
public static class IWebHostExtensions
{
public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext,IServiceProvider> seeder) where TContext : DbContext
{
using (var scope = webHost.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>();
try
{
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(new TimeSpan[]
{
TimeSpan.FromSeconds(3),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(8),
});
retry.Execute(() =>
{
//if the sql server container is not created on run docker compose this
//migration can't fail for network related exception. The retry options for DbContext only
//apply to transient exceptions.
context.Database.Migrate();
});
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
}
}
return webHost;
}
}
Has anyone ever come across this?
I tried a totally fresh App Insights instance
As you can see, the method used for logger uses ServiceProvider like the approach that works for migrations
I really need the normal ILogger convention to work reliably
Im using .NET Core 3.1
In my startup.cs I have
services.AddApplicationInsightsTelemetry();
I have tried changing the logging levels in my appsettings and still my custom messages are not being logged
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Trace",
"System": "Trace"
}
},
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"System": "Warning"
}
},
Paul
Upvotes: 0
Views: 194
Reputation: 18387
Have you tried using the right type for ILogger? In your code I can see you're using BankingController as the Type but on a different class. Try this:
from:
public class LogController : ControllerBase
{
private readonly ILogger<BankingController> _logger;
to:
public class LogController : ControllerBase
{
private readonly ILogger<LogController > _logger;
Upvotes: 1