diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-10-05 09:27:54 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-05 09:27:54 -0500 |
| commit | 228cd7f97761137bde191d45c1771879a8d45de3 (patch) | |
| tree | 814080998abb27aa0edf07585b9833c5c56c4535 /src/slskd/Program.cs | |
| parent | 1c790a33ba51598e5d5e9da77cf294c601e702be (diff) | |
| parent | 869bbac175c9ee55a6e0ed4c30237ec969e48dc0 (diff) | |
| download | slskd-228cd7f97761137bde191d45c1771879a8d45de3.tar.gz | |
Merge pull request #208 from slskd/buffer-logs
Buffer application logs and make them available via a REST API and SignalR hub
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9590ed48..b5e628cc 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -84,6 +84,11 @@ namespace slskd public static readonly string XmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); /// <summary> + /// Occurs when a new log event is emitted. + /// </summary> + public static event EventHandler<LogRecord> LogEmitted; + + /// <summary> /// Gets the assembly version of the application. /// </summary> /// <remarks> @@ -119,6 +124,11 @@ namespace slskd /// </summary> public static bool IsCanary { get; } = InformationalVersion.EndsWith("65534"); + /// <summary> + /// Gets a buffer containing the last few log events. + /// </summary> + public static ConcurrentFixedSizeQueue<LogRecord> LogBuffer { get; } = new ConcurrentFixedSizeQueue<LogRecord>(size: 500); + private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); @@ -233,6 +243,19 @@ namespace slskd config => config.GrafanaLoki( OptionsAtStartup.Logger.Loki ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) + .WriteTo.Sink(new DelegatingSink(logEvent => + { + var record = new LogRecord() + { + Timestamp = logEvent.Timestamp.LocalDateTime, + Context = logEvent.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'), + Level = logEvent.Level.ToString(), + Message = logEvent.RenderMessage().TrimStart('"').TrimEnd('"'), + }; + + LogBuffer.Enqueue(record); + LogEmitted?.Invoke(null, record); + })) .CreateLogger(); var logger = Log.ForContext(typeof(Program)); |
