|
| 1 | +# Serilog.Sinks.Notepad [](https://www.nuget.org/packages/Serilog.Sinks.Notepad) [](http://stackoverflow.com/questions/tagged/serilog) |
| 2 | + |
| 3 | +A [Serilog](https://serilog.net) sink that writes log events to Notepad (_Yes, you've read it right!_). The default output is plain text; JSON formatting can be plugged in using a package such as [_Serilog.Formatting.Compact_](https://github.com/serilog/serilog-formatting-compact). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Give a Star! :star: |
| 8 | + |
| 9 | +If you like or are using this project please give it a star. Thanks! |
| 10 | + |
| 11 | +## Getting started |
| 12 | + |
| 13 | +Install the [Serilog.Sinks.Notepad](https://www.nuget.org/packages/Serilog.Sinks.Notepad) package from NuGet: |
| 14 | + |
| 15 | +```powershell |
| 16 | +Install-Package Serilog.Sinks.Notepad |
| 17 | +``` |
| 18 | + |
| 19 | +To configure the sink in C# code, call `WriteTo.Notepad()` during logger configuration: |
| 20 | + |
| 21 | +```csharp |
| 22 | +Log.Logger = new LoggerConfiguration() |
| 23 | + .WriteTo.Notepad() |
| 24 | + .CreateLogger(); |
| 25 | + |
| 26 | +Log.Information("Hello, world!"); |
| 27 | +``` |
| 28 | + |
| 29 | +Open Notepad, and you should see the logs appear in that Notepad window. By default, `Serilog.Sinks.Notepad` writes messages to the most recent `notepad.exe` started on the machine. This behavior can be changed in the sink configuration. |
| 30 | + |
| 31 | +## Background |
| 32 | + |
| 33 | +I created this sink just for fun, after reading [this comment on Reddit](https://www.reddit.com/r/programming/comments/gnazif/ray_tracing_in_notepadexe_at_30_fps/fr8uy2l/): |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +I thought that's a clever idea to be able to simply open a Notepad instance and immediately start receiving logs from your application, and I can imagine this actually being useful for troubleshooting issues with applications. |
| 38 | + |
| 39 | +## Configuration |
| 40 | + |
| 41 | +### Output templates |
| 42 | + |
| 43 | +The format of events written to Notepad can be modified using the `outputTemplate` configuration parameter: |
| 44 | + |
| 45 | +```csharp |
| 46 | + .WriteTo.Notepad( |
| 47 | + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") |
| 48 | +``` |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +### JSON output |
| 53 | + |
| 54 | +The sink can write JSON output instead of plain text. `CompactJsonFormatter` or `RenderedCompactJsonFormatter` from [Serilog.Formatting.Compact](https://github.com/serilog/serilog-formatting-compact) is recommended: |
| 55 | + |
| 56 | +```powershell |
| 57 | +Install-Package Serilog.Formatting.Compact |
| 58 | +``` |
| 59 | + |
| 60 | +Pass a formatter to the `Notepad()` configuration method: |
| 61 | + |
| 62 | +```csharp |
| 63 | + .WriteTo.Notepad(new RenderedCompactJsonFormatter()) |
| 64 | +``` |
| 65 | + |
| 66 | +Output theming is not available when custom formatters are used. |
| 67 | + |
| 68 | +### XML `<appSettings>` configuration |
| 69 | + |
| 70 | +To use the Notepad sink with the [Serilog.Settings.AppSettings](https://github.com/serilog/serilog-settings-appsettings) package, first install that package if you haven't already done so: |
| 71 | + |
| 72 | +```powershell |
| 73 | +Install-Package Serilog.Settings.AppSettings |
| 74 | +``` |
| 75 | + |
| 76 | +Instead of configuring the logger in code, call `ReadFrom.AppSettings()`: |
| 77 | + |
| 78 | +```csharp |
| 79 | +Log.Logger = new LoggerConfiguration() |
| 80 | + .ReadFrom.AppSettings() |
| 81 | + .CreateLogger(); |
| 82 | +``` |
| 83 | + |
| 84 | +In your application's `App.config` or `Web.config` file, specify the Notepad sink assembly under the `<appSettings>` node: |
| 85 | + |
| 86 | +```xml |
| 87 | +<configuration> |
| 88 | + <appSettings> |
| 89 | + <add key="serilog:using:Notepad" value="Serilog.Sinks.Notepad" /> |
| 90 | + <add key="serilog:write-to:Notepad" /> |
| 91 | +``` |
| 92 | + |
| 93 | +To configure the Notepad sink and include the `SourceContext` in the output, change your `App.config`/`Web.config` to: |
| 94 | +```xml |
| 95 | +<configuration> |
| 96 | + <appSettings> |
| 97 | + <add key="serilog:using:Notepad" value="Serilog.Sinks.Notepad" /> |
| 98 | + <add key="serilog:write-to:Notepad.outputTemplate" value="[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}" /> |
| 99 | +``` |
| 100 | + |
| 101 | +### JSON `appsettings.json` configuration |
| 102 | + |
| 103 | +To use the Notepad sink with _Microsoft.Extensions.Configuration_, for example with ASP.NET Core or .NET Core, use the [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration) package. First install that package if you have not already done so: |
| 104 | + |
| 105 | +```powershell |
| 106 | +Install-Package Serilog.Settings.Configuration |
| 107 | +``` |
| 108 | + |
| 109 | +Instead of configuring the sink directly in code, call `ReadFrom.Configuration()`: |
| 110 | + |
| 111 | +```csharp |
| 112 | +var configuration = new ConfigurationBuilder() |
| 113 | + .AddJsonFile("appsettings.json") |
| 114 | + .Build(); |
| 115 | + |
| 116 | +Log.Logger = new LoggerConfiguration() |
| 117 | + .ReadFrom.Configuration(configuration) |
| 118 | + .CreateLogger(); |
| 119 | +``` |
| 120 | + |
| 121 | +In your `appsettings.json` file, under the `Serilog` node, : |
| 122 | +```json |
| 123 | +{ |
| 124 | + "Serilog": { |
| 125 | + "WriteTo": [{"Name": "Notepad"}] |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +To configure the Notepad sink and include the `SourceContext` in the output, change your `appsettings.json` to: |
| 131 | +```json |
| 132 | +{ |
| 133 | + "Serilog": { |
| 134 | + "WriteTo": [ |
| 135 | + { |
| 136 | + "Name": "Notepad", |
| 137 | + "Args": { |
| 138 | + "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}" |
| 139 | + } |
| 140 | + } |
| 141 | + ] |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Release History |
| 147 | + |
| 148 | +Click on the [Releases](https://github.com/augustoproiete/serilog-sinks-notepad/releases) tab on GitHub. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +_Copyright © 2020 C. Augusto Proiete & Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._ |
0 commit comments