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

@ -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);
}
}

View file

@ -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"),