Home > Back-end >  How to log stack trace error when ConnectionMultiplexer fails to connect?
How to log stack trace error when ConnectionMultiplexer fails to connect?

Time:09-28

I have this code

private ILogger logger;
.
.
.
try
{
    redisClient = ConnectionMultiplexer.Connect(connectionString);
}
catch (Exception ex)
{
    logger.LogError(ex, ex.toString());
    redisClient = null;
    return;
}

We intended that if it failed to connect, we wanted to log the stack trace error. But it looks like that won't really cause and exception. it would just have redisClient.IsConnected return false

Is there a way to log stack trace or helful messsage why it failed to connect? This is something we had in mind?

private ILogger logger;
.
.
.
var redisClient = ConnectionMultiplexer.Connect(connectionString);
if(!redisClient.IsConnected)
{
    logger.LogError(redisClient.StackTraceError);
}

CodePudding user response:

I think I understood u now.

there is an event in ConnectionMultiplexer. that event is called ConnectionFailed

you dont need the try catch. you can do something like this :

redisClient.ConnectionFailed  = (sender, args) =>
{
    Console.WriteLine("Connection failed: "   args.Exception.Message);
};

Plus you can missaround with the args for extra details

Hope that helps

  • Related