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?
_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:
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.
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: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:
The
IMsLoggingService
interface declaration, just boilerplate:The boilerplate composer:
The error I get:
Thank you to Warren Buckley for replying on Twitter and to Bjarke Berg for twittering the key hint:
This did the trick.
I can simply do
and the messages logged to
_msLogger
show up in the Umbraco log.is working on a reply...