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" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" 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="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>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -4,29 +4,58 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
namespace GodReplacementProduct;
|
namespace GodReplacementProduct;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args) {
|
public static int Main(string[] args)
|
||||||
var builder = Host.CreateApplicationBuilder(args);
|
|
||||||
|
|
||||||
builder.Services.AddHostedService<GodReplacementBot>();
|
|
||||||
|
|
||||||
var config = new DiscordSocketConfig()
|
|
||||||
{
|
{
|
||||||
};
|
Log.Logger = new LoggerConfiguration()
|
||||||
builder.Services.AddSingleton(config);
|
.MinimumLevel.Debug()
|
||||||
builder.Services.AddSingleton<DiscordSocketClient>();
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
var host = builder.Build();
|
try
|
||||||
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 {
|
public sealed class GodReplacementBot : IHostedService
|
||||||
private readonly ILogger _logger;
|
{
|
||||||
|
private readonly ILogger<GodReplacementBot> _logger;
|
||||||
private readonly IConfiguration _config;
|
private readonly IConfiguration _config;
|
||||||
private readonly DiscordSocketClient _client;
|
private readonly DiscordSocketClient _client;
|
||||||
|
|
||||||
@ -34,28 +63,42 @@ public sealed class GodReplacementBot : IHostedService {
|
|||||||
ILogger<GodReplacementBot> logger,
|
ILogger<GodReplacementBot> logger,
|
||||||
IConfiguration config,
|
IConfiguration config,
|
||||||
DiscordSocketClient client
|
DiscordSocketClient client
|
||||||
) {
|
)
|
||||||
_logger = logger;
|
{
|
||||||
_config = config;
|
_config = config;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StartAsync(CancellationToken cancellationToken) {
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
_client.Log += Log;
|
{
|
||||||
var token = _config.GetValue<string>("GodReplacementProject:DiscordToken");
|
var token = _config.GetValue<string>("GodReplacementProject:DiscordToken");
|
||||||
|
_client.Log += LogAsync;
|
||||||
|
|
||||||
await _client.LoginAsync(TokenType.Bot, token);
|
await _client.LoginAsync(TokenType.Bot, token);
|
||||||
await _client.StartAsync();
|
await _client.StartAsync();
|
||||||
|
|
||||||
await Task.Delay(Timeout.Infinite);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StopAsync(CancellationToken cancellationToken) {
|
public async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
await _client.StopAsync();
|
await _client.StopAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Log(LogMessage msg) {
|
private Task LogAsync(LogMessage message) {
|
||||||
Console.WriteLine(msg.ToString());
|
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;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user