Skip to content

Commit

Permalink
Support custom timestamp format
Browse files Browse the repository at this point in the history
Support customising the format of the timestamps.
Resolves #311.
  • Loading branch information
martincostello committed May 17, 2022
1 parent bbae6a8 commit e8c828a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MartinCostello.Logging.XUnit;
/// </summary>
public partial class XUnitLogger : ILogger
{
//// Based on https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging.Console/ConsoleLogger.cs
//// Based on https://github.com/dotnet/runtime/blob/65067052e433eda400c5e7cc9f7b21c84640f901/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLogger.cs#L41-L66

/// <summary>
/// The padding to use for log levels.
Expand All @@ -36,6 +36,11 @@ public partial class XUnitLogger : ILogger
[ThreadStatic]
private static StringBuilder? _logBuilder;

/// <summary>
/// The format string used to format the timestamp in log messages.
/// </summary>
private readonly string _timestampFormat;

/// <summary>
/// Gets or sets the filter to use.
/// </summary>
Expand All @@ -52,6 +57,7 @@ private XUnitLogger(string name, XUnitLoggerOptions? options)

_filter = options?.Filter ?? (static (_, _) => true);
_messageSinkMessageFactory = options?.MessageSinkMessageFactory ?? (message => new DiagnosticMessage(message));
_timestampFormat = options?.TimestampFormat ?? "u";
IncludeScopes = options?.IncludeScopes ?? false;
}

Expand Down Expand Up @@ -156,7 +162,8 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message
logBuilder.Append(Name);
logBuilder.Append('[');
logBuilder.Append(eventId);
logBuilder.AppendLine("]");
logBuilder.Append(']');
logBuilder.AppendLine();

if (IncludeScopes)
{
Expand Down Expand Up @@ -184,11 +191,17 @@ public virtual void WriteMessage(LogLevel logLevel, int eventId, string? message
logBuilder.Append(exception.ToString());
}

string formatted = logBuilder.ToString();
// Prefix the formatted message so it renders like this:
// [{timestamp}] {logLevelString}{message}
logBuilder.Insert(0, logLevelString);
logBuilder.Insert(0, "] ");
logBuilder.Insert(0, Clock().ToString(_timestampFormat, CultureInfo.CurrentCulture));
logBuilder.Insert(0, '[');

string line = logBuilder.ToString();

try
{
var line = $"[{Clock():u}] {logLevelString}{formatted}";
if (outputHelper != null)
{
outputHelper.WriteLine(line);
Expand Down
8 changes: 8 additions & 0 deletions src/Logging.XUnit/XUnitLoggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public XUnitLoggerOptions()
/// Gets or sets a value indicating whether to include scopes.
/// </summary>
public bool IncludeScopes { get; set; }

/// <summary>
/// Gets or sets format string used to format the timestamp in log messages. Defaults to <c>u</c>.
/// </summary>
#if NET7_0_OR_GREATER
[StringSyntax(StringSyntaxAttribute.DateTimeFormat)]
#endif
public string? TimestampFormat { get; set; }
}
1 change: 1 addition & 0 deletions tests/Logging.XUnit.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static void Can_Configure_xunit_For_ILoggerFactory_With_Options_Factory()
var options = new XUnitLoggerOptions()
{
Filter = (_, level) => level >= LogLevel.Error,
TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff",
};

var logger = BootstrapFactory((builder) => builder.AddXUnit(mock.Object, () => options));
Expand Down

0 comments on commit e8c828a

Please sign in to comment.