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.
And guess what? Besides GitHub repository for Notepad.Extensions.Logging there’s also NuGet package available!
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!
Pingback:Dew Drop – May 22, 2020 (#3203) | Morning Dew
Pingback:The Morning Brew - Chris Alcock » The Morning Brew #3000
This actually has some cool possibilities. What I have always wanted is to have different types of output go to different output windows. Think Debug.Writeline(windowN,”message”)
That way all my SQL related stuff can go in 1, maybe stack trace stuff in another. That way I don’t have to sift through it too much if it’s already separated. Perhaps with this I can get something like that. Funny post, but sometimes the coolest things come stuff like this!
This is so cool! I might use this in production, to be honest.
I found many interesting implementations:
Rust – https://github.com/sgrif/notepad_logger
C# / Serilog – https://github.com/augustoproiete/serilog-sinks-notepad
Zig – https://github.com/Sobeston/notepad-stream/
Frida – https://github.com/FuzzySecurity/Fermion/blob/master/Examples/NotepadLogger.js