2018-08-30 13:53:16 +00:00
using System ;
2019-08-14 10:20:53 +00:00
using Tapeti.Config ;
using ISerilogLogger = Serilog . ILogger ;
2018-08-30 13:53:16 +00:00
2019-01-24 21:52:21 +00:00
// ReSharper disable UnusedMember.Global
2018-08-30 13:53:16 +00:00
namespace Tapeti.Serilog
{
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
/// <summary>
/// Implements the Tapeti ILogger interface for Serilog output.
/// </summary>
2018-08-30 13:53:16 +00:00
public class TapetiSeriLogger : ILogger
{
2019-08-14 10:20:53 +00:00
private readonly ISerilogLogger seriLogger ;
2018-08-30 13:53:16 +00:00
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
public TapetiSeriLogger ( ISerilogLogger seriLogger )
2018-08-30 13:53:16 +00:00
{
this . seriLogger = seriLogger ;
}
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
public void Connect ( TapetiConnectionParams connectionParams , bool isReconnect )
2018-08-30 13:53:16 +00:00
{
2019-08-14 10:20:53 +00:00
seriLogger
. ForContext ( "isReconnect" , isReconnect )
. Information ( "Tapeti: trying to connect to {host}:{port}/{virtualHost}" ,
connectionParams . HostName ,
connectionParams . Port ,
connectionParams . VirtualHost ) ;
2018-08-30 13:53:16 +00:00
}
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
2019-01-08 15:36:52 +00:00
public void ConnectFailed ( TapetiConnectionParams connectionParams , Exception exception )
2018-08-30 13:53:16 +00:00
{
2019-01-08 15:36:52 +00:00
seriLogger . Error ( exception , "Tapeti: could not connect to {host}:{port}/{virtualHost}" ,
connectionParams . HostName ,
connectionParams . Port ,
connectionParams . VirtualHost ) ;
2018-08-30 13:53:16 +00:00
}
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
public void ConnectSuccess ( TapetiConnectionParams connectionParams , bool isReconnect )
2018-08-30 13:53:16 +00:00
{
2019-08-14 10:20:53 +00:00
seriLogger
. ForContext ( "isReconnect" , isReconnect )
. Information ( "Tapeti: successfully connected to {host}:{port}/{virtualHost}" ,
connectionParams . HostName ,
connectionParams . Port ,
connectionParams . VirtualHost ) ;
2018-08-30 13:53:16 +00:00
}
2019-08-14 10:20:53 +00:00
/// <inheritdoc />
public void ConsumeException ( Exception exception , IMessageContext messageContext , ConsumeResult consumeResult )
2018-08-30 13:53:16 +00:00
{
2019-08-14 10:20:53 +00:00
var contextLogger = seriLogger
. ForContext ( "consumeResult" , consumeResult )
. ForContext ( "exchange" , messageContext . Exchange )
. ForContext ( "queue" , messageContext . Queue )
. ForContext ( "routingKey" , messageContext . RoutingKey ) ;
if ( messageContext is IControllerMessageContext controllerMessageContext )
{
contextLogger = contextLogger
. ForContext ( "controller" , controllerMessageContext . Binding . Controller . FullName )
. ForContext ( "method" , controllerMessageContext . Binding . Method . Name ) ;
}
contextLogger . Error ( exception , "Tapeti: exception in message handler" ) ;
2018-08-30 13:53:16 +00:00
}
2019-08-20 09:47:53 +00:00
/// <inheritdoc />
public void QueueObsolete ( string queueName , bool deleted , uint messageCount )
{
if ( deleted )
seriLogger . Information ( "Tapeti: obsolete queue {queue} has been deleted" , queueName ) ;
else
seriLogger . Information ( "Tapeti: obsolete queue {queue} has been unbound but not yet deleted, {messageCount} messages remaining" , queueName , messageCount ) ;
}
2018-08-30 13:53:16 +00:00
}
}