From 3816bc2813375f22b955b9cd3d7c760c7f9ab7ac Mon Sep 17 00:00:00 2001 From: Andrii Annenko Date: Tue, 12 Nov 2024 10:35:41 +0100 Subject: [PATCH] Hide TryFormat extensions under preprocessor directives for target frameworks greater than netstandard2.0 --- .../Extensions/DateTimeExtensions.cs | 28 ++++++++++++------- .../Extensions/IntExtensions.cs | 2 ++ src/NReco.Logging.File/FileLogger.cs | 16 +++++------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/NReco.Logging.File/Extensions/DateTimeExtensions.cs b/src/NReco.Logging.File/Extensions/DateTimeExtensions.cs index 1230773..c670868 100644 --- a/src/NReco.Logging.File/Extensions/DateTimeExtensions.cs +++ b/src/NReco.Logging.File/Extensions/DateTimeExtensions.cs @@ -4,18 +4,23 @@ namespace NReco.Logging.File.Extensions { public static class DateTimeExtensions { - public static bool TryFormatO(this DateTime dateTime, Span destination, out int charsWritten) { + public static int GetFormattedLength(this DateTime dateTime) { const int BaseCharCountInFormatO = 27; - int charsRequired = BaseCharCountInFormatO; - var kind = dateTime.Kind; - var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime); - if (kind == DateTimeKind.Local) { - charsRequired += 6; - } - else if (kind == DateTimeKind.Utc) { - charsRequired++; - } + return BaseCharCountInFormatO + dateTime.Kind switch { + DateTimeKind.Local => 6, + DateTimeKind.Utc => 1, + _ => 0 + }; + } + +#if NETSTANDARD2_0 + public static bool TryFormat(this DateTime date, Span destination, out int charsWritten) { + return TryFormatO(date, destination, out charsWritten); + } + + public static bool TryFormatO(this DateTime dateTime, Span destination, out int charsWritten) { + var charsRequired = dateTime.GetFormattedLength(); if (destination.Length < charsRequired) { charsWritten = 0; @@ -46,7 +51,9 @@ public static bool TryFormatO(this DateTime dateTime, Span destination, ou destination[19] = '.'; tick.WriteDigits(destination.Slice(20), 7); + var kind = dateTime.Kind; if (kind == DateTimeKind.Local) { + var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime); var offsetTotalMinutes = (int)(offset.Ticks / TimeSpan.TicksPerMinute); var sign = '+'; @@ -68,5 +75,6 @@ public static bool TryFormatO(this DateTime dateTime, Span destination, ou return true; } +#endif } } diff --git a/src/NReco.Logging.File/Extensions/IntExtensions.cs b/src/NReco.Logging.File/Extensions/IntExtensions.cs index 2e60e56..aa0f406 100644 --- a/src/NReco.Logging.File/Extensions/IntExtensions.cs +++ b/src/NReco.Logging.File/Extensions/IntExtensions.cs @@ -8,6 +8,7 @@ public static int GetFormattedLength(this int value) { return value == 0 ? 1 : (int)Math.Floor(Math.Log10(Math.Abs((double)value))) + (value > 0 ? 1 : 2); } +#if NETSTANDARD2_0 public static bool TryFormat(this int value, Span destination, out int charsWritten) { charsWritten = value.GetFormattedLength(); if (destination.Length < charsWritten) { @@ -35,5 +36,6 @@ internal static void WriteDigits(this uint value, Span destination, int co destination[0] = (char)('0' + value); } +#endif } } diff --git a/src/NReco.Logging.File/FileLogger.cs b/src/NReco.Logging.File/FileLogger.cs index 643da66..9e6445e 100644 --- a/src/NReco.Logging.File/FileLogger.cs +++ b/src/NReco.Logging.File/FileLogger.cs @@ -75,14 +75,15 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, } else { const int MaxStackAllocatedBufferLength = 256; - var logMessageLength = CalculateLogMessageLength(eventId, message); + DateTime timeStamp = LoggerPrv.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now; + var logMessageLength = CalculateLogMessageLength(timeStamp, eventId, message); char[] charBuffer = null; try { Span buffer = logMessageLength <= MaxStackAllocatedBufferLength ? stackalloc char[MaxStackAllocatedBufferLength] : (charBuffer = ArrayPool.Shared.Rent(logMessageLength)); - FormatLogEntryDefault(buffer, message, logLevel, eventId, exception); + FormatLogEntryDefault(buffer, timeStamp, message, logLevel, eventId, exception); } finally { if (charBuffer is not null) { @@ -92,13 +93,12 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, } } - private void FormatLogEntryDefault(Span buffer, string message, LogLevel logLevel, - EventId eventId, Exception exception) { + private void FormatLogEntryDefault(Span buffer, DateTime timeStamp, string message, + LogLevel logLevel, EventId eventId, Exception exception) { // default formatting logic using var logBuilder = new ValueStringBuilder(buffer); if (!string.IsNullOrEmpty(message)) { - DateTime timeStamp = LoggerPrv.UseUtcTimestamp ? DateTime.UtcNow : DateTime.Now; - timeStamp.TryFormatO(logBuilder.RemainingRawChars, out var charsWritten); + timeStamp.TryFormat(logBuilder.RemainingRawChars, out var charsWritten); logBuilder.AppendSpan(charsWritten); logBuilder.Append('\t'); logBuilder.Append(GetShortLogLevel(logLevel)); @@ -124,8 +124,8 @@ private void FormatLogEntryDefault(Span buffer, string message, LogLevel l LoggerPrv.WriteEntry(logBuilder.ToString()); } - private int CalculateLogMessageLength(EventId eventId, string message) { - return 33 /* timeStamp.TryFormatO */ + private int CalculateLogMessageLength(DateTime timeStamp, EventId eventId, string message) { + return timeStamp.GetFormattedLength() + 1 /* '\t' */ + 4 /* GetShortLogLevel */ + 2 /* "\t[" */