Skip to content

Commit

Permalink
Merge pull request #32 from luigiberrettini/dateFormatting
Browse files Browse the repository at this point in the history
Add configurable timestamp format to support also RFC3339 for rsyslog
  • Loading branch information
Jesper Hess Nielsen committed Feb 8, 2016
2 parents d36e8a2 + 1d3b37e commit 4f825e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 8 additions & 10 deletions src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class Syslog : TargetWithLayout
/// </summary>
public string Sender { get; set; }

/// <summary>
/// Gets or sets the timestamp format
/// </summary>
public string TimestampFormat { get; set; }

/// <summary>
/// Gets or sets the machine name hosting syslog
/// </summary>
Expand Down Expand Up @@ -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;
}
Expand All @@ -98,21 +104,13 @@ public Syslog()
/// <param name="logEvent">The NLog.LogEventInfo </param>
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)
{
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<string> GetFormattedMessageLines(LogEventInfo logEvent)
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 4f825e8

Please sign in to comment.