/serilog-sinks-notepad

A Serilog sink that writes log events to Notepad as text or JSON

Primary LanguageC#Apache License 2.0Apache-2.0

README.md
Serilog.Sinks.Notepad

Serilog.Sinks.Notepad

A Serilog sink that writes log events to Notepad (Yes, you've read it right!). Simply open Notepad and immediately start receiving logs from your application, without even touching the filesystem.

NuGet Version Stack Overflow

Screenshot of Serilog.Sinks.Notepad in action

Serilog.Sinks.Notepad writes messages to the most recent notepad.exe started on current user's session, by default. This behavior can be changed in the sink configuration.

The default output is plain text; JSON formatting can be plugged in using a package such as Serilog.Formatting.Compact.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Getting started 🚀

Install the Serilog.Sinks.Notepad package from NuGet:

Install-Package Serilog.Sinks.Notepad

To configure the sink in C# code, call WriteTo.Notepad() during logger configuration:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Notepad()
    .CreateLogger();

Log.Information("Hello, world!");

Log.CloseAndFlush();

Open Notepad, and you should see the logs appear in that Notepad window you've just opened. By default, Serilog.Sinks.Notepad writes messages to the most recent notepad.exe started by the user. This behavior can be changed in the sink configuration.

Background

I created this sink just for fun, after reading this comment on Reddit:

Screenshot of Serilog.Sinks.Notepad in action

I thought it was 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.

Configuration

Output templates

The format of events written to Notepad can be modified using the outputTemplate configuration parameter:

    .WriteTo.Notepad(
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")

The default template, shown in the example above, uses built-in properties like Timestamp and Level. Properties from events, including those attached using enrichers, can also appear in the output template.

JSON output

The sink can write JSON output instead of plain text. CompactJsonFormatter or RenderedCompactJsonFormatter from Serilog.Formatting.Compact is recommended:

Install-Package Serilog.Formatting.Compact

Pass a formatter to the Notepad() configuration method:

    .WriteTo.Notepad(new RenderedCompactJsonFormatter())

XML <appSettings> configuration

To use the Notepad sink with the Serilog.Settings.AppSettings package, first install that package if you haven't already done so:

Install-Package Serilog.Settings.AppSettings

Instead of configuring the logger in code, call ReadFrom.AppSettings():

Log.Logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .CreateLogger();

In your application's App.config or Web.config file, specify the Notepad sink assembly under the <appSettings> node:

<configuration>
  <appSettings>
    <add key="serilog:using:Notepad" value="Serilog.Sinks.Notepad" />
    <add key="serilog:write-to:Notepad" />

To configure the Notepad sink and include the SourceContext in the output, change your App.config/Web.config to:

<configuration>
  <appSettings>
    <add key="serilog:using:Notepad" value="Serilog.Sinks.Notepad" />
    <add key="serilog:write-to:Notepad.outputTemplate" value="[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} &lt;s:{SourceContext}&gt;{NewLine}{Exception}" />

JSON appsettings.json configuration

To use the Notepad sink with Microsoft.Extensions.Configuration, for example with ASP.NET Core or .NET Core, use the Serilog.Settings.Configuration package. First install that package if you have not already done so:

Install-Package Serilog.Settings.Configuration

Instead of configuring the sink directly in code, call ReadFrom.Configuration():

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

In your appsettings.json file, under the Serilog node, :

{
  "Serilog": {
    "WriteTo": [{"Name": "Notepad"}]
  }
}

To configure the Notepad sink and include the SourceContext in the output, change your appsettings.json to:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Notepad",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
        }
      }
    ]
  }
}

Debugging and Diagnostics

When Serilog is not behaving as you expect, this may be caused by an internal exception or configuration issue. Serilog writes simple diagnostic messages to SelfLog, which can be forwarded to Notepad, using the NotepadWindow static class:

Serilog.Debugging.SelfLog.Enable(s => NotepadWindow.WriteLine($"Internal Error with Serilog: {s}"));

The above will attempt to write Serilog's diagnostic messages to the most recent Notepad process open in the user's session.

Release History

Click on the Releases tab on GitHub.


Copyright © 2020-2023 C. Augusto Proiete & Contributors - Provided under the Apache License, Version 2.0.