Add Serilog

This commit is contained in:
Runebaas 2017-09-26 13:02:38 +02:00
parent a7e3bb957f
commit 79e52774b8
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
7 changed files with 60 additions and 46 deletions

2
.gitignore vendored
View file

@ -7,3 +7,5 @@ Backup/
UpgradeLog.htm UpgradeLog.htm
.idea .idea
.vscode .vscode
Geekbot.net/Logs/*
!/Geekbot.net/Logs/.keep

View file

@ -13,9 +13,7 @@
<PackageReference Include="Discord.Net"> <PackageReference Include="Discord.Net">
<Version>1.0.2</Version> <Version>1.0.2</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Google.Apis.YouTube.v3"> <PackageReference Include="Google.Apis.YouTube.v3" Version="1.29.1.991" />
<Version>1.29.1.976</Version>
</PackageReference>
<PackageReference Include="HtmlAgilityPack.NetCore"> <PackageReference Include="HtmlAgilityPack.NetCore">
<Version>1.5.0.1</Version> <Version>1.5.0.1</Version>
</PackageReference> </PackageReference>
@ -23,15 +21,14 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="RestSharp.NetCore"> <PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
<Version>105.2.3</Version> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
</PackageReference> <PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
<PackageReference Include="StackExchange.Redis"> <PackageReference Include="StackExchange.Redis">
<Version>1.2.6</Version> <Version>1.2.6</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Net.Http"> <PackageReference Include="System.Net.Http" Version="4.3.3" />
<Version>4.3.2</Version>
</PackageReference>
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" /> <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Json"> <PackageReference Include="System.Runtime.Serialization.Json">
<Version>4.3.0</Version> <Version>4.3.0</Version>

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using Serilog;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
{ {
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
private readonly Random rnd; private readonly Random rnd;
private readonly int totalCheckEmImages; private readonly int totalCheckEmImages;
public CheckEmImageProvider(Random rnd) public CheckEmImageProvider(Random rnd, ILogger logger)
{ {
var path = Path.GetFullPath("./Storage/checkEmPics"); var path = Path.GetFullPath("./Storage/checkEmPics");
if (File.Exists(path)) if (File.Exists(path))
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
checkEmImageArray = rawCheckEmPics.Split("\n"); checkEmImageArray = rawCheckEmPics.Split("\n");
totalCheckEmImages = checkEmImageArray.Length; totalCheckEmImages = checkEmImageArray.Length;
this.rnd = rnd; this.rnd = rnd;
Console.WriteLine($"-- Loaded {totalCheckEmImages} CheckEm Images"); logger.Information($"-- Loaded {totalCheckEmImages} CheckEm Images");
} }
else else
{ {
Console.WriteLine("checkEmPics File not found"); logger.Error("checkEmPics File not found");
Console.WriteLine($"Path should be {path}"); logger.Error($"Path should be {path}");
} }
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using Serilog;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
{ {
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
private readonly Random rnd; private readonly Random rnd;
private readonly int totalFortunes; private readonly int totalFortunes;
public FortunesProvider(Random rnd) public FortunesProvider(Random rnd, ILogger logger)
{ {
var path = Path.GetFullPath("./Storage/fortunes"); var path = Path.GetFullPath("./Storage/fortunes");
if (File.Exists(path)) if (File.Exists(path))
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
fortuneArray = rawFortunes.Split("%"); fortuneArray = rawFortunes.Split("%");
totalFortunes = fortuneArray.Length; totalFortunes = fortuneArray.Length;
this.rnd = rnd; this.rnd = rnd;
Console.WriteLine($"-- Loaded {totalFortunes} Fortunes"); logger.Information($"-- Loaded {totalFortunes} Fortunes");
} }
else else
{ {
Console.WriteLine("Fortunes File not found"); logger.Error("Fortunes File not found");
Console.WriteLine($"Path should be {path}"); logger.Error($"Path should be {path}");
} }
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using Serilog;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
{ {
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
private readonly Random rnd; private readonly Random rnd;
private readonly int totalPandas; private readonly int totalPandas;
public PandaProvider(Random rnd) public PandaProvider(Random rnd, ILogger logger)
{ {
var path = Path.GetFullPath("./Storage/pandas"); var path = Path.GetFullPath("./Storage/pandas");
if (File.Exists(path)) if (File.Exists(path))
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
PandaArray = rawFortunes.Split("\n"); PandaArray = rawFortunes.Split("\n");
totalPandas = PandaArray.Length; totalPandas = PandaArray.Length;
this.rnd = rnd; this.rnd = rnd;
Console.WriteLine($"-- Loaded {totalPandas} Panda Images"); logger.Information($"-- Loaded {totalPandas} Panda Images");
} }
else else
{ {
Console.WriteLine("Pandas File not found"); logger.Error("Pandas File not found");
Console.WriteLine($"Path should be {path}"); logger.Error($"Path should be {path}");
} }
} }

0
Geekbot.net/Logs/.keep Executable file
View file

View file

@ -1,12 +1,14 @@
using System; using System;
using System.Net.NetworkInformation;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
using StackExchange.Redis; using StackExchange.Redis;
namespace Geekbot.net namespace Geekbot.net
@ -19,23 +21,32 @@ namespace Geekbot.net
private IServiceCollection services; private IServiceCollection services;
private IServiceProvider servicesProvider; private IServiceProvider servicesProvider;
private RedisValue token; private RedisValue token;
private ILogger logger;
private static void Main() private static void Main(string[] args)
{ {
Console.WriteLine(@" ____ _____ _____ _ ______ ___ _____"); var logger = new LoggerConfiguration()
Console.WriteLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); .MinimumLevel.Debug()
Console.WriteLine(@"| | _| _| | _| | ' /| _ \| | | || |"); .WriteTo.LiterateConsole()
Console.WriteLine(@"| |_| | |___| |___| . \| |_) | |_| || |"); .WriteTo.RollingFile("Logs/geekbot-{Hour}.txt", shared: true)
Console.WriteLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); .CreateLogger();
Console.WriteLine("=========================================");
Console.WriteLine("* Starting...");
new Program().MainAsync().GetAwaiter().GetResult(); 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(); client = new DiscordSocketClient();
commands = new CommandService(); commands = new CommandService();
@ -44,11 +55,11 @@ namespace Geekbot.net
{ {
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
redis = redisMultiplexer.GetDatabase(6); redis = redisMultiplexer.GetDatabase(6);
Console.WriteLine("-- Connected to Redis (db6)"); logger.Information($"-- Connected to Redis ({redis.Database})");
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine("Start Redis pls..."); logger.Information("Start Redis pls...");
Environment.Exit(102); Environment.Exit(102);
} }
@ -67,16 +78,17 @@ namespace Geekbot.net
services = new ServiceCollection(); services = new ServiceCollection();
var RandomClient = new Random(); var RandomClient = new Random();
var fortunes = new FortunesProvider(RandomClient); var fortunes = new FortunesProvider(RandomClient, logger);
var checkEmImages = new CheckEmImageProvider(RandomClient); var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
var pandaImages = new PandaProvider(RandomClient); var pandaImages = new PandaProvider(RandomClient, logger);
services.AddSingleton(redis); services.AddSingleton(redis);
services.AddSingleton(RandomClient); services.AddSingleton(RandomClient);
services.AddSingleton<ILogger>(logger);
services.AddSingleton<IFortunesProvider>(fortunes); services.AddSingleton<IFortunesProvider>(fortunes);
services.AddSingleton<ICheckEmImageProvider>(checkEmImages); services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
services.AddSingleton<IPandaProvider>(pandaImages); services.AddSingleton<IPandaProvider>(pandaImages);
Console.WriteLine("* Connecting to Discord"); logger.Information("* Connecting to Discord", Color.Teal);
await Login(); await Login();
@ -93,9 +105,9 @@ namespace Geekbot.net
if (isConneted) if (isConneted)
{ {
await client.SetGameAsync("Ping Pong"); 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 += HandleCommand;
client.MessageReceived += HandleMessageReceived; client.MessageReceived += HandleMessageReceived;
@ -104,12 +116,12 @@ namespace Geekbot.net
services.AddSingleton(commands); services.AddSingleton(commands);
servicesProvider = services.BuildServiceProvider(); servicesProvider = services.BuildServiceProvider();
Console.WriteLine("* Done and ready for use\n"); logger.Information("* Done and ready for use\n", Color.Teal);
} }
} }
catch (AggregateException) catch (AggregateException)
{ {
Console.WriteLine("Could not connect to discord..."); logger.Information("Could not connect to discord...");
Environment.Exit(103); Environment.Exit(103);
} }
} }
@ -155,7 +167,7 @@ namespace Geekbot.net
if (message.Author.Id == client.CurrentUser.Id) return; if (message.Author.Id == client.CurrentUser.Id) return;
var channel = (SocketGuildChannel) message.Channel; 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); message.Content);
await userRec; await userRec;
await guildRec; await guildRec;