From 6c0f22a11e5a4a910309a5d507cb34dbb2b18d0d Mon Sep 17 00:00:00 2001 From: Robin Dadswell <19610103+RobinDadswell@users.noreply.github.com> Date: Mon, 27 Sep 2021 23:30:44 +0100 Subject: [PATCH] New: Added UDP syslog support (cherry picked from commit 8d856b2edb8bf46a2b516d5f7644ae3fa1151323) --- src/NzbDrone.Common/Sonarr.Common.csproj | 3 ++- .../Configuration/ConfigFileProvider.cs | 12 ++++++++- .../Instrumentation/ReconfigureLogging.cs | 25 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Common/Sonarr.Common.csproj b/src/NzbDrone.Common/Sonarr.Common.csproj index a6e53bc85..1de976996 100644 --- a/src/NzbDrone.Common/Sonarr.Common.csproj +++ b/src/NzbDrone.Common/Sonarr.Common.csproj @@ -7,6 +7,7 @@ + @@ -30,4 +31,4 @@ ExceptionMessages.Designer.cs - \ No newline at end of file + diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 556050b9a..73df37d95 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -42,6 +42,9 @@ namespace NzbDrone.Core.Configuration bool UpdateAutomatically { get; } UpdateMechanism UpdateMechanism { get; } string UpdateScriptPath { get; } + string SyslogServer { get; } + int SyslogPort { get; } + string SyslogLevel { get; } } public class ConfigFileProvider : IConfigFileProvider @@ -209,7 +212,14 @@ namespace NzbDrone.Core.Configuration public string UpdateScriptPath => GetValue("UpdateScriptPath", "", false); - public int GetValueInt(string key, int defaultValue) + public string SyslogServer => GetValue("SyslogServer", "", persist: false); + + public int SyslogPort => GetValueInt("SyslogPort", 514, persist: false); + + public string SyslogLevel => GetValue("SyslogLevel", LogLevel, persist: false).ToLowerInvariant(); + + + public int GetValueInt(string key, int defaultValue, bool persist = true) { return Convert.ToInt32(GetValue(key, defaultValue)); } diff --git a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs index acd25e57e..15ce3c808 100644 --- a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs +++ b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using NLog; using NLog.Config; +using NLog.Targets.Syslog; +using NLog.Targets.Syslog.Settings; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; using NzbDrone.Common.Instrumentation.Sentry; @@ -32,6 +34,11 @@ namespace NzbDrone.Core.Instrumentation else minimumConsoleLogLevel = LogLevel.Info; + if (_configFileProvider.SyslogServer.IsNotNullOrWhiteSpace()) + { + SetSyslogParameters(_configFileProvider.SyslogServer, _configFileProvider.SyslogPort, minimumLogLevel); + } + var rules = LogManager.Configuration.LoggingRules; //Console @@ -81,6 +88,24 @@ namespace NzbDrone.Core.Instrumentation } } + private void SetSyslogParameters(string syslogServer, int syslogPort, LogLevel minimumLogLevel) + { + var syslogTarget = new SyslogTarget(); + + syslogTarget.Name = "syslogTarget"; + syslogTarget.MessageSend.Protocol = ProtocolType.Udp; + syslogTarget.MessageSend.Udp.Port = syslogPort; + syslogTarget.MessageSend.Udp.Server = syslogServer; + syslogTarget.MessageSend.Udp.ReconnectInterval = 500; + syslogTarget.MessageCreation.Rfc = RfcNumber.Rfc5424; + syslogTarget.MessageCreation.Rfc5424.AppName = BuildInfo.AppName; + + var loggingRule = new LoggingRule("*", minimumLogLevel, syslogTarget); + + LogManager.Configuration.AddTarget("syslogTarget", syslogTarget); + LogManager.Configuration.LoggingRules.Add(loggingRule); + } + private List GetLogLevels() { return new List