Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikael Axel Kleinwort 97 posts 384 karma points c-trib
    23 days ago
    Mikael Axel Kleinwort
    0

    How to obtain a Serilog.ILogger instance through Dependency Injection in Umbraco 8?

    In Umbraco 8, I try to obtain a Serilog.ILogger instance through dependency injection, but I get an 'unresolved dependency' error. How can I achieve this?

    Background:

    For some of my .Net Standard libraries, I need to provide a Microsoft.Extensions.Logging.ILogger. There is a Serilog extension for this, see this StackOverflow answer:

    _msLogger = new SerilogLoggerProvider(serilogLogger).CreateLogger("test");
    

    However, I need to get the Serilog logger for this. My goal is that the log messages of the libraries are logged to the regular Umbraco log.

    My service code, where I try to obtain the Serilog instance through DI and create a Microsoft ILogger from it:

    using Microsoft.Extensions.Logging; // from Nuget Microsoft.Extensions.Logging.Abstractions Nuget package
    using Serilog.Extensions.Logging; // from Serilog.Extensions.Logging Nuget package
    
    namespace DhyanMikael.Core
    {
        public class MsLoggingService : IMsLoggingService
        {
            private readonly ILogger _msLogger;
    
            public MsLoggingService(Serilog.ILogger serilogLogger)
            {
                // Obtain a Microsoft.Extensions.Logging.ILogger instance from Serilog
                // This requires Serilog.Extensions.Logging Nuget package.
                _msLogger = new SerilogLoggerProvider(serilogLogger).CreateLogger("test");
            }
    
            public ILogger MsLogger => _msLogger;
        }
    }
    

    The IMsLoggingServiceinterface declaration, just boilerplate:

    namespace DhyanMikael.Core
    {
        public interface IMsLoggingService
        {
            ILogger MsLogger { get; }
        }
    }
    

    The boilerplate composer:

    namespace DhyanMikael.Core.Composers
    {
        class MsLoggerComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<IMsLoggingService, MsLoggingService>(Lifetime.Singleton);
            }
        }
    }
    

    The error I get:

    Unresolved dependency [Target Type: DhyanMikael.Core.MsLoggingService], [Parameter: serilogLogger(Serilog.ILogger)], [Requested dependency: ServiceType:Serilog.ILogger, ServiceName:]
    
  • Mikael Axel Kleinwort 97 posts 384 karma points c-trib
    23 days ago
    Mikael Axel Kleinwort
    100

    Thank you to Warren Buckley for replying on Twitter and to Bjarke Berg for twittering the key hint:

    Serilog has a static instance called Log.Logger, which is populated in Umbraco v9. I guess that also exists in v8.

    This did the trick.

    I can simply do

    using Microsoft.Extensions.Logging; // from Nuget Microsoft.Extensions.Logging.Abstractions Nuget package
    using Serilog.Extensions.Logging; // from Serilog.Extensions.Logging Nuget package
    
    namespace DhyanMikael.Core
    {
        public class MsLoggingService : IMsLoggingService
        {
            private readonly ILogger _msLogger;
    
            public MsLoggingService(Serilog.ILogger serilogLogger)
            {
                // Obtain a Microsoft.Extensions.Logging.ILogger instance from Serilog 
                // This requires Serilog.Extensions.Logging Nuget package.
                __msLogger = new SerilogLoggerProvider(Serilog.Log.Logger).CreateLogger("test");
            }
    
            public ILogger MsLogger => _msLogger;
        }
    }
    

    and the messages logged to _msLogger show up in the Umbraco log.

Please Sign in or register to post replies

Write your reply to:

Draft