From 58b3c4b3760792512acfdb1d6f32ebf0d60b65dd Mon Sep 17 00:00:00 2001 From: Luigi Berrettini Date: Mon, 8 Feb 2016 17:34:22 +0100 Subject: [PATCH 1/4] Add VS 2015 dot files to ignore list --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index cfe647e8..6b3c4a3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. +# VS 2015 dot files +.vs/ + # User-specific files *.suo *.user From f472286500f063d238b270c3c5b1d91d12f4d216 Mon Sep 17 00:00:00 2001 From: Luigi Berrettini Date: Mon, 8 Feb 2016 17:37:29 +0100 Subject: [PATCH 2/4] Add timestamp format configuration setting --- src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs index 80186f3e..57d98015 100644 --- a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs +++ b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs @@ -52,6 +52,11 @@ public class Syslog : TargetWithLayout /// public string Sender { get; set; } + /// + /// Gets or sets the timestamp format + /// + public string TimestampFormat { get; set; } + /// /// Gets or sets the machine name hosting syslog /// @@ -88,6 +93,7 @@ public Syslog() this.Sender = Assembly.GetCallingAssembly().GetName().Name; this.Facility = SyslogFacility.Local1; this.Protocol = ProtocolType.Udp; + this.TimestampFormat = "MMM dd HH:mm:ss "; this.MachineName = Dns.GetHostName(); this.SplitNewlines = true; } @@ -225,7 +231,7 @@ private byte[] BuildSyslogMessage(SyslogFacility facility, SyslogSeverity priori var calculatedPriority = (int)facility * 8 + (int)priority; var pri = "<" + calculatedPriority.ToString(CultureInfo.InvariantCulture) + ">"; - var timeToString = time.ToString("MMM dd HH:mm:ss "); + var timeToString = time.ToString(this.TimestampFormat); sender = sender + ": "; string[] strParams = { pri, timeToString, machine, sender, body, Environment.NewLine }; From 2675ef8da23e9f44d96a66e2bb127c30c2bf2841 Mon Sep 17 00:00:00 2001 From: Luigi Berrettini Date: Mon, 8 Feb 2016 17:39:11 +0100 Subject: [PATCH 3/4] Use culture info when calling ToString --- src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs index 57d98015..807f212e 100644 --- a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs +++ b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs @@ -104,11 +104,6 @@ public Syslog() /// The NLog.LogEventInfo protected override void Write(LogEventInfo logEvent) { - // Store the current UI culture - var currentCulture = Thread.CurrentThread.CurrentCulture; - // Set the current Locale to "en-US" for proper date formatting - Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); - var formattedMessageLines = this.GetFormattedMessageLines(logEvent); var severity = GetSyslogSeverity(logEvent.Level); foreach (var formattedMessageLine in formattedMessageLines) @@ -116,9 +111,6 @@ protected override void Write(LogEventInfo logEvent) var message = this.BuildSyslogMessage(this.Facility, severity, DateTime.Now, this.Sender, formattedMessageLine); SendMessage(this.SyslogServer, this.Port, message, this.Protocol, this.Ssl); } - - // Restore the original culture - Thread.CurrentThread.CurrentCulture = currentCulture; } private IEnumerable GetFormattedMessageLines(LogEventInfo logEvent) @@ -231,7 +223,7 @@ private byte[] BuildSyslogMessage(SyslogFacility facility, SyslogSeverity priori var calculatedPriority = (int)facility * 8 + (int)priority; var pri = "<" + calculatedPriority.ToString(CultureInfo.InvariantCulture) + ">"; - var timeToString = time.ToString(this.TimestampFormat); + var timeToString = time.ToString(this.TimestampFormat, CultureInfo.GetCultureInfo("en-US")); sender = sender + ": "; string[] strParams = { pri, timeToString, machine, sender, body, Environment.NewLine }; From 1d3b37e6e317ff009d792ef373e6a299c7f83df8 Mon Sep 17 00:00:00 2001 From: Luigi Berrettini Date: Mon, 8 Feb 2016 17:41:30 +0100 Subject: [PATCH 4/4] Replace string concat with join to increase performances --- src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs index 807f212e..a62ce929 100644 --- a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs +++ b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs @@ -227,7 +227,7 @@ private byte[] BuildSyslogMessage(SyslogFacility facility, SyslogSeverity priori sender = sender + ": "; string[] strParams = { pri, timeToString, machine, sender, body, Environment.NewLine }; - return Encoding.ASCII.GetBytes(string.Concat(strParams)); + return Encoding.ASCII.GetBytes(string.Join(string.Empty, strParams)); } } }