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 diff --git a/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs b/src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs index 80186f3e..a62ce929 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; } @@ -98,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) @@ -110,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) @@ -225,11 +223,11 @@ 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, CultureInfo.GetCultureInfo("en-US")); 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)); } } }