Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShortDateLayoutRenderer with support for IRawValue + IStringValueRenderer #3337

Merged
merged 2 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/NLog/LayoutRenderers/ShortDateLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ namespace NLog.LayoutRenderers
using System.Globalization;
using System.Text;
using NLog.Config;
using NLog.Internal;

/// <summary>
/// The short date in a sortable format yyyy-MM-dd.
/// </summary>
[LayoutRenderer("shortdate")]
[ThreadAgnostic]
[ThreadSafe]
public class ShortDateLayoutRenderer : LayoutRenderer
public class ShortDateLayoutRenderer : LayoutRenderer, IRawValue, IStringValueRenderer
{
/// <summary>
/// Gets or sets a value indicating whether to output UTC time instead of local time.
Expand All @@ -63,11 +64,13 @@ public class ShortDateLayoutRenderer : LayoutRenderer
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var timestamp = logEvent.TimeStamp;
if (UniversalTime)
{
timestamp = timestamp.ToUniversalTime();
}
string formattedDate = GetStringValue(logEvent);
builder.Append(formattedDate);
}

private string GetStringValue(LogEventInfo logEvent)
{
DateTime timestamp = GetValue(logEvent);

var cachedDateFormatted = _cachedDateFormatted;
if (cachedDateFormatted.Date != timestamp.Date)
Expand All @@ -76,9 +79,27 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
_cachedDateFormatted = cachedDateFormatted = new CachedDateFormatted(timestamp.Date, formatTime);
}

builder.Append(cachedDateFormatted.FormattedDate);
return cachedDateFormatted.FormattedDate;
}

private DateTime GetValue(LogEventInfo logEvent)
{
var timestamp = logEvent.TimeStamp;
if (UniversalTime)
{
timestamp = timestamp.ToUniversalTime();
}
return timestamp;
}

bool IRawValue.TryGetRawValue(LogEventInfo logEvent, out object value)
{
value = GetValue(logEvent).Date; // Only Date-part
return true;
}

string IStringValueRenderer.GetFormattedString(LogEventInfo logEvent) => GetStringValue(logEvent);

private class CachedDateFormatted
{
public CachedDateFormatted(DateTime date, string formattedDate)
Expand Down
1 change: 1 addition & 0 deletions tests/NLog.UnitTests/Targets/DatabaseTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ public static IEnumerable<object[]> ConvertFromStringTestCases()
yield return new object[] { "${db-null}", DbType.DateTime, DBNull.Value };
yield return new object[] { "${event-properties:userid}", DbType.Int32, 0 };
yield return new object[] { "${date:universalTime=true:format=yyyy-MM:norawvalue=true}", DbType.DateTime, DateTime.SpecifyKind(DateTime.UtcNow.Date.AddDays(-DateTime.UtcNow.Day + 1), DateTimeKind.Unspecified)};
yield return new object[] { "${shortdate:universalTime=true}", DbType.DateTime, DateTime.UtcNow.Date };
}

[Fact]
Expand Down