From d19c9e340397071599498d2adb03a4654492bd15 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 4 Oct 2021 18:48:42 -0500 Subject: add ConcurrentFixedSizeQueue --- src/slskd/Common/ConcurrentFixedSizeQueue.cs | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/slskd/Common/ConcurrentFixedSizeQueue.cs (limited to 'src') diff --git a/src/slskd/Common/ConcurrentFixedSizeQueue.cs b/src/slskd/Common/ConcurrentFixedSizeQueue.cs new file mode 100644 index 00000000..f0420f2a --- /dev/null +++ b/src/slskd/Common/ConcurrentFixedSizeQueue.cs @@ -0,0 +1,50 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System.Collections; + using System.Collections.Concurrent; + using System.Collections.Generic; + + public class ConcurrentFixedSizeQueue : IReadOnlyCollection + { + public ConcurrentFixedSizeQueue(int size) + { + Size = size; + } + + public int Count => Queue.Count; + + private ConcurrentQueue Queue { get; } = new ConcurrentQueue(); + private int Size { get; } + + public void Enqueue(T item) + { + Queue.Enqueue(item); + + if (Queue.Count > Size) + { + Queue.TryDequeue(out _); + } + } + + public IEnumerator GetEnumerator() => Queue.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => Queue.GetEnumerator(); + } +} \ No newline at end of file -- cgit v1.2.3 From f94744408cac94ffe096d4c5e7b646d37a52291d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 4 Oct 2021 18:50:48 -0500 Subject: add DelegatingSink --- src/slskd/Common/DelegatingSink.cs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/slskd/Common/DelegatingSink.cs (limited to 'src') diff --git a/src/slskd/Common/DelegatingSink.cs b/src/slskd/Common/DelegatingSink.cs new file mode 100644 index 00000000..75b0e60f --- /dev/null +++ b/src/slskd/Common/DelegatingSink.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using Serilog.Core; + using Serilog.Events; + + public class DelegatingSink : ILogEventSink + { + public DelegatingSink(Action action) + { + Action = action; + } + + private Action Action { get; } + + public void Emit(LogEvent logEvent) + { + Action(logEvent); + } + } +} \ No newline at end of file -- cgit v1.2.3 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') 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 090b4ba79d0859e90d71671c62da96c3f8a6deb2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 4 Oct 2021 18:51:44 -0500 Subject: add LogsController and DTO --- src/slskd/Core/API/Controllers/LogsController.cs | 62 ++++++++++++++++++++++++ src/slskd/Core/API/DTO/Log.cs | 11 +++++ 2 files changed, 73 insertions(+) create mode 100644 src/slskd/Core/API/Controllers/LogsController.cs create mode 100644 src/slskd/Core/API/DTO/Log.cs (limited to 'src') diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs new file mode 100644 index 00000000..6704a9e6 --- /dev/null +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -0,0 +1,62 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Core.API +{ + using System.Collections.Generic; + using System.IO; + using System.Linq; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using Serilog.Formatting.Compact; + using Serilog.Formatting.Display; + using Serilog.Formatting.Json; + + /// + /// Application. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class LogsController : ControllerBase + { + /// + /// Gets the last few application logs. + /// + /// + [HttpGet] + [Authorize] + public IActionResult Logs() + { + var fmt = new CompactJsonFormatter(); + var logs = Program.LogBuffer.ToList(); + + List o = new List(); + + foreach (var log in logs) + { + var renderSpace = new StringWriter(); + fmt.Format(log, renderSpace); + o.Add(renderSpace.ToString()); + } + + return Ok(o); + } + } +} diff --git a/src/slskd/Core/API/DTO/Log.cs b/src/slskd/Core/API/DTO/Log.cs new file mode 100644 index 00000000..506960d5 --- /dev/null +++ b/src/slskd/Core/API/DTO/Log.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace slskd.Core.API.DTO +{ + public class Log + { + } +} -- cgit v1.2.3 From 5cff5deef0279f1bfa38f95086a9906a1200260a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 4 Oct 2021 20:39:59 -0500 Subject: refine return type --- src/slskd/Core/API/Controllers/LogsController.cs | 22 +++----------- src/slskd/Core/API/DTO/Log.cs | 11 ------- src/slskd/Core/API/DTO/LogRecord.cs | 38 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 29 deletions(-) delete mode 100644 src/slskd/Core/API/DTO/Log.cs create mode 100644 src/slskd/Core/API/DTO/LogRecord.cs (limited to 'src') diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index 6704a9e6..a016d493 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -17,17 +17,12 @@ namespace slskd.Core.API { - using System.Collections.Generic; - using System.IO; using System.Linq; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using Serilog.Formatting.Compact; - using Serilog.Formatting.Display; - using Serilog.Formatting.Json; /// - /// Application. + /// Logs. /// [Route("api/v{version:apiVersion}/[controller]")] [ApiVersion("0")] @@ -44,19 +39,10 @@ namespace slskd.Core.API [Authorize] public IActionResult Logs() { - var fmt = new CompactJsonFormatter(); - var logs = Program.LogBuffer.ToList(); + var logs = Program.LogBuffer + .Select(logEvent => LogRecord.FromLogEvent(logEvent)); - List o = new List(); - - foreach (var log in logs) - { - var renderSpace = new StringWriter(); - fmt.Format(log, renderSpace); - o.Add(renderSpace.ToString()); - } - - return Ok(o); + return Ok(logs); } } } diff --git a/src/slskd/Core/API/DTO/Log.cs b/src/slskd/Core/API/DTO/Log.cs deleted file mode 100644 index 506960d5..00000000 --- a/src/slskd/Core/API/DTO/Log.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace slskd.Core.API.DTO -{ - public class Log - { - } -} diff --git a/src/slskd/Core/API/DTO/LogRecord.cs b/src/slskd/Core/API/DTO/LogRecord.cs new file mode 100644 index 00000000..c65f62ad --- /dev/null +++ b/src/slskd/Core/API/DTO/LogRecord.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Core.API +{ + using System; + using Serilog.Events; + + public class LogRecord + { + public DateTime Timestamp { get; set; } + public string Context { get; set; } + public string Level { get; set; } + public string Message { get; set; } + + public static LogRecord FromLogEvent(LogEvent e) => new LogRecord() + { + Timestamp = e.Timestamp.LocalDateTime, + Context = e.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'), + Level = e.Level.ToString(), + Message = e.RenderMessage(), + }; + } +} -- cgit v1.2.3 From fd6f8adfdbae8769c484416ff6ef80b5a4781f12 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:24:41 -0500 Subject: move log stuff to Logging --- src/slskd/Common/DelegatingSink.cs | 38 ------------------------------ src/slskd/Common/Logging/DelegatingSink.cs | 38 ++++++++++++++++++++++++++++++ src/slskd/Common/Logging/LogRecord.cs | 29 +++++++++++++++++++++++ src/slskd/Core/API/DTO/LogRecord.cs | 38 ------------------------------ 4 files changed, 67 insertions(+), 76 deletions(-) delete mode 100644 src/slskd/Common/DelegatingSink.cs create mode 100644 src/slskd/Common/Logging/DelegatingSink.cs create mode 100644 src/slskd/Common/Logging/LogRecord.cs delete mode 100644 src/slskd/Core/API/DTO/LogRecord.cs (limited to 'src') diff --git a/src/slskd/Common/DelegatingSink.cs b/src/slskd/Common/DelegatingSink.cs deleted file mode 100644 index 75b0e60f..00000000 --- a/src/slskd/Common/DelegatingSink.cs +++ /dev/null @@ -1,38 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd -{ - using System; - using Serilog.Core; - using Serilog.Events; - - public class DelegatingSink : ILogEventSink - { - public DelegatingSink(Action action) - { - Action = action; - } - - private Action Action { get; } - - public void Emit(LogEvent logEvent) - { - Action(logEvent); - } - } -} \ No newline at end of file diff --git a/src/slskd/Common/Logging/DelegatingSink.cs b/src/slskd/Common/Logging/DelegatingSink.cs new file mode 100644 index 00000000..75b0e60f --- /dev/null +++ b/src/slskd/Common/Logging/DelegatingSink.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using Serilog.Core; + using Serilog.Events; + + public class DelegatingSink : ILogEventSink + { + public DelegatingSink(Action action) + { + Action = action; + } + + private Action Action { get; } + + public void Emit(LogEvent logEvent) + { + Action(logEvent); + } + } +} \ No newline at end of file diff --git a/src/slskd/Common/Logging/LogRecord.cs b/src/slskd/Common/Logging/LogRecord.cs new file mode 100644 index 00000000..49e00276 --- /dev/null +++ b/src/slskd/Common/Logging/LogRecord.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + + public record LogRecord + { + public DateTime Timestamp { get; init; } + public string Context { get; init; } + public string Level { get; init; } + public string Message { get; init; } + } +} diff --git a/src/slskd/Core/API/DTO/LogRecord.cs b/src/slskd/Core/API/DTO/LogRecord.cs deleted file mode 100644 index c65f62ad..00000000 --- a/src/slskd/Core/API/DTO/LogRecord.cs +++ /dev/null @@ -1,38 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Core.API -{ - using System; - using Serilog.Events; - - public class LogRecord - { - public DateTime Timestamp { get; set; } - public string Context { get; set; } - public string Level { get; set; } - public string Message { get; set; } - - public static LogRecord FromLogEvent(LogEvent e) => new LogRecord() - { - Timestamp = e.Timestamp.LocalDateTime, - Context = e.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'), - Level = e.Level.ToString(), - Message = e.RenderMessage(), - }; - } -} -- 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') 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 From f7958c7fa8dba310d5e75d491214de7866862b87 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:25:22 -0500 Subject: map log hub route --- src/slskd/Startup.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 2098ee3c..f848c4aa 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -333,6 +333,7 @@ namespace slskd app.UseEndpoints(endpoints => { endpoints.MapHub("/hub/application"); + endpoints.MapHub("/hub/log"); endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); -- cgit v1.2.3 From 18cd187fc4d1cd0eacc77ad5290ae060c08263ff Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:25:45 -0500 Subject: bind the LogEmitted event to the hub --- src/slskd/Application.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index dfdd2d78..b6d29d6b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -64,7 +64,8 @@ namespace slskd IRoomService roomService, ISharedFileCache sharedFileCache, IPushbulletService pushbulletService, - IHubContext applicationHub) + IHubContext applicationHub, + IHubContext logHub) { OptionsAtStartup = optionsAtStartup; @@ -87,6 +88,9 @@ namespace slskd RoomService = roomService; ApplicationHub = applicationHub; + LogHub = logHub; + Program.LogEmitted += (_, log) => LogHub.EmitLogAsync(log); + Client = soulseekClient; Client.DiagnosticGenerated += Client_DiagnosticGenerated; @@ -128,6 +132,7 @@ namespace slskd private IManagedState State { get; } private ITransferTracker TransferTracker { get; set; } private IHubContext ApplicationHub { get; set; } + private IHubContext LogHub { get; set; } /// /// Gets the version of the latest application release. -- cgit v1.2.3 From 8cceee2b986a93783d5a9c3f3e0a25fd7581b971 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:25:56 -0500 Subject: remove unused using --- src/slskd/Core/API/Hubs/ApplicationHub.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Core/API/Hubs/ApplicationHub.cs b/src/slskd/Core/API/Hubs/ApplicationHub.cs index 1900a8d0..c02f625f 100644 --- a/src/slskd/Core/API/Hubs/ApplicationHub.cs +++ b/src/slskd/Core/API/Hubs/ApplicationHub.cs @@ -17,7 +17,6 @@ namespace slskd.Core.API { - using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; -- cgit v1.2.3 From d59164efe4286a49c3cbc18ba1fb7db6fb387581 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:26:06 -0500 Subject: add LogHub --- src/slskd/Core/API/Hubs/LogHub.cs | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/slskd/Core/API/Hubs/LogHub.cs (limited to 'src') diff --git a/src/slskd/Core/API/Hubs/LogHub.cs b/src/slskd/Core/API/Hubs/LogHub.cs new file mode 100644 index 00000000..18d12e1c --- /dev/null +++ b/src/slskd/Core/API/Hubs/LogHub.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Core.API +{ + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.SignalR; + + public static class LogHubMethods + { + public static readonly string Buffer = "BUFFER"; + public static readonly string Log = "LOG"; + } + + /// + /// Extension methods for the logs SignalR hub. + /// + public static class LogHubExtensions + { + /// + /// Broadcast a log record. + /// + /// The hub. + /// The log record to broadcast. + /// The operation context. + public static Task EmitLogAsync(this IHubContext hub, LogRecord record) + { + return hub.Clients.All.SendAsync(LogHubMethods.Log, record); + } + } + + /// + /// The logs SignalR hub. + /// + [Authorize] + public class LogHub : Hub + { + public override async Task OnConnectedAsync() + { + await Clients.Caller.SendAsync(LogHubMethods.Buffer, Program.LogBuffer); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 9e9d63b36ed8aed0a1d00ee819cf48de60d22565 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:26:25 -0500 Subject: simplify processing in LogsController --- src/slskd/Core/API/Controllers/LogsController.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index a016d493..57366867 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -17,7 +17,6 @@ namespace slskd.Core.API { - using System.Linq; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -39,10 +38,7 @@ namespace slskd.Core.API [Authorize] public IActionResult Logs() { - var logs = Program.LogBuffer - .Select(logEvent => LogRecord.FromLogEvent(logEvent)); - - return Ok(logs); + return Ok(Program.LogBuffer); } } } -- cgit v1.2.3 From 869bbac175c9ee55a6e0ed4c30237ec969e48dc0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 5 Oct 2021 08:27:09 -0500 Subject: Log -> Logs --- src/slskd/Application.cs | 4 +-- src/slskd/Core/API/Hubs/LogHub.cs | 58 -------------------------------------- src/slskd/Core/API/Hubs/LogsHub.cs | 58 ++++++++++++++++++++++++++++++++++++++ src/slskd/Startup.cs | 2 +- 4 files changed, 61 insertions(+), 61 deletions(-) delete mode 100644 src/slskd/Core/API/Hubs/LogHub.cs create mode 100644 src/slskd/Core/API/Hubs/LogsHub.cs (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b6d29d6b..1027d7f3 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -65,7 +65,7 @@ namespace slskd ISharedFileCache sharedFileCache, IPushbulletService pushbulletService, IHubContext applicationHub, - IHubContext logHub) + IHubContext logHub) { OptionsAtStartup = optionsAtStartup; @@ -132,7 +132,7 @@ namespace slskd private IManagedState State { get; } private ITransferTracker TransferTracker { get; set; } private IHubContext ApplicationHub { get; set; } - private IHubContext LogHub { get; set; } + private IHubContext LogHub { get; set; } /// /// Gets the version of the latest application release. diff --git a/src/slskd/Core/API/Hubs/LogHub.cs b/src/slskd/Core/API/Hubs/LogHub.cs deleted file mode 100644 index 18d12e1c..00000000 --- a/src/slskd/Core/API/Hubs/LogHub.cs +++ /dev/null @@ -1,58 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Core.API -{ - using System.Threading.Tasks; - using Microsoft.AspNetCore.Authorization; - using Microsoft.AspNetCore.SignalR; - - public static class LogHubMethods - { - public static readonly string Buffer = "BUFFER"; - public static readonly string Log = "LOG"; - } - - /// - /// Extension methods for the logs SignalR hub. - /// - public static class LogHubExtensions - { - /// - /// Broadcast a log record. - /// - /// The hub. - /// The log record to broadcast. - /// The operation context. - public static Task EmitLogAsync(this IHubContext hub, LogRecord record) - { - return hub.Clients.All.SendAsync(LogHubMethods.Log, record); - } - } - - /// - /// The logs SignalR hub. - /// - [Authorize] - public class LogHub : Hub - { - public override async Task OnConnectedAsync() - { - await Clients.Caller.SendAsync(LogHubMethods.Buffer, Program.LogBuffer); - } - } -} \ No newline at end of file diff --git a/src/slskd/Core/API/Hubs/LogsHub.cs b/src/slskd/Core/API/Hubs/LogsHub.cs new file mode 100644 index 00000000..7c14cc17 --- /dev/null +++ b/src/slskd/Core/API/Hubs/LogsHub.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Core.API +{ + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.SignalR; + + public static class LogHubMethods + { + public static readonly string Buffer = "BUFFER"; + public static readonly string Log = "LOG"; + } + + /// + /// Extension methods for the logs SignalR hub. + /// + public static class LogHubExtensions + { + /// + /// Broadcast a log record. + /// + /// The hub. + /// The log record to broadcast. + /// The operation context. + public static Task EmitLogAsync(this IHubContext hub, LogRecord record) + { + return hub.Clients.All.SendAsync(LogHubMethods.Log, record); + } + } + + /// + /// The logs SignalR hub. + /// + [Authorize] + public class LogsHub : Hub + { + public override async Task OnConnectedAsync() + { + await Clients.Caller.SendAsync(LogHubMethods.Buffer, Program.LogBuffer); + } + } +} \ No newline at end of file diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index f848c4aa..322d8ed8 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -333,7 +333,7 @@ namespace slskd app.UseEndpoints(endpoints => { endpoints.MapHub("/hub/application"); - endpoints.MapHub("/hub/log"); + endpoints.MapHub("/hub/logs"); endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); -- cgit v1.2.3