From 285cd798a4d879319752d58364e3a7f08002ac62 Mon Sep 17 00:00:00 2001 From: Matthieu Livoye Date: Thu, 10 Mar 2016 16:36:29 +0100 Subject: [PATCH 1/3] Added support for multiple redis servers (master/server) Changed version to 1.0.3.0 --- src/NLog.Redis.Tests/RedisTargetTests.cs | 9 ++++----- src/NLog.Redis/NLog.Redis.nuspec | 2 +- src/NLog.Redis/Properties/AssemblyInfo.cs | 4 ++-- src/NLog.Redis/RedisConnectionManager.cs | 13 ++++++++----- src/NLog.Redis/RedisTarget.cs | 15 +++++---------- src/NuGetPack.bat | 2 +- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/NLog.Redis.Tests/RedisTargetTests.cs b/src/NLog.Redis.Tests/RedisTargetTests.cs index fb3d627..88ee996 100644 --- a/src/NLog.Redis.Tests/RedisTargetTests.cs +++ b/src/NLog.Redis.Tests/RedisTargetTests.cs @@ -17,8 +17,7 @@ namespace NLog.Redis.Tests public class RedisTargetTests { 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; @@ -42,8 +41,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; @@ -68,7 +66,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/NLog.Redis.nuspec b/src/NLog.Redis/NLog.Redis.nuspec index b7a82ef..f28f3b2 100644 --- a/src/NLog.Redis/NLog.Redis.nuspec +++ b/src/NLog.Redis/NLog.Redis.nuspec @@ -10,7 +10,7 @@ https://github.com/richclement/NLog.Redis false $description$ - 1.0.2 - Update dependencies + 1.0.3 - Support for master/slave configuration Copyright 2014 NLog Redis diff --git a/src/NLog.Redis/Properties/AssemblyInfo.cs b/src/NLog.Redis/Properties/AssemblyInfo.cs index d0b8dec..01f73fa 100644 --- a/src/NLog.Redis/Properties/AssemblyInfo.cs +++ b/src/NLog.Redis/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.2.0")] -[assembly: AssemblyFileVersion("1.0.2.0")] +[assembly: AssemblyVersion("1.0.3.0")] +[assembly: AssemblyFileVersion("1.0.3.0")] diff --git a/src/NLog.Redis/RedisConnectionManager.cs b/src/NLog.Redis/RedisConnectionManager.cs index b958ba0..6593799 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,14 @@ internal class RedisConnectionManager : IDisposable { private ConnectionMultiplexer _connectionMultiplexer; - private readonly string _host; + private readonly List _hosts; private readonly int _port; private readonly int _db; private readonly string _password; - public RedisConnectionManager(string host, int port, int db, string password) + public RedisConnectionManager(List hosts, int db, string password) { - _host = host; - _port = port; + _hosts = hosts; _db = db; _password = password; @@ -32,7 +32,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 34978bc..8bb9a3e 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; namespace NLog.Targets { @@ -10,17 +11,11 @@ public class RedisTarget : TargetWithLayout protected const string ChannelDataType = "channel"; /// - /// Sets the host name or IP Address of the redis server + /// Sets the hosts names or IP Addresses and ports of the redis servers /// [RequiredParameter] - public string Host { get; set; } - - /// - /// Sets the port number redis is running on - /// - [RequiredParameter] - public int Port { get; set; } - + public string Hosts { get; set; } + /// /// Sets the key to be used for either the list or the pub/sub channel in redis /// @@ -53,7 +48,7 @@ protected override void InitializeTarget() { base.InitializeTarget(); - _redisConnectionManager = new RedisConnectionManager(Host, Port, Db, Password); + _redisConnectionManager = new RedisConnectionManager(Hosts.Split(',').ToList(), Db, Password); } protected override void CloseTarget() diff --git a/src/NuGetPack.bat b/src/NuGetPack.bat index 8ba98ba..d238e5a 100644 --- a/src/NuGetPack.bat +++ b/src/NuGetPack.bat @@ -1 +1 @@ -.\.nuget\NuGet.exe pack -Symbols .\NLog.Redis\NLog.Redis.csproj \ No newline at end of file +.\.nuget\NuGet.exe pack -Symbols .\NLog.Redis\NLog.Redis.csproj -Prop Configuration=Release \ No newline at end of file From 1ff989ac8fe0d6b2d0c23ce28869103425aaa0bd Mon Sep 17 00:00:00 2001 From: Matthieu Livoye Date: Thu, 21 Apr 2016 10:40:29 +0200 Subject: [PATCH 2/3] Retro-compatibility for host/port parameter config !Removed RequiredParameter attribute for the switch between host and hosts! Marked host and port as deprecated --- src/NLog.Redis/Properties/AssemblyInfo.cs | 4 ++-- src/NLog.Redis/RedisConnectionManager.cs | 11 +++++++++++ src/NLog.Redis/RedisTarget.cs | 20 ++++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/NLog.Redis/Properties/AssemblyInfo.cs b/src/NLog.Redis/Properties/AssemblyInfo.cs index 01f73fa..60fe0c2 100644 --- a/src/NLog.Redis/Properties/AssemblyInfo.cs +++ b/src/NLog.Redis/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.3.0")] -[assembly: AssemblyFileVersion("1.0.3.0")] +[assembly: AssemblyVersion("1.0.3.1")] +[assembly: AssemblyFileVersion("1.0.3.1")] diff --git a/src/NLog.Redis/RedisConnectionManager.cs b/src/NLog.Redis/RedisConnectionManager.cs index 6593799..2c22f8d 100644 --- a/src/NLog.Redis/RedisConnectionManager.cs +++ b/src/NLog.Redis/RedisConnectionManager.cs @@ -22,6 +22,17 @@ public RedisConnectionManager(List hosts, int db, string password) InitializeConnection(); } + [Obsolete("Use constructor with hosts instead")] + public RedisConnectionManager(string host, int port, int db, string password) + { + _hosts = new List(); + _hosts.Add($"{host}:{port}"); + _db = db; + _password = password; + + InitializeConnection(); + } + private void InitializeConnection() { var connectionOptions = new ConfigurationOptions diff --git a/src/NLog.Redis/RedisTarget.cs b/src/NLog.Redis/RedisTarget.cs index 8bb9a3e..b854c5c 100644 --- a/src/NLog.Redis/RedisTarget.cs +++ b/src/NLog.Redis/RedisTarget.cs @@ -10,10 +10,21 @@ public class RedisTarget : TargetWithLayout protected const string ListDataType = "list"; protected const string ChannelDataType = "channel"; + /// + /// Sets the host name or IP Address of the redis server + /// + [Obsolete("Use hosts instead")] + public string Host { get; set; } + + /// + /// Sets the port number redis is running on + /// + [Obsolete("Use hosts instead")] + public int Port { get; set; } + /// /// Sets the hosts names or IP Addresses and ports of the redis servers /// - [RequiredParameter] public string Hosts { get; set; } /// @@ -48,7 +59,12 @@ protected override void InitializeTarget() { base.InitializeTarget(); - _redisConnectionManager = new RedisConnectionManager(Hosts.Split(',').ToList(), 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() From 885732790c3dbffeb348d3080f3c0ef935161aea Mon Sep 17 00:00:00 2001 From: MaTaM673 Date: Fri, 1 Dec 2017 09:22:13 +0100 Subject: [PATCH 3/3] Update RedisConnectionManager.cs Removed old private field. --- src/NLog.Redis/RedisConnectionManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NLog.Redis/RedisConnectionManager.cs b/src/NLog.Redis/RedisConnectionManager.cs index 2c22f8d..d629998 100644 --- a/src/NLog.Redis/RedisConnectionManager.cs +++ b/src/NLog.Redis/RedisConnectionManager.cs @@ -9,7 +9,6 @@ internal class RedisConnectionManager : IDisposable private ConnectionMultiplexer _connectionMultiplexer; private readonly List _hosts; - private readonly int _port; private readonly int _db; private readonly string _password;