Guides for Serilog and EFCore (#2134)

* Add serilog guide

* added suggestions from Rozen

* Add efcore guide

* Fix review changes

* Fix grammatical errors & review points
This commit is contained in:
Duke
2022-03-02 20:08:51 +01:00
committed by GitHub
parent 7d8911bfed
commit 1fc07e742f
10 changed files with 213 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
using Discord;
using Serilog;
using Serilog.Events;
public class Program
{
static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
_client = new DiscordSocketClient();
_client.Log += LogAsync;
// You can assign your bot token to a string, and pass that in to connect.
// This is, however, insecure, particularly if you plan to have your code hosted in a public repository.
var token = "token";
// Some alternative options would be to keep your token in an Environment Variable or a standalone file.
// var token = Environment.GetEnvironmentVariable("NameOfYourEnvironmentVariable");
// var token = File.ReadAllText("token.txt");
// var token = JsonConvert.DeserializeObject<AConfigurationClass>(File.ReadAllText("config.json")).Token;
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
// Block this task until the program is closed.
await Task.Delay(Timeout.Infinite);
}
}

View File

@@ -0,0 +1,9 @@
private static ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddDbContext<ApplicationDbContext>(
options => options.UseNpgsql("Your connection string")
)
[...]
.BuildServiceProvider();
}

View File

@@ -0,0 +1,19 @@
// ApplicationDbContext.cs
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<UserEntity> Users { get; set; } = null!;
}
// UserEntity.cs
public class UserEntity
{
public ulong Id { get; set; }
public string Name { get; set; }
}

View File

@@ -0,0 +1,20 @@
using Discord;
public class SampleModule : InteractionModuleBase<SocketInteractionContext>
{
private readonly ApplicationDbContext _db;
public SampleModule(ApplicationDbContext db)
{
_db = db;
}
[SlashCommand("sample", "sample")]
public async Task Sample()
{
// Do stuff with your injected DbContext
var user = _db.Users.FirstOrDefault(x => x.Id == Context.User.Id);
...
}
}

View File

@@ -0,0 +1 @@
Log.Debug("Your log message, with {Variables}!", 10); // This will output "[21:51:00 DBG] Your log message, with 10!"

View File

@@ -0,0 +1,15 @@
private static async Task LogAsync(LogMessage message)
{
var severity = message.Severity switch
{
LogSeverity.Critical => LogEventLevel.Fatal,
LogSeverity.Error => LogEventLevel.Error,
LogSeverity.Warning => LogEventLevel.Warning,
LogSeverity.Info => LogEventLevel.Information,
LogSeverity.Verbose => LogEventLevel.Verbose,
LogSeverity.Debug => LogEventLevel.Debug,
_ => LogEventLevel.Information
};
Log.Write(severity, message.Exception, "[{Source}] {Message}", message.Source, message.Message);
await Task.CompletedTask;
}