serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
313 stars 100 forks source link

No log entry when calling logger instance from Task #128

Closed samirbanjanovic closed 5 years ago

samirbanjanovic commented 5 years ago

There could be a simple answer to this, and to be honest it seems to me to be context related. However, I figured I'd post here see if it get's a conversation started. In my scenario I have a class which hosts the LoggerFactory and is used to create a logger

        private readonly ILoggerFactory _loggerFactory;
        private readonly ILogger<MyClass> _logger;

        public MyClass()
        {            
            _loggerFactory = new LoggerFactory().AddSerilog(dispose: true);            
            _logger = _loggerFactory.CreateLogger<MyClass>();
        }

Logging works as expected as long as I am awaiting all my async methods or the method is synchronous. But, when I call _logger from a task I am not seeing those entries come through.

The code below yields now log entry:

        internal void Configure()
        {
            Task.Run(async () =>
            {
               // doing IO stuff

               //this log entry doesn't get committed
                _logger.LogInformation("{Message}", "Configuring services");

                SetComponentSettings();
                SetMessagePubslisherSettings();

                await SomeOtherStuffAsync();
            });
        }

I was able to get log entries to work by creating another logger within the Configure() method scope: var scopedLogger = _loggerFactory.CreateLogger("ConfigurationLogger"); and calling scopedLogger from within the Task.

I am curious to what I am missing or if this is a legit issue

nblumhardt commented 5 years ago

Nothing immediately comes to mind, @SamirBanjanovic - I hope you found some clues somewhere. Closing as this issue is now a bit stale, but if you did end up tracking this down to something Serilog-related please let us know :-)