2

I'm trying to attach Serilog to Dotnet core logging framework in a console application. Everything works great in terms of configuration. However, I cannot somehow get the Dependency Injection to work.

For this logging approach that I'm following, each class that needs logging will have to work with ILogger field. The problem that I have is how to register ILogger service as an open-generic type(So I can pass any ILogger<>). When I run my current code I get this exception:

Open generic service type 'Microsoft.Extensions.Logging.ILogger`1[TCategoryName]' requires registering an open generic implementation type. (Parameter 'descriptors')

I have looked at the thread over here but it seems like my problem is a bit different.

This is my code:

The "Test" is a class that needs logging functionality:

 public class Test
    {
        private readonly ILogger _logger;

        public Test(ILogger<Test> logger)
        {
            _logger = logger;
        }

        public void DoSomething()
        {
            //code here 

            _logger.LogInformation("This is the logged error");
        }
    }

In the Main I have something like:

var log = serviceProvider.GetService<ILogger<Test>>();

new Test(log).DoSomething();

And this is how I configure my ILogger service:

 public static IServiceCollection ConfigureSerilog(this IServiceCollection services)
        {
            var logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
                .WriteTo.Console(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
                .WriteTo.File(@"Logs\orgManager.log", rollingInterval: RollingInterval.Day)
                .WriteTo.Sink(new TelegramSink(), LogEventLevel.Error)
                .CreateLogger();

            ApplicationLogging.LoggerFactory.AddSerilog(logger);

            services.AddSingleton(typeof(ILogger<>), s => ApplicationLogging.CreateLogger(typeof(ILogger<>)));
           

            return services;

        }

This is the ApplocationLogging class(accessible throughout the application):

public static class ApplicationLogging
    {
        public static ILoggerFactory LoggerFactory { get; } = new LoggerFactory();
     
        public static ILogger CreateLogger(Type t)
        {
            return LoggerFactory.CreateLogger(t);
        }
    }
Moji
  • 361
  • 6
  • 19
  • 3
    If you're using Generic Host or WebHost there is a Serilog extension method `UseSerilog` that sets this all up for you. It's in the Serilog.Extensions.Hosting [package](https://www.nuget.org/packages/Serilog.Extensions.Hosting/) or the Serilog.AspNetCore [package](https://www.nuget.org/packages/Serilog.AspNetCore/) respectively – pinkfloydx33 Aug 31 '20 at 09:08
  • 1
    As suggested above, _Serilog.Extensions.Hosting_ and `UseSerilog()` will cover this; if you need to set up logging without hosting, can you please post some further details of how your app fits together? Cheers! – Nicholas Blumhardt Aug 31 '20 at 20:53
  • @pinkfloydx33 Actually I'm not using any hosting here, but thanks for that, I'll take a look at that approach too. Here I was wondering how I could fix this problem(Just kinda wanted to set DI manually). – Moji Sep 01 '20 at 05:34
  • @NicholasBlumhardt I'm not using hosting in this project. I just came across a post on dotnetcore logging by MSDN and wanted to merge that in my current C# console application. It's a simple app with 3 layers of Bussiness, Core, and Data + Tests. I'm working on a way to enable logging for any class that needs logging along the way. Here is the post on MSDN: https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/april/essential-net-logging-with-net-core – Moji Sep 01 '20 at 05:39

0 Answers0