/logger-discord-provider

A .NET Core Logger provider to send log messages to Discord

Primary LanguageC#

Discord .NET Logger Provider

AppVayor - Build status NuGet NuGet

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

Target

Discord Webhook Client
.NET Standard 2.0+

For more information about suported versions visit https://docs.microsoft.com/pt-br/dotnet/standard/net-standard

Installation

NuGet

Install-Package logger-discord-provider

.NET CLI

dotnet add package logger-discord-provider

Configuration

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();
        }
    }
}

How to logging

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!");
            
            ...
        }
    }
}

Message types

Trace

_logger.LogTrace("My trace message is here!");

Trace message

Debug

_logger.LogDebug("My debug message is here!");

Debug message

Information

_logger.LogInformation("My information message is here!");

Debug message

Warning

_logger.LogWarning("My warning message is here!");

Warning message

Error

 _logger.LogError("My error message is here!");

Error message

Critical

 _logger.LogCritical("My critical message is here!");

Error message

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!");
}

Error message