From 79e52774b8ed7d202b0d78b98b9b490e90aa1d5b Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 26 Sep 2017 13:02:38 +0200 Subject: [PATCH] Add Serilog --- .gitignore | 2 + Geekbot.net/Geekbot.net.csproj | 15 +++---- Geekbot.net/Lib/CheckEmImageProvider.cs | 9 ++-- Geekbot.net/Lib/FortunesProvider.cs | 9 ++-- Geekbot.net/Lib/PandaProvider.cs | 11 ++--- Geekbot.net/Logs/.keep | 0 Geekbot.net/Program.cs | 60 +++++++++++++++---------- 7 files changed, 60 insertions(+), 46 deletions(-) create mode 100755 Geekbot.net/Logs/.keep diff --git a/.gitignore b/.gitignore index bec8c02..2e43789 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ Backup/ UpgradeLog.htm .idea .vscode +Geekbot.net/Logs/* +!/Geekbot.net/Logs/.keep diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 5e543ab..37f26fa 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -13,9 +13,7 @@ 1.0.2 - - 1.29.1.976 - + 1.5.0.1 @@ -23,15 +21,14 @@ - - 105.2.3 - + + + + 1.2.6 - - 4.3.2 - + 4.3.0 diff --git a/Geekbot.net/Lib/CheckEmImageProvider.cs b/Geekbot.net/Lib/CheckEmImageProvider.cs index b29a6a2..8b66194 100644 --- a/Geekbot.net/Lib/CheckEmImageProvider.cs +++ b/Geekbot.net/Lib/CheckEmImageProvider.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Serilog; namespace Geekbot.net.Lib { @@ -9,7 +10,7 @@ namespace Geekbot.net.Lib private readonly Random rnd; private readonly int totalCheckEmImages; - public CheckEmImageProvider(Random rnd) + public CheckEmImageProvider(Random rnd, ILogger logger) { var path = Path.GetFullPath("./Storage/checkEmPics"); if (File.Exists(path)) @@ -18,12 +19,12 @@ namespace Geekbot.net.Lib checkEmImageArray = rawCheckEmPics.Split("\n"); totalCheckEmImages = checkEmImageArray.Length; this.rnd = rnd; - Console.WriteLine($"-- Loaded {totalCheckEmImages} CheckEm Images"); + logger.Information($"-- Loaded {totalCheckEmImages} CheckEm Images"); } else { - Console.WriteLine("checkEmPics File not found"); - Console.WriteLine($"Path should be {path}"); + logger.Error("checkEmPics File not found"); + logger.Error($"Path should be {path}"); } } diff --git a/Geekbot.net/Lib/FortunesProvider.cs b/Geekbot.net/Lib/FortunesProvider.cs index 2c6a2e5..f485952 100644 --- a/Geekbot.net/Lib/FortunesProvider.cs +++ b/Geekbot.net/Lib/FortunesProvider.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Serilog; namespace Geekbot.net.Lib { @@ -9,7 +10,7 @@ namespace Geekbot.net.Lib private readonly Random rnd; private readonly int totalFortunes; - public FortunesProvider(Random rnd) + public FortunesProvider(Random rnd, ILogger logger) { var path = Path.GetFullPath("./Storage/fortunes"); if (File.Exists(path)) @@ -18,12 +19,12 @@ namespace Geekbot.net.Lib fortuneArray = rawFortunes.Split("%"); totalFortunes = fortuneArray.Length; this.rnd = rnd; - Console.WriteLine($"-- Loaded {totalFortunes} Fortunes"); + logger.Information($"-- Loaded {totalFortunes} Fortunes"); } else { - Console.WriteLine("Fortunes File not found"); - Console.WriteLine($"Path should be {path}"); + logger.Error("Fortunes File not found"); + logger.Error($"Path should be {path}"); } } diff --git a/Geekbot.net/Lib/PandaProvider.cs b/Geekbot.net/Lib/PandaProvider.cs index 9a392e5..6ac927a 100644 --- a/Geekbot.net/Lib/PandaProvider.cs +++ b/Geekbot.net/Lib/PandaProvider.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Serilog; namespace Geekbot.net.Lib { @@ -9,7 +10,7 @@ namespace Geekbot.net.Lib private readonly Random rnd; private readonly int totalPandas; - public PandaProvider(Random rnd) + public PandaProvider(Random rnd, ILogger logger) { var path = Path.GetFullPath("./Storage/pandas"); if (File.Exists(path)) @@ -18,12 +19,12 @@ namespace Geekbot.net.Lib PandaArray = rawFortunes.Split("\n"); totalPandas = PandaArray.Length; this.rnd = rnd; - Console.WriteLine($"-- Loaded {totalPandas} Panda Images"); + logger.Information($"-- Loaded {totalPandas} Panda Images"); } else { - Console.WriteLine("Pandas File not found"); - Console.WriteLine($"Path should be {path}"); + logger.Error("Pandas File not found"); + logger.Error($"Path should be {path}"); } } @@ -32,7 +33,7 @@ namespace Geekbot.net.Lib return PandaArray[rnd.Next(0, totalPandas)]; } } - + public interface IPandaProvider { string GetRandomPanda(); diff --git a/Geekbot.net/Logs/.keep b/Geekbot.net/Logs/.keep new file mode 100755 index 0000000..e69de29 diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f9e250f..3edeb4e 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,12 +1,14 @@ using System; -using System.Net.NetworkInformation; using System.Reflection; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; using Microsoft.Extensions.DependencyInjection; +using Serilog; +using Serilog.Sinks.SystemConsole.Themes; using StackExchange.Redis; namespace Geekbot.net @@ -19,23 +21,32 @@ namespace Geekbot.net private IServiceCollection services; private IServiceProvider servicesProvider; private RedisValue token; + private ILogger logger; - private static void Main() + private static void Main(string[] args) { - Console.WriteLine(@" ____ _____ _____ _ ______ ___ _____"); - Console.WriteLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); - Console.WriteLine(@"| | _| _| | _| | ' /| _ \| | | || |"); - Console.WriteLine(@"| |_| | |___| |___| . \| |_) | |_| || |"); - Console.WriteLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); - Console.WriteLine("========================================="); - Console.WriteLine("* Starting..."); - - new Program().MainAsync().GetAwaiter().GetResult(); + var logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.LiterateConsole() + .WriteTo.RollingFile("Logs/geekbot-{Hour}.txt", shared: true) + .CreateLogger(); + + var logo = new StringBuilder(); + logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____"); + logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); + logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |"); + logo.AppendLine(@"| |_| | |___| |___| . \| |_) | |_| || |"); + logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); + logo.AppendLine("========================================="); + Console.WriteLine(logo.ToString()); + logger.Information("* Starting..."); + new Program().MainAsync(logger).GetAwaiter().GetResult(); } - public async Task MainAsync() + private async Task MainAsync(ILogger logger) { - Console.WriteLine("* Initing Stuff"); + this.logger = logger; + logger.Information("* Initing Stuff"); client = new DiscordSocketClient(); commands = new CommandService(); @@ -44,11 +55,11 @@ namespace Geekbot.net { var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); redis = redisMultiplexer.GetDatabase(6); - Console.WriteLine("-- Connected to Redis (db6)"); + logger.Information($"-- Connected to Redis ({redis.Database})"); } catch (Exception) { - Console.WriteLine("Start Redis pls..."); + logger.Information("Start Redis pls..."); Environment.Exit(102); } @@ -67,16 +78,17 @@ namespace Geekbot.net services = new ServiceCollection(); var RandomClient = new Random(); - var fortunes = new FortunesProvider(RandomClient); - var checkEmImages = new CheckEmImageProvider(RandomClient); - var pandaImages = new PandaProvider(RandomClient); + var fortunes = new FortunesProvider(RandomClient, logger); + var checkEmImages = new CheckEmImageProvider(RandomClient, logger); + var pandaImages = new PandaProvider(RandomClient, logger); services.AddSingleton(redis); services.AddSingleton(RandomClient); + services.AddSingleton(logger); services.AddSingleton(fortunes); services.AddSingleton(checkEmImages); services.AddSingleton(pandaImages); - Console.WriteLine("* Connecting to Discord"); + logger.Information("* Connecting to Discord", Color.Teal); await Login(); @@ -93,9 +105,9 @@ namespace Geekbot.net if (isConneted) { await client.SetGameAsync("Ping Pong"); - Console.WriteLine($"* Now Connected to {client.Guilds.Count} Servers"); + logger.Information($"* Now Connected to {client.Guilds.Count} Servers"); - Console.WriteLine("* Registering Stuff"); + logger.Information("* Registering Stuff", Color.Teal); client.MessageReceived += HandleCommand; client.MessageReceived += HandleMessageReceived; @@ -104,12 +116,12 @@ namespace Geekbot.net services.AddSingleton(commands); servicesProvider = services.BuildServiceProvider(); - Console.WriteLine("* Done and ready for use\n"); + logger.Information("* Done and ready for use\n", Color.Teal); } } catch (AggregateException) { - Console.WriteLine("Could not connect to discord..."); + logger.Information("Could not connect to discord..."); Environment.Exit(103); } } @@ -155,7 +167,7 @@ namespace Geekbot.net if (message.Author.Id == client.CurrentUser.Id) return; var channel = (SocketGuildChannel) message.Channel; - Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + + logger.Information(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + message.Content); await userRec; await guildRec;