Struct LogInterpolatedStringHandler
Core interpolated string handler that defers string construction until the log level is confirmed enabled, avoiding unnecessary string allocations when logging is disabled.
public ref struct LogInterpolatedStringHandler
- Inherited Members
Remarks
On net8.0 and later this delegates to System.Runtime.CompilerServices.DefaultInterpolatedStringHandler,
which uses pooled character buffers and ISpanFormattable to avoid boxing of value-type arguments.
On older target frameworks a System.Text.StringBuilder is used. All formatting is performed using
InvariantCulture so log output is culture-independent.
Constructors
LogInterpolatedStringHandler(int, int, ILogger, LogEventLevel, out bool)
Initializes a new instance of the LogInterpolatedStringHandler struct.
public LogInterpolatedStringHandler(int literalLength, int formattedCount, ILogger logger, LogEventLevel level, out bool isEnabled)
Parameters
literalLengthintThe number of literal characters in the interpolated string.
formattedCountintThe number of interpolation holes in the interpolated string.
loggerILoggerThe logger to check for enabled status.
levelLogEventLevelThe log event level to check.
isEnabledboolOn return, indicates whether logging is enabled for this level.
Methods
AppendFormatted<T>(T)
Appends a formatted value to the handler.
public void AppendFormatted<T>(T value)
Parameters
valueTThe value to format and append.
Type Parameters
TThe type of the value to format.
AppendFormatted<T>(T, int)
Appends a formatted value with alignment to the handler.
public void AppendFormatted<T>(T value, int alignment)
Parameters
valueTThe value to format and append.
alignmentintThe alignment for the formatted value.
Type Parameters
TThe type of the value to format.
AppendFormatted<T>(T, int, string?)
Appends a formatted value with alignment and a format string to the handler.
public void AppendFormatted<T>(T value, int alignment, string? format)
Parameters
valueTThe value to format and append.
alignmentintThe alignment for the formatted value.
formatstringThe format string.
Type Parameters
TThe type of the value to format.
AppendFormatted<T>(T, string?)
Appends a formatted value with a format string to the handler.
public void AppendFormatted<T>(T value, string? format)
Parameters
valueTThe value to format and append.
formatstringThe format string.
Type Parameters
TThe type of the value to format.
AppendLiteral(string)
Appends a literal string to the handler.
public void AppendLiteral(string s)
Parameters
sstringThe literal string to append.