ilogger
How to configure and use Serilog in ASP.NET Core 6?
You’ll need to make sure you have the following packages installed: Serilog Serilog.Extensions.Hosting (this provides the .UseSerilog extension method. If you have the Serilog.AspNetCore package, you do not need to explicitly include this) Then you’ll need a using: using Serilog; Which should allow you to access .UseSerilog via builder.Host: using Serilog; var builder = WebApplication.CreateBuilder(args); … Read more
Use Serilog with Microsoft.Extensions.Logging.ILogger
In the Serilog.Extensions.Logging assembly there is a extension method on IloggingBuilder called AddSerilog (it’s in the Serilog namespace) that will allow you to use Serilog for logging. For example: .NET Core 2.2 and earlier (using WebHost): WebHost.CreateDefaultBuilder(args) .ConfigureLogging(logging => { logging.ClearProviders(); logging.AddSerilog(); }); .NET Core 3.1 and later (using generic Host for either web or … Read more
Unable to resolve ILogger from Microsoft.Extensions.Logging
ILogger is no longer registered by default but ILogger<T> is. If you still want to use ILogger you can register it manually with the following (in Startup.cs): public void ConfigureServices(IServiceCollection services) { var serviceProvider = services.BuildServiceProvider(); var logger = serviceProvider.GetService<ILogger<AnyClass>>(); services.AddSingleton(typeof(ILogger), logger); … } Where AnyClass can be something generic, such as: public class ApplicationLogs … Read more
How to unit test with ILogger in ASP.NET Core
Just mock it as well as any other dependency: var mock = new Mock<ILogger<BlogController>>(); ILogger<BlogController> logger = mock.Object; //or use this short equivalent logger = Mock.Of<ILogger<BlogController>>() var controller = new BlogController(logger); You probably will need to install Microsoft.Extensions.Logging.Abstractions package to use ILogger<T>. Moreover you can create a real logger: var serviceProvider = new ServiceCollection() .AddLogging() … Read more