Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

uniteeio/glowing-telegram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Glowing telegram

ILogger that logs into a mssql database.

$ dotnet add package glowing-telegram

How to use

Add the logger to the Program.cs.

builder.Logging.ClearProviders();
builder.Logging.AddDbLogger(configuration =>
{
    configuration.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
});

Configuration

configuration.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
configuration.MaxDays = 7; // Keep the logs for 7 days (-1 = never delete logs)
configuration.ServiceName = "MyService"; // The name of the service
configuration.MinimumLogLevel = LogLevel.Warning; // Minimum log level

In Azure Function

[assembly: FunctionsStartup(typeof(Namespace.Function.Startup))]
namespace Housebase.Function;

public class Startup : FunctionsStartup
{
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
       // ...
    }


    public override void Configure(IFunctionsHostBuilder builder)
    {
        var configuration = builder.GetContext().Configuration;
        var dbConn = configuration.GetSection("ConnectionStrings").GetValue<string>("Db");

        builder.Services.AddLogging(builder =>
        {
            builder.AddDbLogger(configuration =>
            {
                configuration.ConnectionString = dbConn;
                configuration.MaxDays = 7;
                configuration.ServiceName = "MyService";
            });
        });
    }
}

Use the logger

Use this logger like any other.

Inject in the constructor using dependency injection:

private readonly ILogger<MyService> _logger;
public MyService(ILogger<MyService> logger)
{
    _logger = logger;
}
_logger.Information("Hello {Name}", "World");

Add extra info the the logs

Use the special Extra property of the ApplicationException to add extra options.

var MyException = new ApplicationException("Oops something went wrong", new { myExtraProperty = "MyValue" });
_logger.LogError(MyException, "Something went wrong", null);

Use the global handler to log all exceptions

There is a global handler available that is able to log all exceptions coming from the requests.

app.UseMiddleware<ErrorHandlerMiddleware>();

This middleware comes with a default implementation that add headers and claims as an extra info to the logs.

You can change the information logged by implementing your own delegate:

Func<HttpContext, object> myImplementation = delegate(HttpContext context)
{
    var myValue = "Hello World"
    return new 
    {
        myProperty = myValue
    };
};

app.UseMiddleware<ErrorHandlerMiddleware>(myImplementation);

Create the table in the database

CREATE TABLE [dbo].[Logs](
	[Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
	[Message] [nvarchar](max) NULL,
	[LogLevel] [nvarchar](50) NULL,
	[Extra] [nvarchar](max) NULL,
	[CreatedAt] [datetime2](7) NULL,
	[StackTrace] [nvarchar](max) NULL,
	[ExceptionType] [nvarchar](max) NULL,
	[ServiceName] [nvarchar](max) NULL,
)

Migrations to v2.0.0

ALTER TABLE [dbo].[Logs] ADD [ExceptionType] NVARCHAR(max)

Migrations to v3.0.0

ALTER TABLE [dbo].[Logs] ADD [ServiceName] NVARCHAR(max)

Join with your tables to extract data

SELECT TOP(20) * FROM Logs L JOIN Users U ON JSON_VALUE(L.Extra, '$.claims.userId') = U.Id WHERE U.Id = 25

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages