From 26b9b7665a68059c560e8631a023339f4e3fe6ad 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) --- .../Configuration/ConfigFileProvider.cs | 5 ++++ .../Instrumentation/ReconfigureLogging.cs | 25 +++++++++++++++++++ src/NzbDrone.Core/Lidarr.Core.csproj | 1 + 3 files changed, 31 insertions(+) diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 7119871f0..861cd6949 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -44,6 +44,8 @@ namespace NzbDrone.Core.Configuration bool UpdateAutomatically { get; } UpdateMechanism UpdateMechanism { get; } string UpdateScriptPath { get; } + string SyslogServer { get; } + int SyslogPort { get; } } public class ConfigFileProvider : IConfigFileProvider @@ -208,6 +210,9 @@ namespace NzbDrone.Core.Configuration public string UpdateScriptPath => GetValue("UpdateScriptPath", "", false); + public string SyslogServer => GetValue("SyslogServer", "", persist: false); + public int SyslogPort => GetValueInt("SyslogPort", 514, persist: false); + public int GetValueInt(string key, int defaultValue, bool persist = true) { return Convert.ToInt32(GetValue(key, defaultValue, persist)); diff --git a/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs b/src/NzbDrone.Core/Instrumentation/ReconfigureLogging.cs index 35421a90d..6e13f3811 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; @@ -40,6 +42,11 @@ namespace NzbDrone.Core.Instrumentation minimumConsoleLogLevel = LogLevel.Info; } + if (_configFileProvider.SyslogServer.IsNotNullOrWhiteSpace()) + { + SetSyslogParameters(_configFileProvider.SyslogServer, _configFileProvider.SyslogPort, minimumLogLevel); + } + var rules = LogManager.Configuration.LoggingRules; //Console @@ -101,6 +108,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 diff --git a/src/NzbDrone.Core/Lidarr.Core.csproj b/src/NzbDrone.Core/Lidarr.Core.csproj index c7b490b2c..17b072bea 100644 --- a/src/NzbDrone.Core/Lidarr.Core.csproj +++ b/src/NzbDrone.Core/Lidarr.Core.csproj @@ -16,6 +16,7 @@ +