Skip to content

Commit ab9ac54

Browse files
committed
Sometimes, the editor handle seems not correct. So if the text length does not change by sending text to it, it is cleared in order to reget it.
1 parent 0084257 commit ab9ac54

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,16 @@ public override void Flush()
8585
// Write the log message to Notepad
8686
User32.SendMessage(_currentNotepadEditorHandle, User32.EM_REPLACESEL, (IntPtr)1, message);
8787

88-
buffer.Clear();
88+
// Get how many characters are in the Notepad editor after putting in new text
89+
var textLengthAfter = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
90+
91+
// If no change in textLength, reset editor handle to try to find it again.
92+
if (textLengthAfter == textLength)
93+
_currentNotepadEditorHandle = IntPtr.Zero;
94+
95+
// Otherwise, we clear the buffer
96+
else
97+
buffer.Clear();
8998
}
9099

91100
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)