From f3156176dd1cc6bc1a95f0a19324e26bced11f77 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 1 May 2018 16:50:48 +0200 Subject: [PATCH] More explicit logging configuration --- Geekbot.net/Commands/RandomAnimals.cs | 4 ++-- Geekbot.net/Lib/GeekbotLogger.cs | 19 +++++++++++++------ Geekbot.net/Lib/LoggerFactory.cs | 4 ++-- Geekbot.net/Program.cs | 3 ++- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/RandomAnimals.cs index 2101c33..7829b21 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/RandomAnimals.cs @@ -59,7 +59,7 @@ namespace Geekbot.net.Commands [Command("pinguin", RunMode = RunMode.Async)] [Alias("pingu")] [Remarks(CommandCategories.Randomness)] - [Summary("Get a random turtle image")] + [Summary("Get a random pinguin image")] public async Task Pinguin() { await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin())); @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands [Command("fox", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] - [Summary("Get a random turtle image")] + [Summary("Get a random fox image")] public async Task Fox() { await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index 5ae65d6..a9fb3a1 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; using Newtonsoft.Json; using Serilog; @@ -8,9 +7,18 @@ namespace Geekbot.net.Lib public class GeekbotLogger : IGeekbotLogger { private readonly ILogger _serilog; - public GeekbotLogger() + private readonly JsonSerializerSettings _serializerSettings; + private readonly Formatting _jsonFormatting; + + public GeekbotLogger(bool sumologicActive) { - _serilog = LoggerFactory.CreateLogger(); + _serilog = LoggerFactory.CreateLogger(sumologicActive); + _serializerSettings = new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, + NullValueHandling = NullValueHandling.Include + }; + _jsonFormatting = sumologicActive ? Formatting.None : Formatting.Indented; Information("Geekbot", "Using GeekbotLogger"); } @@ -34,12 +42,11 @@ namespace Geekbot.net.Lib HandleLogObject("Error", source, message, stackTrace, extra); } - private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) + private void HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { var logJson = CreateLogObject(type, source, message, stackTrace, extra); // fuck serilog _serilog.Information(logJson + "}"); - return Task.CompletedTask; } private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) @@ -53,7 +60,7 @@ namespace Geekbot.net.Lib StackTrace = stackTrace, Extra = extra }; - return JsonConvert.SerializeObject(logObject); + return JsonConvert.SerializeObject(logObject, _jsonFormatting, _serializerSettings); } } diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 9bff873..24df1ab 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -6,11 +6,11 @@ namespace Geekbot.net.Lib { public class LoggerFactory { - public static ILogger CreateLogger() + public static ILogger CreateLogger(bool sumologicActive) { var loggerCreation = new LoggerConfiguration(); var template = "{Message}{NewLine}"; - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"))) + if (sumologicActive) { Console.WriteLine("Logging Geekbot Logs to Sumologic"); loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 0a107e9..10f0dcb 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -39,7 +39,8 @@ namespace Geekbot.net logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); logo.AppendLine("========================================="); Console.WriteLine(logo.ToString()); - var logger = new GeekbotLogger(); + var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); + var logger = new GeekbotLogger(sumologicActive); logger.Information("Geekbot", "Starting..."); try {