Giter VIP home page Giter VIP logo

glowing-telegram's Introduction

DbLogger

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)

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(MyExeception, "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
)

Migrations to v2.0.0

ALTER TABLE [dbo].[Logs] ADD [ExceptionType] 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.