From 6b871b24a60357db2502110393d854455d80e257 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 12:38:30 +1000 Subject: [PATCH 1/6] Add shields badge for nuget --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a82768..8241474 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # Notepad.Extensions.Logging +![Nuget](https://img.shields.io/nuget/v/Notepad.Extensions.Logging) + This is a library for .NET / .NET Core to log your program's output to a handy Notepad window! Inspired by [this tweet](https://twitter.com/steveklabnik/status/1263190719721766918): -![](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig) \ No newline at end of file +![](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig) From 9fb4b447149838940acd33235b2e61c177cfb774 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 13:22:55 +1000 Subject: [PATCH 2/6] make nuget button link to package --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 8241474..2242c9c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # Notepad.Extensions.Logging -![Nuget](https://img.shields.io/nuget/v/Notepad.Extensions.Logging) +[![NuGet](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig)](https://www.nuget.org/packages/Notepad.Extensions.Logging/) This is a library for .NET / .NET Core to log your program's output to a handy Notepad window! Inspired by [this tweet](https://twitter.com/steveklabnik/status/1263190719721766918): -![](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig) From 4ee15ce1c84bb0014a4d97b1563a892851d6a71c Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 13:24:10 +1000 Subject: [PATCH 3/6] whoopie --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2242c9c..c51699d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Notepad.Extensions.Logging -[![NuGet](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig)](https://www.nuget.org/packages/Notepad.Extensions.Logging/) +[![NuGet](https://img.shields.io/nuget/v/Notepad.Extensions.Logging)](https://www.nuget.org/packages/Notepad.Extensions.Logging/) This is a library for .NET / .NET Core to log your program's output to a handy Notepad window! Inspired by [this tweet](https://twitter.com/steveklabnik/status/1263190719721766918): +![](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig) From f293b6f25653dd0ddbfecee4c4a7b6aea8ee8b2e Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 13:51:23 +1000 Subject: [PATCH 4/6] Add instructions --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index c51699d..9855b4d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,29 @@ This is a library for .NET / .NET Core to log your program's output to a handy Notepad window! +## Installation + +To use: + +1. Add a package reference to Notepad.Extensions.Logging +2. In `Startup.cs`, call `AddNotepad()`, like so: + +```csharp +// This method gets called by the runtime. Use this method to add services to the container. +public void ConfigureServices(IServiceCollection services) +{ + services.AddControllersWithViews(); + + // ... + + services.AddLogging(lb => lb.AddNotepad()); // This is where the magic happens +} +``` + +3. Open a new Notepad window + +## Source Material + Inspired by [this tweet](https://twitter.com/steveklabnik/status/1263190719721766918): ![](https://pbs.twimg.com/media/EYfBQ5cXsAEcu2e?format=jpg&name=orig) From 6c43dae404aec4ebfd47c8b58b455423ffd3be75 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 13:51:52 +1000 Subject: [PATCH 5/6] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9855b4d..3540414 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is a library for .NET / .NET Core to log your program's output to a handy N To use: -1. Add a package reference to Notepad.Extensions.Logging +1. Add a package reference to `Notepad.Extensions.Logging`. 2. In `Startup.cs`, call `AddNotepad()`, like so: ```csharp @@ -23,7 +23,8 @@ public void ConfigureServices(IServiceCollection services) } ``` -3. Open a new Notepad window +3. Open a new Notepad window. +4. Run your application. ## Source Material From 2d7131997493a3f9a05876364ec80cd663ca37c5 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 17:38:01 +1000 Subject: [PATCH 6/6] Also search for Notepad windows with changes. Fixes #3. --- Notepad.Extensions.Logging/NotepadLogger.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index f365058..2636b13 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -84,10 +84,29 @@ namespace Microsoft.Extensions.Logging static void WriteToNotepad(string message) { - IntPtr hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); + IntPtr hwnd = FindNotepadWindow(); IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null); NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message); } + + static IntPtr FindNotepadWindow() + { + IntPtr hwnd; + + hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); + if (hwnd != IntPtr.Zero) + { + return hwnd; + } + + hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad"); + if (hwnd != IntPtr.Zero) + { + return hwnd; + } + + return IntPtr.Zero; + } } static class NativeMethods