Skip to content

A Serilog sink that writes log events to WinUI 3 collection controls like the ItemsRepeater.

License

Notifications You must be signed in to change notification settings

AndrewKeepCoding/serilog-sinks-winui3

Repository files navigation

📃 Serilog.Sinks.WinUi3

A Serilog sink that writes log events to WinUI 3 collection controls like ItemsRepeater.

Serilog.Sinks.WinUi3 ItemsRepeater Sample Screenshot

🙌 Quick Start

Install the Serilog.Sinks.WinUi3 package.

Install-Package Serilog.Sinks.WinUi3 -Prerelease

Install the Serilog.Sinks.WinUi3.LogViewModels package. (Optional: You can implement your own viewmodels for your logs.)

Install-Package Serilog.Sinks.WinUi3.LogViewModels -Prerelease

Also install these Serilog packages.

Install-Package Serilog
Install-Package Serilog.Expressions

Like the example below, create a LogBroker (naming is hard...) and a LogViewModelBuilder, register your LogBroker with WriteTo.WinUi3Control() and create the logger. Take a look at the sample app.

// Create your custom LogBroker.
_logBroker = new ItemsRepeaterLogBroker(
    LogSinkItemsRepeater,
    LogSinkScrollViewer,
    new EmojiLogViewModelBuilder(defaultForeground: Colors.White)
        // Timestamp format
        .SetTimestampFormat(new ExpressionTemplate("[{@t:yyyy-MM-dd HH:mm:ss.fff}]"))
        // Level format and colors
        .SetLevelsFormat(new ExpressionTemplate("{@l:u3}"))
        .SetLevelForeground(LogEventLevel.Verbose, Colors.Gray)
        .SetLevelForeground(LogEventLevel.Debug, Colors.Gray)
        .SetLevelForeground(LogEventLevel.Warning, Colors.Yellow)
        .SetLevelForeground(LogEventLevel.Error, Colors.Red)
        .SetLevelForeground(LogEventLevel.Fatal, Colors.HotPink)
        // Message format and colors
        .SetMessageFormat(new ExpressionTemplate("{@m}"))
        .SetMessageForeground(LogEventLevel.Verbose, Colors.Gray)
        .SetMessageForeground(LogEventLevel.Debug, Colors.Gray)
        .SetMessageForeground(LogEventLevel.Warning, Colors.Yellow)
        .SetMessageForeground(LogEventLevel.Error, Colors.Red)
        .SetMessageForeground(LogEventLevel.Fatal, Colors.HotPink)
        // Exception format and color
        .SetExceptionFormat(new ExpressionTemplate("{@x}"))
        .SetExceptionForeground(Colors.HotPink)
        );

// Register your LogBroker with WriteTo.WinUi3Control() and create the logger.
Log.Logger = new LoggerConfiguration()
    .WriteTo.WinUi3Control(_logBroker)
    .CreateLogger();

⚠️ Warning

Avoid using the ItemsRepeater with variable height items. There is a known bug on the ItemsRepeater and the Microsoft Team is still working on it.

🎬 YouTube

About

A Serilog sink that writes log events to WinUI 3 collection controls like the ItemsRepeater.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages