From 8703a391008a4c0675b369831bca8268928e0c2b Mon Sep 17 00:00:00 2001 From: jvyden Date: Fri, 19 Nov 2021 02:21:49 -0500 Subject: [PATCH] Add InfluxLogger for Kettu, log slot counts --- ProjectLighthouse/Helpers/InfluxHelper.cs | 12 +++++---- ProjectLighthouse/Logging/InfluxLogger.cs | 27 +++++++++++++++++++ ProjectLighthouse/Program.cs | 3 +++ .../Types/Settings/ServerSettings.cs | 5 ++-- 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 ProjectLighthouse/Logging/InfluxLogger.cs diff --git a/ProjectLighthouse/Helpers/InfluxHelper.cs b/ProjectLighthouse/Helpers/InfluxHelper.cs index cd5b844e..166429fc 100644 --- a/ProjectLighthouse/Helpers/InfluxHelper.cs +++ b/ProjectLighthouse/Helpers/InfluxHelper.cs @@ -14,8 +14,10 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static async void Log() { - WriteApi writeApi = Client.GetWriteApi(); - PointData point = PointData.Measurement("lighthouse").Field("playerCount", await StatisticsHelper.RecentMatches()); + using WriteApi writeApi = Client.GetWriteApi(); + PointData point = PointData.Measurement("lighthouse") + .Field("playerCount", await StatisticsHelper.RecentMatches()) + .Field("slotCount", await StatisticsHelper.SlotCount()); writeApi.WritePoint(ServerSettings.Instance.InfluxBucket, ServerSettings.Instance.InfluxOrg, point); @@ -26,17 +28,17 @@ namespace LBPUnion.ProjectLighthouse.Helpers { await Client.ReadyAsync(); Logger.Log("InfluxDB is now ready.", LoggerLevelInflux.Instance); - Thread t = new Thread + Thread t = new ( delegate() { while (true) { - Thread.Sleep(5000); #pragma warning disable CS4014 Log(); #pragma warning restore CS4014 - Logger.Log("Logged.", LoggerLevelInflux.Instance); +// Logger.Log("Logged.", LoggerLevelInflux.Instance); + Thread.Sleep(60000); } } ); diff --git a/ProjectLighthouse/Logging/InfluxLogger.cs b/ProjectLighthouse/Logging/InfluxLogger.cs new file mode 100644 index 00000000..a4ab28e9 --- /dev/null +++ b/ProjectLighthouse/Logging/InfluxLogger.cs @@ -0,0 +1,27 @@ +using InfluxDB.Client; +using InfluxDB.Client.Writes; +using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Types.Settings; + +namespace LBPUnion.ProjectLighthouse.Logging +{ + public class InfluxLogger : LoggerBase + { + + public override void Send(LoggerLine line) + { + string channel = string.IsNullOrEmpty(line.LoggerLevel.Channel) ? "" : $"[{line.LoggerLevel.Channel}] "; + + string level = $"{$"{line.LoggerLevel.Name} {channel}".TrimEnd()}"; + string content = line.LineData; + + using WriteApi writeApi = InfluxHelper.Client.GetWriteApi(); + + PointData point = PointData.Measurement("lighthouseLog").Field("level", level).Field("content", content); + + writeApi.WritePoint(ServerSettings.Instance.InfluxBucket, ServerSettings.Instance.InfluxOrg, point); + } + public override bool AllowMultiple => false; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index e03ba6f5..695e2470 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -47,7 +47,10 @@ namespace LBPUnion.ProjectLighthouse if (ServerSettings.Instance.InfluxEnabled) { Logger.Log("Influx logging is enabled. Starting influx logging...", LoggerLevelStartup.Instance); + #pragma warning disable CS4014 InfluxHelper.StartLogging(); + #pragma warning restore CS4014 + if (ServerSettings.Instance.InfluxLoggingEnabled) Logger.AddLogger(new InfluxLogger()); } stopwatch.Stop(); diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index fcdc9a8f..405e90db 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -68,7 +68,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings [NotNull] public static ServerSettings Instance; - public const int CurrentConfigVersion = 3; + public const int CurrentConfigVersion = 4; [JsonPropertyName("ConfigVersionDoNotModifyOrYouWillBeSlapped")] public int ConfigVersion { get; set; } = CurrentConfigVersion; @@ -77,7 +77,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings #endregion Meta - public bool InfluxEnabled { get; set; } = false; + public bool InfluxEnabled { get; set; } + public bool InfluxLoggingEnabled { get; set; } public string InfluxOrg { get; set; } = "lighthouse"; public string InfluxBucket { get; set; } = "lighthouse"; public string InfluxToken { get; set; } = "";