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:
36
docs/guides/other_libs/samples/ConfiguringSerilog.cs
Normal file
36
docs/guides/other_libs/samples/ConfiguringSerilog.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
9
docs/guides/other_libs/samples/DbContextDepInjection.cs
Normal file
9
docs/guides/other_libs/samples/DbContextDepInjection.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
private static ServiceProvider ConfigureServices()
|
||||
{
|
||||
return new ServiceCollection()
|
||||
.AddDbContext<ApplicationDbContext>(
|
||||
options => options.UseNpgsql("Your connection string")
|
||||
)
|
||||
[...]
|
||||
.BuildServiceProvider();
|
||||
}
|
||||
19
docs/guides/other_libs/samples/DbContextSample.cs
Normal file
19
docs/guides/other_libs/samples/DbContextSample.cs
Normal 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; }
|
||||
}
|
||||
20
docs/guides/other_libs/samples/InteractionModuleDISample.cs
Normal file
20
docs/guides/other_libs/samples/InteractionModuleDISample.cs
Normal 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);
|
||||
|
||||
...
|
||||
}
|
||||
}
|
||||
1
docs/guides/other_libs/samples/LogDebugSample.cs
Normal file
1
docs/guides/other_libs/samples/LogDebugSample.cs
Normal file
@@ -0,0 +1 @@
|
||||
Log.Debug("Your log message, with {Variables}!", 10); // This will output "[21:51:00 DBG] Your log message, with 10!"
|
||||
15
docs/guides/other_libs/samples/ModifyLogMethod.cs
Normal file
15
docs/guides/other_libs/samples/ModifyLogMethod.cs
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user