| | 1 | | using Serilog.Core; |
| | 2 | | using Serilog.Events; |
| | 3 | | using Serilog.Formatting; |
| | 4 | | using Serilog.Formatting.Display; |
| | 5 | |
|
| | 6 | | namespace Kestrun.Logging.Sinks; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// A Serilog sink that formats log events and invokes a callback for PowerShell integration. |
| | 10 | | /// </summary> |
| | 11 | | /// <remarks> |
| | 12 | | /// Initializes a new instance of the <see cref="PowerShellSink"/> class. |
| | 13 | | /// </remarks> |
| | 14 | | /// <param name="callback">The callback action invoked with the log event and its formatted message.</param> |
| | 15 | | /// <param name="outputTemplate">The output template used for formatting log messages.</param> |
| | 16 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="callback"/> is null.</exception> |
| | 17 | | /// <remarks> |
| | 18 | | /// This constructor initializes the text formatter and callback action for the sink. |
| | 19 | | /// </remarks> |
| 8 | 20 | | public class PowerShellSink(Action<LogEvent, string> callback, string outputTemplate = PowerShellSink.DEFAULT_OUTPUT_TEM |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// The default output template used for formatting log messages. |
| | 24 | | /// </summary> |
| | 25 | | public const string DEFAULT_OUTPUT_TEMPLATE = "{Message:lj}"; |
| | 26 | |
|
| | 27 | | #if NET9_0_OR_GREATER |
| 1 | 28 | | private static readonly Lock _syncRoot = new(); |
| | 29 | | #else |
| | 30 | | private static readonly object _syncRoot = new(); |
| | 31 | | #endif |
| | 32 | | /// <summary> |
| | 33 | | /// Gets or sets the text formatter used to format log events. |
| | 34 | | /// </summary> |
| 17 | 35 | | public ITextFormatter TextFormatter { get; set; } = new MessageTemplateTextFormatter(string.IsNullOrWhiteSpace(outpu |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets or sets the callback action that is invoked with the log event and its formatted message. |
| | 39 | | /// </summary> |
| 17 | 40 | | public Action<LogEvent, string> Callback { get; set; } = callback ?? throw new ArgumentNullException(nameof(callback |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Emits a log event by formatting it and invoking the callback action. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="logEvent">The log event to emit.</param> |
| | 46 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="logEvent"/> is null.</exception> |
| | 47 | | /// <remarks> |
| | 48 | | /// This method formats the log event using the specified text formatter and invokes the callback with the formatted |
| | 49 | | /// </remarks> |
| | 50 | | public void Emit(LogEvent logEvent) |
| | 51 | | { |
| 8 | 52 | | ArgumentNullException.ThrowIfNull(logEvent); |
| | 53 | |
|
| 8 | 54 | | using StringWriter strWriter = new(); |
| 8 | 55 | | TextFormatter.Format(logEvent, strWriter); |
| 8 | 56 | | var renderedMessage = strWriter.ToString(); |
| | 57 | |
|
| | 58 | | lock (_syncRoot) |
| | 59 | | { |
| 8 | 60 | | Callback(logEvent, renderedMessage); |
| 8 | 61 | | } |
| 8 | 62 | | } |
| | 63 | | } |