GithubHelp home page GithubHelp logo

bit-logger's Introduction

Build server Platform Build status
AppVeyor Windows Build status
Travis Linux / macOS Build Status

Coverage Status NuGet Version NuGet Downloads License

Logger for .NET Core

Logger for .NET Core Apps that enables logging to different sources

One logger to log them all

Console

If the code that implements the logger uses a console messages can be sent to it

Database

A SQLite database is created to save the records in the path where the assembly is run

File

A file is created every hour to keep small files in the path where the assembly is run

Custom

You can create custom sources to send the messages to a different output of your preference

Configurable

All the provided sources and the ones you may create can be configured to only show specific data according to the needs of your business

How to use with default sources

class Program
{
    // Inject in a service provider
    static void Main(string[] args)
    {
        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        // Create service provider
        var serviceProvider = serviceCollection.BuildServiceProvider();

        // Run app
        serviceProvider.GetService<Test>().It();
    }

    private void ConfigureServices()
    {
        // Build logger
        ILogger logger = new Logger()
            .AddConsoleSource()
            .AddDatabaseSource()
            .AddFileSource();

        // Add access to generic ILogger
        serviceCollection.AddSingleton(logger);

        // Add the Test class
        serviceCollection.AddTransient<Test>();
    }
}

// Somewhere else in the code

internal class Test
{
    private readonly ILogger logger;

    internal Test(ILogger logger) => this.logger = logger;

    internal void It() => logger.Information("sample");
}

// Call it
var args = new string[] {};
Program.Main(args);

// this should produce an output to a file, database and console and display a message like this
// <INFORMATION> 2019-03-06 23:02:56 [Test::It] sample

How the Log Levels work

// This is the Level enum
public enum Level
{
    Trace = 0,
    Debug = 1,
    Verbose = 2,
    Information = 3,
    Warning = 4,
    Error = 5,
    Critical = 6,
    None = 7
}

public void Test()
{
    var consoleConfiguration = new Configuration
    {
        Level = Level.Trace // Do not ignore any level and log all messages
    };

    var databaseConfiguration = new Configuration
    {
        Level = Level.Warning // Ignore any lower levels and only log messages equal or greater than Warning
    };

    var fileConfiguration = new Configuration
    {
        Level = Level.Critical // Ignore any lower levels and only log Critical messages 
    };

    // Build logger
    ILogger logger = new Logger()
        .AddConsoleSource(consoleConfiguration)
        .AddDatabaseSource(databaseConfiguration)
        .AddFileSource(fileConfiguration);

    logger.Trace("test"); // this will only be sent to the console
    logger.Warning("test"); // this will be sent to the console and the database
    logger.Critical("test"); // this will be sent to the console, the database and the file
}

See the samples

Console application with the 3 default loggers configured

bit-logger's People

Contributors

b1tf8er avatar

Watchers

James Cloos avatar

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.