diff --git a/src/NLog.Targets.Syslog/MessageSend/SocketInitialization.cs b/src/NLog.Targets.Syslog/MessageSend/SocketInitialization.cs index 1925af6..267c567 100644 --- a/src/NLog.Targets.Syslog/MessageSend/SocketInitialization.cs +++ b/src/NLog.Targets.Syslog/MessageSend/SocketInitialization.cs @@ -5,16 +5,31 @@ using System.Runtime.InteropServices; using NLog.Targets.Syslog.Settings; +using Debug = System.Diagnostics.Debug; + namespace NLog.Targets.Syslog.MessageSend { internal abstract class SocketInitialization { public static SocketInitialization ForCurrentOs() { + string rt = RuntimeInformation.OSDescription; + + bool isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + + Debug.WriteLine($"===== NLog.Targets.Syslog detected OS: {rt}"); + Debug.WriteLine($"isMac: {isMac}"); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return new SocketInitializationForWindows(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + || + rt.ToLower().Contains("linux")) + { return new SocketInitializationForLinux(); + } + return new SocketInitializationForOsx(); } diff --git a/src/NLog.Targets.Syslog/Policies/PolicySet.cs b/src/NLog.Targets.Syslog/Policies/PolicySet.cs index 314b3c7..8f33089 100644 --- a/src/NLog.Targets.Syslog/Policies/PolicySet.cs +++ b/src/NLog.Targets.Syslog/Policies/PolicySet.cs @@ -1,31 +1,31 @@ -// Licensed under the BSD license -// See the LICENSE file in the project root for more information - -using System.Collections.Generic; - -namespace NLog.Targets.Syslog.Policies -{ - internal abstract class PolicySet - { - private readonly List> policies; - - protected PolicySet() - { - policies = new List>(); - } - - protected void AddPolicies(IEnumerable> policiesToAdd) - { - policies.AddRange(policiesToAdd); - } - - public string Apply(string s) - { - var afterApplication = s; - foreach (var policy in policies) - if (policy.IsApplicable()) - afterApplication = policy.Apply(afterApplication); - return afterApplication; - } - } +// Licensed under the BSD license +// See the LICENSE file in the project root for more information + +using System.Collections.Generic; + +namespace NLog.Targets.Syslog.Policies +{ + internal abstract class PolicySet + { + private readonly List> policies; + + protected PolicySet() + { + policies = new List>(); + } + + protected void AddPolicies(IEnumerable> policiesToAdd) + { + policies.AddRange(policiesToAdd); + } + + public string Apply(string s) + { + var afterApplication = s; + foreach (var policy in policies) + if (policy.IsApplicable()) + afterApplication = policy.Apply(afterApplication); + return afterApplication; + } + } } \ No newline at end of file diff --git a/src/NLog.Targets.Syslog/Settings/MessageBuilderConfig.cs b/src/NLog.Targets.Syslog/Settings/MessageBuilderConfig.cs index ac28a66..071d980 100644 --- a/src/NLog.Targets.Syslog/Settings/MessageBuilderConfig.cs +++ b/src/NLog.Targets.Syslog/Settings/MessageBuilderConfig.cs @@ -1,87 +1,87 @@ -// Licensed under the BSD license -// See the LICENSE file in the project root for more information - -using System; -using System.ComponentModel; -using NLog.Config; - -namespace NLog.Targets.Syslog.Settings -{ - /// - /// - /// Message build configuration - [NLogConfigurationItem] - public class MessageBuilderConfig : NotifyPropertyChanged, IDisposable - { - private Facility facility; - private LogLevelSeverityConfig perLogLevelSeverity; - private readonly PropertyChangedEventHandler perLogLevelSeverityPropsChanged; - private RfcNumber rfc; - private Rfc3164Config rfc3164; - private readonly PropertyChangedEventHandler rfc3164PropsChanged; - private Rfc5424Config rfc5424; - private readonly PropertyChangedEventHandler rfc5424PropsChanged; - - /// The Syslog facility to log from (its name e.g. local0 or local7) - public Facility Facility - { - get => facility; - set => SetProperty(ref facility, value); - } - - /// Log level Syslog severity related fields - public LogLevelSeverityConfig PerLogLevelSeverity - { - get => perLogLevelSeverity; - set => SetProperty(ref perLogLevelSeverity, value); - } - - /// The Syslog protocol RFC to be followed - public RfcNumber Rfc - { - get => rfc; - set => SetProperty(ref rfc, value); - } - - /// RFC 3164 related fields - public Rfc3164Config Rfc3164 - { - get => rfc3164; - set => SetProperty(ref rfc3164, value); - } - - /// RFC 5424 related fields - public Rfc5424Config Rfc5424 - { - get => rfc5424; - set => SetProperty(ref rfc5424, value); - } - - /// Builds a new instance of the MessageBuilderConfig class - public MessageBuilderConfig() - { - perLogLevelSeverity = new LogLevelSeverityConfig(); - perLogLevelSeverityPropsChanged = (sender, args) => OnPropertyChanged(nameof(PerLogLevelSeverity)); - - rfc = RfcNumber.Rfc5424; - - rfc3164 = new Rfc3164Config(); - rfc3164PropsChanged = (sender, args) => OnPropertyChanged(nameof(Rfc3164)); - rfc3164.PropertyChanged += rfc3164PropsChanged; - - rfc5424 = new Rfc5424Config(); - rfc5424PropsChanged = (sender, args) => OnPropertyChanged(nameof(Rfc5424)); - rfc5424.PropertyChanged += rfc5424PropsChanged; - } - - /// - /// Disposes the instance - public void Dispose() - { - perLogLevelSeverity.PropertyChanged -= perLogLevelSeverityPropsChanged; - rfc3164.PropertyChanged -= rfc3164PropsChanged; - rfc5424.PropertyChanged -= rfc5424PropsChanged; - rfc5424.Dispose(); - } - } +// Licensed under the BSD license +// See the LICENSE file in the project root for more information + +using System; +using System.ComponentModel; +using NLog.Config; + +namespace NLog.Targets.Syslog.Settings +{ + /// + /// + /// Message build configuration + [NLogConfigurationItem] + public class MessageBuilderConfig : NotifyPropertyChanged, IDisposable + { + private Facility facility; + private LogLevelSeverityConfig perLogLevelSeverity; + private readonly PropertyChangedEventHandler perLogLevelSeverityPropsChanged; + private RfcNumber rfc; + private Rfc3164Config rfc3164; + private readonly PropertyChangedEventHandler rfc3164PropsChanged; + private Rfc5424Config rfc5424; + private readonly PropertyChangedEventHandler rfc5424PropsChanged; + + /// The Syslog facility to log from (its name e.g. local0 or local7) + public Facility Facility + { + get => facility; + set => SetProperty(ref facility, value); + } + + /// Log level Syslog severity related fields + public LogLevelSeverityConfig PerLogLevelSeverity + { + get => perLogLevelSeverity; + set => SetProperty(ref perLogLevelSeverity, value); + } + + /// The Syslog protocol RFC to be followed + public RfcNumber Rfc + { + get => rfc; + set => SetProperty(ref rfc, value); + } + + /// RFC 3164 related fields + public Rfc3164Config Rfc3164 + { + get => rfc3164; + set => SetProperty(ref rfc3164, value); + } + + /// RFC 5424 related fields + public Rfc5424Config Rfc5424 + { + get => rfc5424; + set => SetProperty(ref rfc5424, value); + } + + /// Builds a new instance of the MessageBuilderConfig class + public MessageBuilderConfig() + { + perLogLevelSeverity = new LogLevelSeverityConfig(); + perLogLevelSeverityPropsChanged = (sender, args) => OnPropertyChanged(nameof(PerLogLevelSeverity)); + + rfc = RfcNumber.Rfc5424; + + rfc3164 = new Rfc3164Config(); + rfc3164PropsChanged = (sender, args) => OnPropertyChanged(nameof(Rfc3164)); + rfc3164.PropertyChanged += rfc3164PropsChanged; + + rfc5424 = new Rfc5424Config(); + rfc5424PropsChanged = (sender, args) => OnPropertyChanged(nameof(Rfc5424)); + rfc5424.PropertyChanged += rfc5424PropsChanged; + } + + /// + /// Disposes the instance + public void Dispose() + { + perLogLevelSeverity.PropertyChanged -= perLogLevelSeverityPropsChanged; + rfc3164.PropertyChanged -= rfc3164PropsChanged; + rfc5424.PropertyChanged -= rfc5424PropsChanged; + rfc5424.Dispose(); + } + } } \ No newline at end of file diff --git a/src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs b/src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs index 8910033..94775cb 100644 --- a/src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs +++ b/src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs @@ -1,67 +1,67 @@ -// Licensed under the BSD license -// See the LICENSE file in the project root for more information - -using System.Net; -using NLog.Config; -using NLog.Layouts; -using NLog.Targets.Syslog.Extensions; - -namespace NLog.Targets.Syslog.Settings -{ - /// - /// RFC 3164 configuration - [NLogConfigurationItem] - public class Rfc3164Config : NotifyPropertyChanged - { - private bool outputPri; - private bool outputHeader; - private bool outputSpaceBeforeMsg; - private Layout hostname; - private Layout tag; - - /// Whether to output or not the PRI part - public bool OutputPri - { - get => outputPri; - set => SetProperty(ref outputPri, value); - } - - /// Whether to output or not the HEADER part - public bool OutputHeader - { - get => outputHeader; - set => SetProperty(ref outputHeader, value); - } - - /// Whether to output or not the space before the MSG part - public bool OutputSpaceBeforeMsg - { - get => outputSpaceBeforeMsg; - set => SetProperty(ref outputSpaceBeforeMsg, value); - } - - /// The HOSTNAME field of the HEADER part - public Layout Hostname - { - get => hostname; - set => SetProperty(ref hostname, value); - } - - /// The TAG field of the MSG part - public Layout Tag - { - get => tag; - set => SetProperty(ref tag, value); - } - - /// Builds a new instance of the Rfc3164 class - public Rfc3164Config() - { - outputPri = true; - outputHeader = true; - outputSpaceBeforeMsg = true; - hostname = Dns.GetHostName(); - tag = UniversalAssembly.EntryAssembly().Name(); - } - } +// Licensed under the BSD license +// See the LICENSE file in the project root for more information + +using System.Net; +using NLog.Config; +using NLog.Layouts; +using NLog.Targets.Syslog.Extensions; + +namespace NLog.Targets.Syslog.Settings +{ + /// + /// RFC 3164 configuration + [NLogConfigurationItem] + public class Rfc3164Config : NotifyPropertyChanged + { + private bool outputPri; + private bool outputHeader; + private bool outputSpaceBeforeMsg; + private Layout hostname; + private Layout tag; + + /// Whether to output or not the PRI part + public bool OutputPri + { + get => outputPri; + set => SetProperty(ref outputPri, value); + } + + /// Whether to output or not the HEADER part + public bool OutputHeader + { + get => outputHeader; + set => SetProperty(ref outputHeader, value); + } + + /// Whether to output or not the space before the MSG part + public bool OutputSpaceBeforeMsg + { + get => outputSpaceBeforeMsg; + set => SetProperty(ref outputSpaceBeforeMsg, value); + } + + /// The HOSTNAME field of the HEADER part + public Layout Hostname + { + get => hostname; + set => SetProperty(ref hostname, value); + } + + /// The TAG field of the MSG part + public Layout Tag + { + get => tag; + set => SetProperty(ref tag, value); + } + + /// Builds a new instance of the Rfc3164 class + public Rfc3164Config() + { + outputPri = true; + outputHeader = true; + outputSpaceBeforeMsg = true; + hostname = Dns.GetHostName(); + tag = UniversalAssembly.EntryAssembly().Name(); + } + } } \ No newline at end of file diff --git a/src/NLog.Targets.Syslog/Settings/Rfc5424Config.cs b/src/NLog.Targets.Syslog/Settings/Rfc5424Config.cs index f522f4c..14584f1 100644 --- a/src/NLog.Targets.Syslog/Settings/Rfc5424Config.cs +++ b/src/NLog.Targets.Syslog/Settings/Rfc5424Config.cs @@ -1,124 +1,124 @@ -// Licensed under the BSD license -// See the LICENSE file in the project root for more information - -using System; -using System.ComponentModel; -using System.Net; -using System.Net.NetworkInformation; -using NLog.Config; -using NLog.Layouts; -using NLog.Targets.Syslog.Extensions; - -namespace NLog.Targets.Syslog.Settings -{ - /// - /// - /// RFC 5424 configuration - [NLogConfigurationItem] - public class Rfc5424Config : NotifyPropertyChanged, IDisposable - { - private const string DefaultVersion = "1"; - private const int DefaultTimestampFractionalDigits = 6; - private const string NilValue = "-"; - private int timestampFractionalDigits; - private Layout hostname; - private Layout appName; - private Layout procId; - private Layout msgId; - private StructuredDataConfig structuredData; - private readonly PropertyChangedEventHandler structuredDataPropsChanged; - private bool disableBom; - - /// The VERSION field of the HEADER part - public string Version { get; } - - /// The number of fractional digits for the TIMESTAMP field of the HEADER part - public int TimestampFractionalDigits - { - get => timestampFractionalDigits; - set => SetProperty(ref timestampFractionalDigits, value); - } - - /// The default HOSTNAME if no value is provided - public string DefaultHostname { get; } - - /// The HOSTNAME field of the HEADER part - public Layout Hostname - { - get => hostname; - set => SetProperty(ref hostname, value); - } - - /// The default APPNAME if no value is provided - public string DefaultAppName { get; } - - /// The APPNAME field of the HEADER part - public Layout AppName - { - get => appName; - set => SetProperty(ref appName, value); - } - - /// The PROCID field of the HEADER part - public Layout ProcId - { - get => procId; - set => SetProperty(ref procId, value); - } - - /// The MSGID field of the HEADER part - public Layout MsgId - { - get => msgId; - set => SetProperty(ref msgId, value); - } - - /// The STRUCTURED-DATA part - public StructuredDataConfig StructuredData - { - get => structuredData; - set => SetProperty(ref structuredData, value); - } - - /// Whether to remove or not BOM in the MSG part - /// RSyslog issue #284 - public bool DisableBom - { - get => disableBom; - set => SetProperty(ref disableBom, value); - } - - /// Builds a new instance of the Rfc5424 class - public Rfc5424Config() - { - Version = DefaultVersion; - TimestampFractionalDigits = DefaultTimestampFractionalDigits; - DefaultHostname = HostFqdn(); - hostname = DefaultHostname; - DefaultAppName = UniversalAssembly.EntryAssembly().Name(); - appName = DefaultAppName; - procId = NilValue; - msgId = NilValue; - structuredData = new StructuredDataConfig(); - structuredDataPropsChanged = (sender, args) => OnPropertyChanged(nameof(StructuredData)); - structuredData.PropertyChanged += structuredDataPropsChanged; - disableBom = false; - } - - /// - /// Disposes the instance - public void Dispose() - { - structuredData.PropertyChanged -= structuredDataPropsChanged; - structuredData.Dispose(); - } - - private static string HostFqdn() - { - var hostname = Dns.GetHostName(); - var domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName; - var domainAsSuffix = $".{domainName}"; - return hostname.EndsWith(domainAsSuffix, StringComparison.InvariantCulture) ? hostname : $"{hostname}{domainAsSuffix}"; - } - } +// Licensed under the BSD license +// See the LICENSE file in the project root for more information + +using System; +using System.ComponentModel; +using System.Net; +using System.Net.NetworkInformation; +using NLog.Config; +using NLog.Layouts; +using NLog.Targets.Syslog.Extensions; + +namespace NLog.Targets.Syslog.Settings +{ + /// + /// + /// RFC 5424 configuration + [NLogConfigurationItem] + public class Rfc5424Config : NotifyPropertyChanged, IDisposable + { + private const string DefaultVersion = "1"; + private const int DefaultTimestampFractionalDigits = 6; + private const string NilValue = "-"; + private int timestampFractionalDigits; + private Layout hostname; + private Layout appName; + private Layout procId; + private Layout msgId; + private StructuredDataConfig structuredData; + private readonly PropertyChangedEventHandler structuredDataPropsChanged; + private bool disableBom; + + /// The VERSION field of the HEADER part + public string Version { get; } + + /// The number of fractional digits for the TIMESTAMP field of the HEADER part + public int TimestampFractionalDigits + { + get => timestampFractionalDigits; + set => SetProperty(ref timestampFractionalDigits, value); + } + + /// The default HOSTNAME if no value is provided + public string DefaultHostname { get; } + + /// The HOSTNAME field of the HEADER part + public Layout Hostname + { + get => hostname; + set => SetProperty(ref hostname, value); + } + + /// The default APPNAME if no value is provided + public string DefaultAppName { get; } + + /// The APPNAME field of the HEADER part + public Layout AppName + { + get => appName; + set => SetProperty(ref appName, value); + } + + /// The PROCID field of the HEADER part + public Layout ProcId + { + get => procId; + set => SetProperty(ref procId, value); + } + + /// The MSGID field of the HEADER part + public Layout MsgId + { + get => msgId; + set => SetProperty(ref msgId, value); + } + + /// The STRUCTURED-DATA part + public StructuredDataConfig StructuredData + { + get => structuredData; + set => SetProperty(ref structuredData, value); + } + + /// Whether to remove or not BOM in the MSG part + /// RSyslog issue #284 + public bool DisableBom + { + get => disableBom; + set => SetProperty(ref disableBom, value); + } + + /// Builds a new instance of the Rfc5424 class + public Rfc5424Config() + { + Version = DefaultVersion; + TimestampFractionalDigits = DefaultTimestampFractionalDigits; + DefaultHostname = HostFqdn(); + hostname = DefaultHostname; + DefaultAppName = UniversalAssembly.EntryAssembly().Name(); + appName = DefaultAppName; + procId = NilValue; + msgId = NilValue; + structuredData = new StructuredDataConfig(); + structuredDataPropsChanged = (sender, args) => OnPropertyChanged(nameof(StructuredData)); + structuredData.PropertyChanged += structuredDataPropsChanged; + disableBom = false; + } + + /// + /// Disposes the instance + public void Dispose() + { + structuredData.PropertyChanged -= structuredDataPropsChanged; + structuredData.Dispose(); + } + + private static string HostFqdn() + { + var hostname = Dns.GetHostName(); + var domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName; + var domainAsSuffix = $".{domainName}"; + return hostname.EndsWith(domainAsSuffix, StringComparison.InvariantCulture) ? hostname : $"{hostname}{domainAsSuffix}"; + } + } } \ No newline at end of file diff --git a/src/NLog.Targets.Syslog/Settings/SdParamConfig.cs b/src/NLog.Targets.Syslog/Settings/SdParamConfig.cs index 68517dd..cd32752 100644 --- a/src/NLog.Targets.Syslog/Settings/SdParamConfig.cs +++ b/src/NLog.Targets.Syslog/Settings/SdParamConfig.cs @@ -1,31 +1,31 @@ -// Licensed under the BSD license -// See the LICENSE file in the project root for more information - -using NLog.Config; -using NLog.Layouts; - -namespace NLog.Targets.Syslog.Settings -{ - /// - /// Syslog SD-PARAM field configuration - [NLogConfigurationItem] - public class SdParamConfig : NotifyPropertyChanged - { - private Layout name; - private Layout value; - - /// The PARAM-NAME field of this SD-PARAM - public Layout Name - { - get => name; - set => SetProperty(ref name, value); - } - - /// The PARAM-VALUE field of this SD-PARAM - public Layout Value - { - get => value; - set => SetProperty(ref this.value, value); - } - } +// Licensed under the BSD license +// See the LICENSE file in the project root for more information + +using NLog.Config; +using NLog.Layouts; + +namespace NLog.Targets.Syslog.Settings +{ + /// + /// Syslog SD-PARAM field configuration + [NLogConfigurationItem] + public class SdParamConfig : NotifyPropertyChanged + { + private Layout name; + private Layout value; + + /// The PARAM-NAME field of this SD-PARAM + public Layout Name + { + get => name; + set => SetProperty(ref name, value); + } + + /// The PARAM-VALUE field of this SD-PARAM + public Layout Value + { + get => value; + set => SetProperty(ref this.value, value); + } + } } \ No newline at end of file