Skip to content

Instantly share code, notes, and snippets.

@PaulFarry
Last active January 28, 2024 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulFarry/5457af2228ab7c31d5e74937d0da082f to your computer and use it in GitHub Desktop.
Save PaulFarry/5457af2228ab7c31d5e74937d0da082f to your computer and use it in GitHub Desktop.
Serilog Console Date Formatting
{
"Serilog": {
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341"
}
},
{
"Name": "Console",
"Args": {
"FormatProvider": "Farryland.Debugger.DateFormatter, Farryland.Debugger",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Literate, Serilog.Sinks.Console"
}
}
]
}
}
using System;
using System.Globalization;
namespace Farryland.Debugger
{
internal class DateFormatter : IFormatProvider
{
readonly IFormatProvider basedOn;
readonly string shortDatePattern;
public DateFormatter() : this("dd-MMM-yyyy", new CultureInfo("en-AU"))
{
}
public DateFormatter(string shortDatePattern, IFormatProvider basedOn)
{
this.shortDatePattern = shortDatePattern;
this.basedOn = basedOn;
}
public object GetFormat(Type formatType)
{
if (formatType == typeof(DateTimeFormatInfo))
{
var basedOnFormatInfo = (DateTimeFormatInfo)basedOn.GetFormat(formatType);
var dateFormatInfo = (DateTimeFormatInfo)basedOnFormatInfo.Clone();
dateFormatInfo.ShortDatePattern = this.shortDatePattern;
return dateFormatInfo;
}
return this.basedOn.GetFormat(formatType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment