More explicit logging configuration

This commit is contained in:
Runebaas 2018-05-01 16:50:48 +02:00
parent fa5797cd22
commit f3156176dd
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
4 changed files with 19 additions and 11 deletions

View file

@ -59,7 +59,7 @@ namespace Geekbot.net.Commands
[Command("pinguin", RunMode = RunMode.Async)] [Command("pinguin", RunMode = RunMode.Async)]
[Alias("pingu")] [Alias("pingu")]
[Remarks(CommandCategories.Randomness)] [Remarks(CommandCategories.Randomness)]
[Summary("Get a random turtle image")] [Summary("Get a random pinguin image")]
public async Task Pinguin() public async Task Pinguin()
{ {
await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin())); await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin()));
@ -67,7 +67,7 @@ namespace Geekbot.net.Commands
[Command("fox", RunMode = RunMode.Async)] [Command("fox", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Randomness)] [Remarks(CommandCategories.Randomness)]
[Summary("Get a random turtle image")] [Summary("Get a random fox image")]
public async Task Fox() public async Task Fox()
{ {
await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); await ReplyAsync("", false, Eb(_mediaProvider.GetFox()));

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Serilog; using Serilog;
@ -8,9 +7,18 @@ namespace Geekbot.net.Lib
public class GeekbotLogger : IGeekbotLogger public class GeekbotLogger : IGeekbotLogger
{ {
private readonly ILogger _serilog; 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"); Information("Geekbot", "Using GeekbotLogger");
} }
@ -34,12 +42,11 @@ namespace Geekbot.net.Lib
HandleLogObject("Error", source, message, stackTrace, extra); 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); var logJson = CreateLogObject(type, source, message, stackTrace, extra);
// fuck serilog // fuck serilog
_serilog.Information(logJson + "}"); _serilog.Information(logJson + "}");
return Task.CompletedTask;
} }
private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) 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, StackTrace = stackTrace,
Extra = extra Extra = extra
}; };
return JsonConvert.SerializeObject(logObject); return JsonConvert.SerializeObject(logObject, _jsonFormatting, _serializerSettings);
} }
} }

View file

@ -6,11 +6,11 @@ namespace Geekbot.net.Lib
{ {
public class LoggerFactory public class LoggerFactory
{ {
public static ILogger CreateLogger() public static ILogger CreateLogger(bool sumologicActive)
{ {
var loggerCreation = new LoggerConfiguration(); var loggerCreation = new LoggerConfiguration();
var template = "{Message}{NewLine}"; var template = "{Message}{NewLine}";
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"))) if (sumologicActive)
{ {
Console.WriteLine("Logging Geekbot Logs to Sumologic"); Console.WriteLine("Logging Geekbot Logs to Sumologic");
loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"),

View file

@ -39,7 +39,8 @@ namespace Geekbot.net
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
logo.AppendLine("========================================="); logo.AppendLine("=========================================");
Console.WriteLine(logo.ToString()); 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..."); logger.Information("Geekbot", "Starting...");
try try
{ {