DEV Community

Discussion on: .NET 6.0 console app - Configuration, tricks and tips

Collapse
 
rjwerning profile image
Rich

Nice article, thanks. I downloaded the repo & noticed that the _logger.LogInformation of Woker. StartAsync isn't writing to the console. Question for you, how would you access the logger in MyService.PerformLongTaskAsync?

Collapse
 
krusty93 profile image
Andrea Grillo • Edited

Thanks a lot, I'm happy you did find this helpful.

Nice spot, there was a mistake into the csproj and the Serilog config was not loaded in the 'Development' configuration. I've updated the repo with this change.

You can access the logger in MyService class injecting the ILogger class through the constructor:

    public class MyService : IMyService
    {
        private readonly ILogger<MyService> _logger;

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

        public async Task PerformLongTaskAsync()
        {
            _logger?.LogInformation("Whatever you want");

            await Task.Delay(5000);
        }
    }
Enter fullscreen mode Exit fullscreen mode