A .NET logger provider to send log entries to Discord (https://discordapp.com/) as message in a channel.
For more information about .NET Core logging API visit Logging in .NET Core and ASP.NET Core and Fundamentals of Logging in .NET Core
Discord Webhook Client
.NET Standard 2.0+
For more information about suported versions visit https://docs.microsoft.com/pt-br/dotnet/standard/net-standard
Install-Package logger-discord-provider
dotnet add package logger-discord-provider
This sample code shows how to add Discord Logger Provider on a ASP.NET Core API project (Startup.cs file):
using JNogueira.Logger.Discord;
namespace My.Sample.Code
{
public IConfiguration Configuration { get; }
public class Startup
{
...
// Add parameters of type ILoggerFactory and IHttpContextAccessor
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor)
{
loggerFactory
// Add "AddDiscord" method with desired options.
.AddDiscord(new DiscordLoggerOptions(Configuration["WebhookUrl"])
{
ApplicationName = "Application Name Test",
EnvironmentName = "Name of environment",
UserName = "Bot username",
// Convert claims user principal values to Discord fields
UserClaimValueToDiscordFields = new List<UserClaimValueToDiscordField> { new UserClaimValueToDiscordField(ClaimTypes.NameIdentifier, "Name identifier"), new UserClaimValueToDiscordField(ClaimTypes.Name, "Name") }
});
app.UseMvc();
}
}
}
This sample code shows how to add Discord Logger on a ASP.NET Core API controller:
using Microsoft.Extensions.Logging;
namespace My.Sample.Code
{
public class TodoController : Controller
{
private readonly ILogger _logger;
public TodoController(ITodoRepository todoRepository, ILogger<TodoController> logger)
{
_logger = logger;
}
public IActionResult SayHello()
{
...
// Call "LogInformation" to sendo log messages to Discord channel
_logger.LogInformation("Hello! This is a sample Discord message sent by ASP.NET Core application!");
...
}
}
}
Trace
_logger.LogTrace("My trace message is here!");
Debug
_logger.LogDebug("My debug message is here!");
Information
_logger.LogInformation("My information message is here!");
Warning
_logger.LogWarning("My warning message is here!");
Error
_logger.LogError("My error message is here!");
Critical
_logger.LogCritical("My critical message is here!");
Handle an exception!
The attachment file "exception-details.txt" contains more exception details like base exception, stack trace content, exception type, exception extra data information.
try
{
var i = 0;
var x = 5 / i;
}
catch (Exception ex)
{
ex.Data["Extra info 1"] = "Extra info 1 value";
ex.Data["Extra info 2"] = "Extra info 2 value";
_logger.LogError(ex, "A exception is handled!");
}