Logging to Notepad window from ASP.NET Core

Something funny to end this week. When checking my Twitter feeds I found a kinky tweet about logging .NET Core messages to Notepad window. Yes, you heard right – .NET Core logger for Notepad. It’s not real, you want to say, but it is. And here’s how it works :)

The story of Notepad.Extensions.Logging

When checking Twitter I found a funny tweet by @yaakov_h where he announced NuGet package for .NET Core logger that targets Notepad. He got inspiration for this from tweet by @steveklabnik.

notepad.extensions.logging-tweet

And guess what? Besides GitHub repository for Notepad.Extensions.Logging there’s also NuGet package available!

notepad.extensions.logging-nuget

It’s a real deal – version 1.0.0, you see? :)

Notepad logger in action

Let’s get hands dirty with this little piece of art. Just create new ASP.NET Core web application and add Notepad.Extensions.Logging NuGet package. After this modify ConfigureServices() method in Startup like in following code fragment.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    services.AddLogging(lb => lb.AddNotepad());
}

To try things out I made my HomeController log some messages.

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("Home.Index opened");
        return View();
    }

    public IActionResult Privacy()
    {
        _logger.LogInformation("Home.Privacy opened");
        return View();
    }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        base.OnActionExecuted(context);
        _logger.LogInformation("Action executed");
    }
}

I didn’t made it work with NuGet package for some reason. I had to clone Notepad.Extensions.Logging repository to my machine and add it as project reference to web application. Then after few runs messages started appear to Notepad. Here’s the short video I made of this experiment.

So, it works after all!

Should I really use it?

Although this solution is funny it may still come handy when debugging something and there are things to dig out from logs frequently. It’s possible to disable all other log targets for debugging and skip logging related disk I/O as communication with Notepad goes directly through memory.

After using Notepad.Logging.Extensions few hours I actually started to like it. It’s weird but feels somehow convenient. Is Notepad the best choice for text editor? Who knows. But it’s not very hard to extend Notepad.Logging.Extensions to use some more advanced text editor.

Anyway, happy weekend and cheers!

Serverless360 Logo

A portal focused on Operations and Support for Microsoft Azure Serverless services

FREE TRIAL

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    4 thoughts on “Logging to Notepad window from ASP.NET Core

    Leave a Reply

    Your email address will not be published. Required fields are marked *