Skip to content

Commit 756b269

Browse files
committed
In some situations, a notepad window opened after logging start would not receive log messages.
1 parent 7647b4b commit 756b269

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

sample/ConsoleDemo/Program.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
//
1515
#endregion
1616

17+
using Serilog;
1718
using System;
1819
using System.Threading;
19-
using Serilog;
2020

2121
namespace ConsoleDemo
2222
{
@@ -33,15 +33,23 @@ private static void Main(string[] args)
3333

3434
try
3535
{
36-
Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
37-
Console.ReadLine();
36+
//Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
37+
//Console.ReadLine();
3838

3939
Console.WriteLine("Writing messages to the most recent Notepad you opened...");
4040

4141
Log.Debug("Getting started");
4242

43-
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"),
44-
Thread.CurrentThread.ManagedThreadId);
43+
var startTime = DateTime.Now;
44+
45+
while (DateTime.Now - startTime < TimeSpan.FromMinutes(1))
46+
{
47+
48+
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"),
49+
Thread.CurrentThread.ManagedThreadId);
50+
51+
Thread.Sleep(1000);
52+
}
4553

4654
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
4755

src/Serilog.Sinks.Notepad/Sinks/Notepad/Interop/NotepadTextWriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public override void Flush()
5757
// No instances of Notepad found... Nothing to do
5858
return;
5959
}
60+
}
6061

62+
if (_currentNotepadEditorHandle == IntPtr.Zero)
63+
{
6164
var notepadWindowHandle = currentNotepadProcess.MainWindowHandle;
6265

6366
var notepadEditorHandle = FindNotepadEditorHandle(notepadWindowHandle);

0 commit comments

Comments
 (0)