Add Serilog
This commit is contained in:
parent
a7e3bb957f
commit
79e52774b8
7 changed files with 60 additions and 46 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -7,3 +7,5 @@ Backup/
|
|||
UpgradeLog.htm
|
||||
.idea
|
||||
.vscode
|
||||
Geekbot.net/Logs/*
|
||||
!/Geekbot.net/Logs/.keep
|
||||
|
|
|
@ -13,9 +13,7 @@
|
|||
<PackageReference Include="Discord.Net">
|
||||
<Version>1.0.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Google.Apis.YouTube.v3">
|
||||
<Version>1.29.1.976</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.29.1.991" />
|
||||
<PackageReference Include="HtmlAgilityPack.NetCore">
|
||||
<Version>1.5.0.1</Version>
|
||||
</PackageReference>
|
||||
|
@ -23,15 +21,14 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="RestSharp.NetCore">
|
||||
<Version>105.2.3</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
||||
<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">
|
||||
<Version>1.2.6</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Http">
|
||||
<Version>4.3.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
||||
<PackageReference Include="System.Runtime.Serialization.Json">
|
||||
<Version>4.3.0</Version>
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
0
Geekbot.net/Logs/.keep
Executable file
0
Geekbot.net/Logs/.keep
Executable file
|
@ -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...");
|
||||
var logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.LiterateConsole()
|
||||
.WriteTo.RollingFile("Logs/geekbot-{Hour}.txt", shared: true)
|
||||
.CreateLogger();
|
||||
|
||||
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();
|
||||
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<ILogger>(logger);
|
||||
services.AddSingleton<IFortunesProvider>(fortunes);
|
||||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||
services.AddSingleton<IPandaProvider>(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;
|
||||
|
|
Loading…
Reference in a new issue