|
| 1 | +// Copyright 2020 C. Augusto Proiete & Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using Serilog.Sinks.Notepad.Interop; |
| 17 | + |
| 18 | +namespace Serilog.Sinks.Notepad |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Writes messages directly to the most recent `notepad.exe` process |
| 22 | + /// </summary> |
| 23 | + /// <example> |
| 24 | + /// Capture internal errors from Serilog sinks via <see cref="Serilog.Debugging.SelfLog"/>: |
| 25 | + /// <code> |
| 26 | + /// Serilog.Debugging.SelfLog.Enable(s => NotepadWindow.WriteLine($"Internal Error with Serilog: {s}")); |
| 27 | + /// </code> |
| 28 | + /// </example> |
| 29 | + public static class NotepadWindow |
| 30 | + { |
| 31 | + private static readonly Lazy<NotepadTextWriter> _notepadWindow = |
| 32 | + new Lazy<NotepadTextWriter>(() => new NotepadTextWriter()); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Writes a string to the most recent Notepad window open. |
| 36 | + /// If the given string is null, nothing is written to the text stream. |
| 37 | + /// </summary> |
| 38 | + /// <param name="value">The string to be written</param> |
| 39 | + public static void Write(string value) |
| 40 | + { |
| 41 | + var notepadWindow = _notepadWindow.Value; |
| 42 | + notepadWindow.Write(value); |
| 43 | + notepadWindow.Flush(); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Writes a line terminator to the most recent Notepad window open. |
| 48 | + /// </summary> |
| 49 | + public static void WriteLine() |
| 50 | + { |
| 51 | + var notepadWindow = _notepadWindow.Value; |
| 52 | + notepadWindow.WriteLine(); |
| 53 | + notepadWindow.Flush(); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Writes a line terminator to the most recent Notepad window open. |
| 58 | + /// </summary> |
| 59 | + /// <param name="value">The string to be written</param> |
| 60 | + public static void WriteLine(string value) |
| 61 | + { |
| 62 | + var notepadWindow = _notepadWindow.Value; |
| 63 | + notepadWindow.WriteLine(value); |
| 64 | + notepadWindow.Flush(); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments