Skip to content

Make OutputTemplateRenderer public for custom formatters #169

@na-pel

Description

@na-pel

Ask: expose the sink’s internal OutputTemplateRenderer (or an equivalent helper) so developers can reuse its theme + template logic instead of re‑implementing it.

Why

  • Console sink already handles colour, token expansion and alignment.
  • To add small post‑processing (wrap long lines, indent continuation lines, highlight certain keywords, mask secrets, etc.) we must currently fork the whole sink because the renderer is internal.

Proposed change

-internal class OutputTemplateRenderer : ITextFormatter
+public sealed class OutputTemplateRenderer : ITextFormatter

No behaviour change - just surface the type for reuse.

Minimal usage

sealed class CustomConsoleFormatter : ITextFormatter
{
    // Use OutputTemplateRenderer as the backbone – keeps theme + template intact
    private readonly ITextFormatter inner = new OutputTemplateRenderer(
        theme: Console.IsOutputRedirected || Console.IsErrorRedirected
            ? ConsoleTheme.None
            : SystemConsoleTheme.Literate,
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");

    public void Format(LogEvent logEvent, TextWriter output)
    {
        using var sw = new StringWriter();
        inner.Format(logEvent, sw);
        var sb = sw.GetStringBuilder();

        // Apply any custom tweaks – wrap, indent, highlight, etc.
        sb.Replace("foo", "bar");
        output.Write(sb);
    }
}

Impact

  • Minor SemVer bump; default output untouched.
  • Enables community extensions without duplicating sink code.

Related: Issue #68.

Thanks! 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions