From 96a7f141ff6b6adefb2ef8af668d5ac125692e54 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 4 Oct 2021 18:51:19 -0500 Subject: configure LogBuffer and LogEmitted --- src/slskd/Program.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9590ed48..f67a3a14 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -83,6 +83,11 @@ namespace slskd /// public static readonly string XmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); + /// + /// Occurs when a new log event is emitted. + /// + public static event EventHandler LogEmitted; + /// /// Gets the assembly version of the application. /// @@ -119,6 +124,11 @@ namespace slskd /// public static bool IsCanary { get; } = InformationalVersion.EndsWith("65534"); + /// + /// Gets a buffer containing the last few log events. + /// + public static ConcurrentFixedSizeQueue LogBuffer { get; } = new ConcurrentFixedSizeQueue(size: 500); + private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); @@ -221,6 +231,11 @@ namespace slskd .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() + .WriteTo.Sink(new DelegatingSink(logEvent => + { + LogBuffer.Enqueue(logEvent); + LogEmitted?.Invoke(null, logEvent); + })) .WriteTo.Console( outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] [{SoulseekContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Async(config => -- cgit v1.2.3 From 3feb7bb0a39719744cdacc64047c95e5d5808c17 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:25:07 -0500 Subject: store LogRecords instead of LogEvents --- src/slskd/Program.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f67a3a14..b5e628cc 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -86,7 +86,7 @@ namespace slskd /// /// Occurs when a new log event is emitted. /// - public static event EventHandler LogEmitted; + public static event EventHandler LogEmitted; /// /// Gets the assembly version of the application. @@ -127,7 +127,7 @@ namespace slskd /// /// Gets a buffer containing the last few log events. /// - public static ConcurrentFixedSizeQueue LogBuffer { get; } = new ConcurrentFixedSizeQueue(size: 500); + public static ConcurrentFixedSizeQueue LogBuffer { get; } = new ConcurrentFixedSizeQueue(size: 500); private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); @@ -231,11 +231,6 @@ namespace slskd .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() - .WriteTo.Sink(new DelegatingSink(logEvent => - { - LogBuffer.Enqueue(logEvent); - LogEmitted?.Invoke(null, logEvent); - })) .WriteTo.Console( outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] [{SoulseekContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Async(config => @@ -248,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)); -- cgit v1.2.3