diff --git a/src/NLog.Redis.Tests/IntegrationTests/RedisTargetTestsBase.cs b/src/NLog.Redis.Tests/IntegrationTests/RedisTargetTestsBase.cs index 6b94f63..3cbb454 100644 --- a/src/NLog.Redis.Tests/IntegrationTests/RedisTargetTestsBase.cs +++ b/src/NLog.Redis.Tests/IntegrationTests/RedisTargetTestsBase.cs @@ -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; @@ -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; @@ -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); } diff --git a/src/NLog.Redis/RedisConnectionManager.cs b/src/NLog.Redis/RedisConnectionManager.cs index b958ba0..d629998 100644 --- a/src/NLog.Redis/RedisConnectionManager.cs +++ b/src/NLog.Redis/RedisConnectionManager.cs @@ -1,5 +1,6 @@ using System; using StackExchange.Redis; +using System.Collections.Generic; namespace NLog.Targets { @@ -7,15 +8,24 @@ internal class RedisConnectionManager : IDisposable { private ConnectionMultiplexer _connectionMultiplexer; - private readonly string _host; - private readonly int _port; + private readonly List _hosts; private readonly int _db; private readonly string _password; + public RedisConnectionManager(List 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(); + _hosts.Add($"{host}:{port}"); _db = db; _password = password; @@ -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)) { diff --git a/src/NLog.Redis/RedisTarget.cs b/src/NLog.Redis/RedisTarget.cs index 03838a9..c3b5eb8 100644 --- a/src/NLog.Redis/RedisTarget.cs +++ b/src/NLog.Redis/RedisTarget.cs @@ -1,5 +1,6 @@ using System; using NLog.Config; +using System.Linq; using NLog.Layouts; namespace NLog.Targets @@ -13,15 +14,20 @@ public class RedisTarget : TargetWithLayout /// /// Sets the host name or IP Address of the redis server /// - [RequiredParameter] + [Obsolete("Use hosts instead")] public string Host { get; set; } /// /// Sets the port number redis is running on /// - [RequiredParameter] + [Obsolete("Use hosts instead")] public int Port { get; set; } + /// + /// Sets the hosts names or IP Addresses and ports of the redis servers + /// + public string Hosts { get; set; } + /// /// Sets the key to be used for either the list or the pub/sub channel in redis /// @@ -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() @@ -99,5 +110,5 @@ protected override void Write(LogEventInfo logEvent) throw new Exception("no data type defined for redis"); } } - } -} + } + }