Last active
January 28, 2024 04:04
-
-
Save PaulFarry/5457af2228ab7c31d5e74937d0da082f to your computer and use it in GitHub Desktop.
Serilog Console Date Formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/serilog/serilog/wiki/Formatting-Output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment