1
0
mirror of synced 2024-11-21 17:03:50 +00:00

Implemented logging for connection events

This commit is contained in:
Mark van Renswoude 2019-01-08 16:36:52 +01:00
parent a5aec88fa4
commit dcd22742e7
7 changed files with 36 additions and 24 deletions

View File

@ -1,8 +1,6 @@
using System;
using ISeriLogger = Serilog.ILogger;
// ReSharper disable UnusedMember.Global
namespace Tapeti.Serilog
{
public class TapetiSeriLogger: ILogger
@ -16,26 +14,31 @@ namespace Tapeti.Serilog
public void Connect(TapetiConnectionParams connectionParams)
{
// method not yet used in Tapeti
seriLogger.Information("Trying to connected to " + connectionParams.HostName);
seriLogger.Information("Tapeti: trying to connect to {host}:{port}/{virtualHost}",
connectionParams.HostName,
connectionParams.Port,
connectionParams.VirtualHost);
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
public void ConnectFailed(TapetiConnectionParams connectionParams, Exception exception)
{
// method not yet used in Tapeti
seriLogger.Error("Could not connect to " + connectionParams.HostName);
seriLogger.Error(exception, "Tapeti: could not connect to {host}:{port}/{virtualHost}",
connectionParams.HostName,
connectionParams.Port,
connectionParams.VirtualHost);
}
public void ConnectSuccess(TapetiConnectionParams connectionParams)
{
// method not yet used in Tapeti
seriLogger.Information("Succesfull connected to " + connectionParams.HostName);
seriLogger.Information("Tapeti: successfully connected to {host}:{port}/{virtualHost}",
connectionParams.HostName,
connectionParams.Port,
connectionParams.VirtualHost);
}
public void HandlerException(Exception e)
{
seriLogger.Error(e, "Exception handled by Tapeti");
seriLogger.Error(e, "Tapeti: exception in message handler");
}
}
}

View File

@ -16,6 +16,7 @@ namespace Tapeti.Connection
private const int PublishMaxConnectAttempts = 3;
private readonly IConfig config;
private readonly ILogger logger;
public TapetiConnectionParams ConnectionParams { get; set; }
public IConnectionEventListener ConnectionEventListener { get; set; }
@ -31,6 +32,7 @@ namespace Tapeti.Connection
{
this.config = config;
logger = config.DependencyResolver.Resolve<ILogger>();
messageSerializer = config.DependencyResolver.Resolve<IMessageSerializer>();
routingKeyStrategy = config.DependencyResolver.Resolve<IRoutingKeyStrategy>();
exchangeStrategy = config.DependencyResolver.Resolve<IExchangeStrategy>();
@ -201,6 +203,8 @@ namespace Tapeti.Connection
{
try
{
logger.Connect(ConnectionParams);
connection = connectionFactory.CreateConnection();
channelInstance = connection.CreateModel();
@ -212,10 +216,14 @@ namespace Tapeti.Connection
channelInstance.ModelShutdown += (sender, e) => ConnectionEventListener?.Disconnected();
ConnectionEventListener?.Connected();
logger.ConnectSuccess(ConnectionParams);
break;
}
catch (BrokerUnreachableException)
catch (BrokerUnreachableException e)
{
logger.ConnectFailed(ConnectionParams, e);
attempts++;
if (maxAttempts.HasValue && attempts > maxAttempts.Value)
throw;

View File

@ -6,17 +6,17 @@ namespace Tapeti.Default
{
public void Connect(TapetiConnectionParams connectionParams)
{
throw new NotImplementedException();
Console.WriteLine($"[Tapeti] Connecting to {connectionParams.HostName}:{connectionParams.Port}{connectionParams.VirtualHost}");
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
public void ConnectFailed(TapetiConnectionParams connectionParams, Exception exception)
{
throw new NotImplementedException();
Console.WriteLine($"[Tapeti] Connection failed: {exception}");
}
public void ConnectSuccess(TapetiConnectionParams connectionParams)
{
throw new NotImplementedException();
Console.WriteLine($"[Tapeti] Connected");
}
public void HandlerException(Exception e)

View File

@ -8,7 +8,7 @@ namespace Tapeti.Default
{
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
public void ConnectFailed(TapetiConnectionParams connectionParams, Exception exception)
{
}

View File

@ -9,7 +9,7 @@ namespace Tapeti
public interface ILogger
{
void Connect(TapetiConnectionParams connectionParams);
void ConnectFailed(TapetiConnectionParams connectionParams);
void ConnectFailed(TapetiConnectionParams connectionParams, Exception exception);
void ConnectSuccess(TapetiConnectionParams connectionParams);
void HandlerException(Exception e);
}

6
Test/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="rabbitmq:hostname" value="localhost" />
</appSettings>
</configuration>

View File

@ -1,25 +1,20 @@
using System;
using Tapeti;
// ReSharper disable UnusedMember.Global
namespace Test
{
public class MyLogger : ILogger
{
public void Connect(TapetiConnectionParams connectionParams)
{
throw new NotImplementedException();
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
public void ConnectFailed(TapetiConnectionParams connectionParams, Exception exception)
{
throw new NotImplementedException();
}
public void ConnectSuccess(TapetiConnectionParams connectionParams)
{
throw new NotImplementedException();
}
public void HandlerException(Exception e)