Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/NLog.Redis.Tests/IntegrationTests/RedisTargetTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace NLog.Redis.Tests.IntegrationTests
public abstract class RedisTargetTestsBase
{
protected const string RedisKey = "testkey";
protected const string RedisHost = "localhost";
protected const int RedisPort = 6379;
protected const string RedisHosts = "localhost:6379";
protected const string RedisPassword = "testingpassword";
protected string Password = null;

Expand All @@ -36,8 +35,7 @@ public void NLogRedisConfiguration(string dataType, bool usePassword = false)

// set target properties
redisTarget.Layout = "${uppercase:${level}} ${message}";
redisTarget.Host = RedisHost;
redisTarget.Port = RedisPort;
redisTarget.Hosts = RedisHosts;
redisTarget.Key = RedisKey;
redisTarget.Db = 0;
redisTarget.DataType = dataType;
Expand All @@ -62,7 +60,8 @@ public ConnectionMultiplexer GetRedisConnection(bool usePassword = false)
};
if (usePassword) connectionOptions.Password = RedisPassword;

connectionOptions.EndPoints.Add(RedisHost, RedisPort);
foreach(var host in RedisHosts.Split(','))
connectionOptions.EndPoints.Add(host);

return ConnectionMultiplexer.Connect(connectionOptions);
}
Expand Down
23 changes: 18 additions & 5 deletions src/NLog.Redis/RedisConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
using System;
using StackExchange.Redis;
using System.Collections.Generic;

namespace NLog.Targets
{
internal class RedisConnectionManager : IDisposable
{
private ConnectionMultiplexer _connectionMultiplexer;

private readonly string _host;
private readonly int _port;
private readonly List<string> _hosts;
private readonly int _db;
private readonly string _password;

public RedisConnectionManager(List<string> hosts, int db, string password)
{
_hosts = hosts;
_db = db;
_password = password;

InitializeConnection();
}

[Obsolete("Use constructor with hosts instead")]
public RedisConnectionManager(string host, int port, int db, string password)
{
_host = host;
_port = port;
_hosts = new List<string>();
_hosts.Add($"{host}:{port}");
_db = db;
_password = password;

Expand All @@ -32,7 +42,10 @@ private void InitializeConnection()
ConnectRetry = 3,
KeepAlive = 5
};
connectionOptions.EndPoints.Add(_host, _port);
foreach (var host in _hosts)
{
connectionOptions.EndPoints.Add(host);
}

if (!string.IsNullOrEmpty(_password))
{
Expand Down
21 changes: 16 additions & 5 deletions src/NLog.Redis/RedisTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using NLog.Config;
using System.Linq;
using NLog.Layouts;

namespace NLog.Targets
Expand All @@ -13,15 +14,20 @@ public class RedisTarget : TargetWithLayout
/// <summary>
/// Sets the host name or IP Address of the redis server
/// </summary>
[RequiredParameter]
[Obsolete("Use hosts instead")]
public string Host { get; set; }

/// <summary>
/// Sets the port number redis is running on
/// </summary>
[RequiredParameter]
[Obsolete("Use hosts instead")]
public int Port { get; set; }

/// <summary>
/// Sets the hosts names or IP Addresses and ports of the redis servers
/// </summary>
public string Hosts { get; set; }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move to list here. todo


/// <summary>
/// Sets the key to be used for either the list or the pub/sub channel in redis
/// </summary>
Expand Down Expand Up @@ -69,7 +75,12 @@ protected override void InitializeTarget()
{
base.InitializeTarget();
_dataTypeToLower = DataType?.ToLower();
_redisConnectionManager = new RedisConnectionManager(Host, Port, Db, Password);
if (!string.IsNullOrWhiteSpace(Host))
_redisConnectionManager = new RedisConnectionManager(Host, Port, Db, Password);
else if (!string.IsNullOrWhiteSpace(Hosts))
_redisConnectionManager = new RedisConnectionManager(Hosts.Split(',').ToList(), Db, Password);
else
throw new ArgumentException("At least a host must be set");
}

protected override void CloseTarget()
Expand Down Expand Up @@ -99,5 +110,5 @@ protected override void Write(LogEventInfo logEvent)
throw new Exception("no data type defined for redis");
}
}
}
}
}
}