From 751c164c820a7daffb84db02176f6bdc1503c10d Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jan 2018 03:30:42 +0100 Subject: [PATCH] Log everything to sumologic --- Geekbot.net/Geekbot.net.csproj | 3 ++- Geekbot.net/Lib/GeekbotLogger.cs | 11 ++++++++--- Geekbot.net/Lib/LoggerFactory.cs | 22 +++++++++++----------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 38e7f94..82b605a 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -25,10 +25,11 @@ - + + 1.2.6 diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index 46799af..18e8c54 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -1,15 +1,19 @@ using System; using System.Threading.Tasks; +using Serilog; using Utf8Json; +using Utf8Json.Formatters; using Utf8Json.Resolvers; namespace Geekbot.net.Lib { public class GeekbotLogger : IGeekbotLogger { + private readonly ILogger _serilog; public GeekbotLogger() { - JsonSerializer.SetDefaultResolver(StandardResolver.AllowPrivateExcludeNullSnakeCase); + _serilog = LoggerFactory.createLogger(); + //JsonSerializer.SetDefaultResolver(StandardResolver.AllowPrivateExcludeNullSnakeCase); Information("Geekbot", "Using GeekbotLogger"); } @@ -36,7 +40,8 @@ namespace Geekbot.net.Lib private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { var logJson = CreateLogObject(type, source, message, null, extra); - Console.WriteLine(logJson); + // fuck serilog + _serilog.Information(logJson + "}"); return Task.CompletedTask; } @@ -51,7 +56,7 @@ namespace Geekbot.net.Lib StackTrace = stackTrace, Extra = extra }; - return Utf8Json.JsonSerializer.ToJsonString(logObject); + return JsonSerializer.ToJsonString(logObject); } } diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index d1fe698..789bdc6 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -1,26 +1,26 @@ using System; using Serilog; -using System.Linq; +using Serilog.Formatting.Json; +using Serilog.Sinks.SumoLogic; namespace Geekbot.net.Lib { public class LoggerFactory { - public static ILogger createLogger(string[] args) + public static ILogger createLogger() { - var loggerCreation = new LoggerConfiguration() - .WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true); - if (!Environment.CurrentDirectory.Contains("deploy")) + var loggerCreation = new LoggerConfiguration(); + var template = "{Message}{NewLine}"; + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"))) { - loggerCreation.WriteTo.LiterateConsole(); - } - if (args.Contains("--verbose")) - { - loggerCreation.MinimumLevel.Verbose(); + Console.WriteLine("Logging Geekbot Logs to Sumologic"); + loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), + outputTemplate: template); } else { - loggerCreation.MinimumLevel.Information(); + loggerCreation.WriteTo.LiterateConsole(outputTemplate: template); + loggerCreation.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true, outputTemplate: template); } return loggerCreation.CreateLogger(); }