Giter VIP home page Giter VIP logo

serilog-sinks-notepad's Introduction

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.

serilog-sinks-notepad's People

Contributors

augustoproiete avatar bash avatar dependabot[bot] avatar gauravagrwal avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

serilog-sinks-notepad's Issues

Finding a renamed notepad process

If I provide a copy of the Notepad.exe file with a different name (say MyNotepadMonitor.exe), I can provide a custom Func<Process> that searches for this process name in the call to LoggerConfiguration().WriteTo.Notepad().

But what if I don't use this, but rely entirely on the configuration in appsettings.json? Is it possible to provide a function there? Or would it be an option to add a parameter that specifies the name of the executable to use instead of Notepad.exe?

Access error to Notepad window

I'm using the SeriLog.Sinks.Notepad latest version, but I cannot get it to work.

Usign the SelfLog feature to Console, I get this error:

2022-03-14T08:47:14.7322195Z Unable to access a Notepad Editor on process Notepad (28836)

So it does find the process, but it is not able to access it.

Add support for writing to new Notepad on Windows 11

I have experienced that the current implementation of finding the notepadEditorHandle on Winodows 11 with the RichEditD2DTP class does not find the actual editor handle.

But by enumerating the child windows from the process MainWindowHandle, I do find it.

I'll make a pull request for this issue with my changes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.