0

Is it possible to use ILog below with Azure Function logging or Serilog etc?

I cannot find code example on how to use it.

Rebus.Logging.ILog

  .Options(o =>
    {
    o.Decorate<IErrorHandler>(c => 
       new ErrorMessageHandler(c.Get<IErrorHandler>(), c.Get<ILog>()));
Pingpong
  • 7,681
  • 21
  • 83
  • 209

1 Answers1

0

It's certainly possibly – but since Rebus' loggers are created with a type (the type works as a context of sorts – I think Serilog calls it "source context"), you do not inject the logger, you inject a logger factory:

.Options(o =>
{
    o.Decorate<IErrorHandler>(c => {
        var errorHandler = c.Get<IErrorHandler>();
        var loggerFactory = c.Get<IRebusLoggerFactory>();
        return new ErrorMessageHandler(errorHandler, loggerFactory));
    });
}

and then, in the constructor of ErrorMessageHandler, you can get the logger:

public class ErrorMessageHandler : IErrorHandler
{
    readonly IErrorHandler errorHandler;
    readonly ILog log;

    public ErrorMessageHandler(IErrorHandler errorHandler, IRebusLoggerFactory loggerFactory)
    {
        this.errorHandler = errorHandler;
        log = loggerFactory.GetLogger<ErrorMessageHandler>();
    }

    public async Task HandlePoisonMessage(TransportMessage transportMessage, ITransactionContext transactionContext, Exception exception)
    {
        // do stuff in here
    }
}
mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • How is the code provided related to Serilog? That ism how to tie Serilog to it? – Pingpong May 31 '19 at 12:05
  • For directions on how to configure Rebus to use Serilog, you might want to check out [the Logging sample](https://github.com/rebus-org/RebusSamples/tree/master/Logging) in the [RebusSamples repository](https://github.com/rebus-org/RebusSamples) – mookid8000 May 31 '19 at 13:14