Switch to Serilog, organize code a bit
This commit is contained in:
parent
f98db9e657
commit
ce0185c6a2
@ -14,6 +14,10 @@
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||
<PackageReference Include="Serilog" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -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<GodReplacementBot>();
|
||||
|
||||
var config = new DiscordSocketConfig()
|
||||
try
|
||||
{
|
||||
};
|
||||
builder.Services.AddSingleton(config);
|
||||
builder.Services.AddSingleton<DiscordSocketClient>();
|
||||
|
||||
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<Program>()
|
||||
.Build()
|
||||
)
|
||||
.ConfigureServices(services => services
|
||||
.AddSingleton<DiscordSocketClient>()
|
||||
.AddHostedService<GodReplacementBot>()
|
||||
)
|
||||
.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<GodReplacementBot> _logger;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
@ -34,28 +63,42 @@ public sealed class GodReplacementBot : IHostedService {
|
||||
ILogger<GodReplacementBot> 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<string>("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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user