Skip to content

Commit

Permalink
Handle config changes (#91 closes #82)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiberrettini authored Apr 11, 2017
1 parent a13b1c1 commit 4ee4e8a
Show file tree
Hide file tree
Showing 16 changed files with 462 additions and 105 deletions.
1 change: 1 addition & 0 deletions src/NLog.Targets.Syslog/NLog.Targets.Syslog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\AssemblyExtensions.cs" />
<Compile Include="Settings\NotifyPropertyChanged.cs" />
<Compile Include="Settings\UniversalAssembly.cs" />
<Compile Include="Extensions\AsyncLogEventInfoExtensions.cs" />
<Compile Include="AsyncLogger.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/NLog.Targets.Syslog/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCopyright("Copyright © 2013 - present by Jesper Hess Nielsen, Luigi Berrettini and others: https://github.com/graffen/NLog.Targets.Syslog/graphs/contributors")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.1.1.0")]
[assembly: AssemblyInformationalVersion("3.1.1")]
[assembly: AssemblyFileVersion("3.1.2.0")]
[assembly: AssemblyInformationalVersion("3.1.2")]
59 changes: 49 additions & 10 deletions src/NLog.Targets.Syslog/Settings/EnforcementConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,83 @@
// See the LICENSE file in the project root for more information

using System;
using System.ComponentModel;

namespace NLog.Targets.Syslog.Settings
{
/// <summary>Enforcement configuration</summary>
public class EnforcementConfig
public class EnforcementConfig : NotifyPropertyChanged, IDisposable
{
private ThrottlingConfig throttling;
private readonly PropertyChangedEventHandler throttlingPropsChanged;
private int messageProcessors;
private bool splitOnNewLine;
private bool transliterate;
private bool replaceInvalidCharacters;
private bool truncateFieldsToMaxLength;
private long truncateMessageTo;

/// <summary>Throttling to be triggered when a configured number of log entries are waiting to be processed</summary>
public ThrottlingConfig Throttling { get; set; }
public ThrottlingConfig Throttling
{
get { return throttling; }
set { SetProperty(ref throttling, value); }
}

/// <summary>The amount of parallel message processors</summary>
public int MessageProcessors
{
get { return messageProcessors; }
set { messageProcessors = value <= 0 ? Environment.ProcessorCount : value; }
set { SetProperty(ref messageProcessors, value <= 0 ? Environment.ProcessorCount : value); }
}

/// <summary>Whether or not to split each log entry by newlines and send each line separately</summary>
public bool SplitOnNewLine { get; set; }
public bool SplitOnNewLine
{
get { return splitOnNewLine; }
set { SetProperty(ref splitOnNewLine, value); }
}

/// <summary>Whether or not to transliterate from Unicode to ASCII</summary>
public bool Transliterate { get; set; }
public bool Transliterate
{
get { return transliterate; }
set { SetProperty(ref transliterate, value); }
}

/// <summary>Whether or not to replace invalid characters on the basis of RFC rules</summary>
public bool ReplaceInvalidCharacters { get; set; }
public bool ReplaceInvalidCharacters
{
get { return replaceInvalidCharacters; }
set { SetProperty(ref replaceInvalidCharacters, value); }
}

/// <summary>Whether or not to truncate fields to the max length specified in RFCs</summary>
public bool TruncateFieldsToMaxLength { get; set; }
public bool TruncateFieldsToMaxLength
{
get { return truncateFieldsToMaxLength; }
set { SetProperty(ref truncateFieldsToMaxLength, value); }
}

/// <summary>The length to truncate the Syslog message to or zero</summary>
public long TruncateMessageTo { get; set; }
public long TruncateMessageTo
{
get { return truncateMessageTo; }
set { SetProperty(ref truncateMessageTo, value); }
}

/// <summary>Builds a new instance of the EnforcementConfig class</summary>
public EnforcementConfig()
{
MessageProcessors = 1;
Throttling = new ThrottlingConfig();
throttling = new ThrottlingConfig();
throttlingPropsChanged = (sender, args) => OnPropertyChanged(nameof(Throttling));
throttling.PropertyChanged += throttlingPropsChanged;
messageProcessors = 1;
}

public void Dispose()
{
throttling.PropertyChanged -= throttlingPropsChanged;
}
}
}
29 changes: 22 additions & 7 deletions src/NLog.Targets.Syslog/Settings/KeepAliveConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@ namespace NLog.Targets.Syslog.Settings
{
/// <summary>KeepAlive configuration</summary>
/// <remarks>The number of keep-alive probes (data retransmissions) is set to 10 and cannot be changed</remarks>
public class KeepAliveConfig
public class KeepAliveConfig : NotifyPropertyChanged
{
private const int DefaultTimeout = 5000;
private const int DefaultInterval = 1000;
private bool enabled;
private int timeout;
private int interval;

/// <summary>Whether to use keep-alive or not</summary>
public bool Enabled { get; set; }
public bool Enabled
{
get { return enabled; }
set { SetProperty(ref enabled, value); }
}

/// <summary>The timeout, in milliseconds, with no activity until the first keep-alive packet is sent</summary>
/// <remarks>The default value, on TCP socket initialization, is 2 hours</remarks>
public int Timeout { get; set; }
public int Timeout
{
get { return timeout; }
set { SetProperty(ref timeout, value); }
}

/// <summary>The interval, in milliseconds, between when successive keep-alive packets are sent if no acknowledgement is received</summary>
/// <remarks>The default value, on TCP socket initialization, is 1 second</remarks>
public int Interval { get; set; }
public int Interval
{
get { return interval; }
set { SetProperty(ref interval, value); }
}

public KeepAliveConfig()
{
Enabled = true;
Timeout = DefaultTimeout;
Interval = DefaultInterval;
enabled = true;
timeout = DefaultTimeout;
interval = DefaultInterval;
}
}
}
54 changes: 46 additions & 8 deletions src/NLog.Targets.Syslog/Settings/MessageBuilderConfig.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
// Licensed under the BSD license
// See the LICENSE file in the project root for more information

using System;
using System.ComponentModel;

namespace NLog.Targets.Syslog.Settings
{
/// <summary>Message build configuration</summary>
public class MessageBuilderConfig
public class MessageBuilderConfig : NotifyPropertyChanged, IDisposable
{
private Facility facility;
private RfcNumber rfc;
private Rfc3164Config rfc3164;
private readonly PropertyChangedEventHandler rfc3164PropsChanged;
private Rfc5424Config rfc5424;
private readonly PropertyChangedEventHandler rfc5424PropsChanged;

/// <summary>The Syslog facility to log from (its name e.g. local0 or local7)</summary>
public Facility Facility { get; set; }
public Facility Facility
{
get { return facility; }
set { SetProperty(ref facility, value); }
}

/// <summary>The Syslog protocol RFC to be followed</summary>
public RfcNumber Rfc { get; set; }
public RfcNumber Rfc
{
get { return rfc; }
set { SetProperty(ref rfc, value); }
}

/// <summary>RFC 3164 related fields</summary>
public Rfc3164Config Rfc3164 { get; set; }
public Rfc3164Config Rfc3164
{
get { return rfc3164; }
set { SetProperty(ref rfc3164, value); }
}

/// <summary>RFC 5424 related fields</summary>
public Rfc5424Config Rfc5424 { get; set; }
public Rfc5424Config Rfc5424
{
get { return rfc5424; }
set { SetProperty(ref rfc5424, value); }
}

/// <summary>Builds a new instance of the MessageBuilderConfig class</summary>
public MessageBuilderConfig()
{
Rfc = RfcNumber.Rfc5424;
Rfc3164 = new Rfc3164Config();
Rfc5424 = new Rfc5424Config();
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;
}

public void Dispose()
{
rfc3164.PropertyChanged -= rfc3164PropsChanged;
rfc5424.PropertyChanged -= rfc5424PropsChanged;
rfc5424.Dispose();
}
}
}
45 changes: 39 additions & 6 deletions src/NLog.Targets.Syslog/Settings/MessageTransmitterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
// Licensed under the BSD license
// See the LICENSE file in the project root for more information

using System;
using System.ComponentModel;

namespace NLog.Targets.Syslog.Settings
{
/// <summary>Message transmission configuration</summary>
public class MessageTransmitterConfig
public class MessageTransmitterConfig : NotifyPropertyChanged, IDisposable
{
private ProtocolType protocol;
private UdpConfig udp;
private readonly PropertyChangedEventHandler udpPropsChanged;
private TcpConfig tcp;
private readonly PropertyChangedEventHandler tcpPropsChanged;

/// <summary>The Syslog server protocol</summary>
public ProtocolType Protocol { get; set; }
public ProtocolType Protocol
{
get { return protocol; }
set { SetProperty(ref protocol, value); }
}

/// <summary>UDP related fields</summary>
public UdpConfig Udp { get; set; }
public UdpConfig Udp
{
get { return udp; }
set { SetProperty(ref udp, value); }
}

/// <summary>TCP related fields</summary>
public TcpConfig Tcp { get; set; }
public TcpConfig Tcp
{
get { return tcp; }
set { SetProperty(ref tcp, value); }
}

/// <summary>Builds a new instance of the MessageTransmitterConfig class</summary>
public MessageTransmitterConfig()
{
Udp = new UdpConfig();
Tcp = new TcpConfig();
udp = new UdpConfig();
udpPropsChanged = (sender, args) => OnPropertyChanged(nameof(Udp));
udp.PropertyChanged += udpPropsChanged;

tcp = new TcpConfig();
tcpPropsChanged = (sender, args) => OnPropertyChanged(nameof(Tcp));
tcp.PropertyChanged += tcpPropsChanged;
}

public void Dispose()
{
udp.PropertyChanged -= udpPropsChanged;
tcp.PropertyChanged -= tcpPropsChanged;
tcp.Dispose();
}
}
}
44 changes: 44 additions & 0 deletions src/NLog.Targets.Syslog/Settings/NotifyPropertyChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace NLog.Targets.Syslog.Settings
{
/// <summary>Implementation of <see cref="INotifyPropertyChanged" /> to simplify config settings</summary>
public abstract class NotifyPropertyChanged : INotifyPropertyChanged
{
/// <summary>Multicast event for property change notifications</summary>
public event PropertyChangedEventHandler PropertyChanged;

protected bool SetProperty<T>(ref T oldValue, T newValue, [CallerMemberName] string propertyName = null)
{
if (Equals(oldValue, newValue))
{
return false;
}

oldValue = newValue;
OnPropertyChanged(propertyName);
return true;
}

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected NotifyCollectionChangedEventHandler CollectionChangedFactory(PropertyChangedEventHandler onElemPropsChanged)
{
return (sender, eventArgs) =>
{
if (eventArgs.NewItems != null)
foreach (INotifyPropertyChanged item in eventArgs.NewItems)
item.PropertyChanged += onElemPropsChanged;

if (eventArgs.OldItems != null)
foreach (INotifyPropertyChanged item in eventArgs.OldItems)
item.PropertyChanged -= onElemPropsChanged;
};
}
}
}
23 changes: 17 additions & 6 deletions src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@
// See the LICENSE file in the project root for more information

using NLog.Layouts;
using System.Net;
using NLog.Targets.Syslog.Extensions;
using System.Net;

namespace NLog.Targets.Syslog.Settings
{
/// <summary>RFC 3164 configuration</summary>
public class Rfc3164Config
public class Rfc3164Config : NotifyPropertyChanged
{
private Layout hostname;
private Layout tag;

/// <summary>The HOSTNAME field of the HEADER part</summary>
public Layout Hostname { get; set; }
public Layout Hostname
{
get { return hostname; }
set { SetProperty(ref hostname, value); }
}

/// <summary>The TAG field of the MSG part</summary>
public Layout Tag { get; set; }
public Layout Tag
{
get { return tag; }
set { SetProperty(ref tag, value); }
}

/// <summary>Builds a new instance of the Rfc3164 class</summary>
public Rfc3164Config()
{
Hostname = Dns.GetHostName();
Tag = UniversalAssembly.EntryAssembly().Name();
hostname = Dns.GetHostName();
tag = UniversalAssembly.EntryAssembly().Name();
}
}
}
Loading

0 comments on commit 4ee4e8a

Please sign in to comment.