diff --git a/GodReplacementProduct/GodReplacementProduct.csproj b/GodReplacementProduct/GodReplacementProduct.csproj
index a10f43c..aa056ab 100644
--- a/GodReplacementProduct/GodReplacementProduct.csproj
+++ b/GodReplacementProduct/GodReplacementProduct.csproj
@@ -14,6 +14,10 @@
+
+
+
+
diff --git a/GodReplacementProduct/Program.cs b/GodReplacementProduct/Program.cs
index 6430684..ca4def4 100644
--- a/GodReplacementProduct/Program.cs
+++ b/GodReplacementProduct/Program.cs
@@ -4,29 +4,58 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
+using Serilog;
+using Serilog.Events;
namespace GodReplacementProduct;
public class Program
{
- static void Main(string[] args) {
- var builder = Host.CreateApplicationBuilder(args);
+ public static int Main(string[] args)
+ {
+ Log.Logger = new LoggerConfiguration()
+ .MinimumLevel.Debug()
+ .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
+ .Enrich.FromLogContext()
+ .WriteTo.Console()
+ .CreateLogger();
- builder.Services.AddHostedService();
-
- var config = new DiscordSocketConfig()
+ try
{
- };
- builder.Services.AddSingleton(config);
- builder.Services.AddSingleton();
-
- var host = builder.Build();
- host.Run();
+ Log.Information("Starting host");
+ BuildHost(args).Run();
+ return 0;
+ }
+ catch (Exception ex)
+ {
+ Log.Fatal(ex, "Host terminated unexpectedly");
+ return 1;
+ }
+ finally
+ {
+ Log.CloseAndFlush();
+ }
}
+
+ public static IHost BuildHost(string[] args) => new HostBuilder()
+ .ConfigureAppConfiguration((context) => context
+ .AddUserSecrets()
+ .Build()
+ )
+ .ConfigureServices(services => services
+ .AddSingleton()
+ .AddHostedService()
+ )
+ .UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
+ .ReadFrom.Configuration(context.Configuration)
+ .Enrich.FromLogContext()
+ .WriteTo.Console())
+ .Build();
}
-public sealed class GodReplacementBot : IHostedService {
- private readonly ILogger _logger;
+public sealed class GodReplacementBot : IHostedService
+{
+ private readonly ILogger _logger;
private readonly IConfiguration _config;
private readonly DiscordSocketClient _client;
@@ -34,28 +63,42 @@ public sealed class GodReplacementBot : IHostedService {
ILogger logger,
IConfiguration config,
DiscordSocketClient client
- ) {
- _logger = logger;
+ )
+ {
_config = config;
_client = client;
+ _logger = logger;
}
- public async Task StartAsync(CancellationToken cancellationToken) {
- _client.Log += Log;
+ public async Task StartAsync(CancellationToken cancellationToken)
+ {
var token = _config.GetValue("GodReplacementProject:DiscordToken");
+ _client.Log += LogAsync;
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
-
- await Task.Delay(Timeout.Infinite);
}
- public async Task StopAsync(CancellationToken cancellationToken) {
+ public async Task StopAsync(CancellationToken cancellationToken)
+ {
await _client.StopAsync();
}
- private Task Log(LogMessage msg) {
- Console.WriteLine(msg.ToString());
+ private Task LogAsync(LogMessage message) {
+ var severity = message.Severity switch
+ {
+ LogSeverity.Critical => LogLevel.Critical,
+ LogSeverity.Error => LogLevel.Error,
+ LogSeverity.Warning => LogLevel.Warning,
+ LogSeverity.Info => LogLevel.Information,
+ LogSeverity.Verbose => LogLevel.Debug,
+ LogSeverity.Debug => LogLevel.Trace,
+ _ => LogLevel.Information,
+ };
+
+ _logger.Log(severity, message.Exception, "[{Source}] {Message}", message.Source, message.Message);
+
return Task.CompletedTask;
}
+
}