|
| 1 | +| README.md | |
| 2 | +|:---| |
| 3 | + |
| 4 | +<div align="center"> |
| 5 | + |
| 6 | +<img src="asset/serilog-sink-nuget.png" alt="Serilog.Sinks.RichTextBox.Wpf" width="100" /> |
| 7 | + |
| 8 | +</div> |
| 9 | + |
| 10 | +<h1 align="center">Serilog.Sinks.RichTextBox.Wpf</h1> |
| 11 | +<div align="center"> |
| 12 | + |
| 13 | +A [Serilog](https://serilog.net) sink that writes log events to any WPF [RichTextBox](https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/richtextbox-overview?view=netframeworkdesktop-4.8) control with coloring and custom theme support. |
| 14 | + |
| 15 | +[](https://www.nuget.org/packages/Serilog.Sinks.RichTextBox.Wpf) [](https://dotnet.microsoft.com/download) [](https://dotnet.microsoft.com/download) [](https://dotnet.microsoft.com/download) [](http://stackoverflow.com/questions/tagged/serilog) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +</div> |
| 20 | + |
| 21 | +## Give a Star! :star: |
| 22 | + |
| 23 | +If you like or are using this project please give it a star. Thanks! |
| 24 | + |
| 25 | +## Getting started :rocket: |
| 26 | + |
| 27 | +Install the [Serilog.Sinks.RichTextBox.Wpf](https://www.nuget.org/packages/Serilog.Sinks.RichTextBox.Wpf) package from NuGet: |
| 28 | + |
| 29 | +```powershell |
| 30 | +Install-Package Serilog.Sinks.RichTextBox.Wpf |
| 31 | +``` |
| 32 | + |
| 33 | +Declare your [RichTextBox](https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/richtextbox-overview?view=netframeworkdesktop-4.8) control and give it a name that you can reference it from the code-behind. e.g.: |
| 34 | + |
| 35 | +```xml |
| 36 | +<Window (...)> |
| 37 | + <RichTextBox |
| 38 | + x:Name="MyRichTextBox" |
| 39 | + Background="Black" |
| 40 | + Foreground="LightGray" |
| 41 | + FontFamily="Cascadia Mono, Consolas, Courier New, monospace" |
| 42 | + VerticalScrollBarVisibility="Auto" |
| 43 | + /> |
| 44 | +</Window> |
| 45 | +``` |
| 46 | + |
| 47 | +Then enable the sink using `WriteTo.RichTextBox()`: |
| 48 | + |
| 49 | +```csharp |
| 50 | +Log.Logger = new LoggerConfiguration() |
| 51 | + .WriteTo.RichTextBox(MyRichTextBox) |
| 52 | + .CreateLogger(); |
| 53 | + |
| 54 | +Log.Information("Hello, world!"); |
| 55 | +``` |
| 56 | + |
| 57 | +Log events will be written to the `RichTextBox` control: |
| 58 | + |
| 59 | +``` |
| 60 | +[11:54:36 INF] Hello, world! |
| 61 | +``` |
| 62 | + |
| 63 | +### Themes |
| 64 | + |
| 65 | +The sink will colorize output by default: |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +Themes can be specified when configuring the sink: |
| 70 | + |
| 71 | +```csharp |
| 72 | + .WriteTo.RichTextBox(MyRichTextBox, theme: RichTextBoxConsoleTheme.Grayscale) |
| 73 | +``` |
| 74 | + |
| 75 | +The following built-in themes are available at this time: |
| 76 | + |
| 77 | +| Theme | Description |
| 78 | +| ----------------------------------- | --------------------------------------------------------------------------------------------------------------------- | |
| 79 | +| `RichTextBoxTheme.None` | No styling. Uses default styles applied to the `RichTextBox` control | |
| 80 | +| `RichTextBoxConsoleTheme.Literate` | Styled to replicate the default theme of _Serilog.Sinks.Console_; **This is the default when no theme is specified** | |
| 81 | +| `RichTextBoxConsoleTheme.Grayscale` | A theme using only shades of gray, white, and black | |
| 82 | +| `RichTextBoxConsoleTheme.Colored` | A theme based on the original `Serilog.Sinks.ColoredConsole` sink | |
| 83 | + |
| 84 | + Adding a new theme is straightforward; examples can be found in the [`RichTextBoxConsoleThemes`](src/Serilog.Sinks.RichTextBox.Wpf/Sinks/RichTextBox/Themes/RichTextBoxConsoleThemes.cs) class. |
| 85 | + |
| 86 | +### Output templates |
| 87 | + |
| 88 | +The format of events to the RichTextBox can be modified using the `outputTemplate` configuration parameter: |
| 89 | + |
| 90 | +```csharp |
| 91 | + .WriteTo.RichTextBox(MyRichTextBox, |
| 92 | + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") |
| 93 | +``` |
| 94 | + |
| 95 | +The default template, shown in the example above, uses built-in properties like `Timestamp` and `Level`. Properties from events, including those attached using [enrichers](https://github.com/serilog/serilog/wiki/Enrichment), can also appear in the output template. |
| 96 | + |
| 97 | +## Release History |
| 98 | + |
| 99 | +Click on the [Releases](https://github.com/augustoproiete/serilog-sinks-richtextbox/releases) tab on GitHub. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +_Copyright © 2021 C. Augusto Proiete & Contributors - Provided under the [Apache License, Version 2.0](LICENSE)._ |
0 commit comments