Giter Club home page Giter Club logo

logging-library's Introduction

Logging Library API NuGet

This library exposes the minimum API for the logging library as interfaces. If you wish to create your own logging utilities, pipes, managers, etc. you should use this API. You can install it using NuGet, and for its usage, please refer to the Logging Library in this repository.

Logging Library NuGet

This library is the core of the functionality for logging. You can install it using NuGet.

Usage

There are many ways this library can be used.
You can instantiate a pipe directly and log to console (ConsolePipe) or to a file (FilePipe).
You can instantiate a logger directly to log to multiple pipes of your chosing at once (DefaultLogger).
Or you can use the LogManager class and let the library handle all the instantiation for you.
Due to this, I will be focusing on the main case of just letting the LogManager class handle all the instantiation.

If you do not wish to configure the outputs

If you do not wish to configure where your logs will go to, you can directly call the following example methods to log with the preset log levels:

LogManager.Trace(object message);
LogManager.Debug(object message);
LogManager.Information(object message);
LogManager.Warning(object message);
LogManager.Error(object message);
LogManager.Fatal(object message);

All of these log levels are documented for their expected purpose, but you are free to use them however you see fit.
Additionally, if you implement a custom log level, or wish to specify an exact level yourself without the helper methods, you can use LogManager.Write(object message, ILogLevel level)

The reason all of these methods use object for the messages, rather than string is to have a similar functionality to Console.WriteLine(), to which you can pass an exception object, and the .NET runtime will still write the exception to console, rather than needing developers to call .ToString() on any object they wish to log to console.

The LogManager is a static class, so no instantiation is needed, but this class instantiates an ILoggerManager (specifically DefaultLoggerManager) in the field LoggerManager which handles creating new Loggers for each assembly, linking a logger to a calling assembly, and constructing any new pipes and loggers.
This field can be overwritten if you wish to extend which ILogPipes the library can create, as well as any internal functionality for loggers.

If you do wish to configure the outputs

When not configuring the logger for your assembly before calling it, the pipes will limit their output to the Information level.
This might not be desired, or you might want to limit output to one pipe, or have multiple file outputs for each log level, so this is where you can call the following method:

LogManager.UpdateConfiguration(ILoggerConfiguration configuration);

With this method, you can update the ILogger instance that is linked to your Assembly and change which pipes it uses, the maximum and minimum log levels for each pipe, as well as the log format for each pipe.

To construct a configuration, you will have to create a few classes, starting with a class that inherits from ILoggerConfiguration:

public class LoggerConfiguration : ILoggerConfiguration
{
    
    public List<IPipeConfiguration> PipeSettings =>
    [
        new ConsolePipeConfiguration(),
        new FilePipeConfiguration()
    ];

    public sealed class ConsolePipeConfiguration : DefaultConsolePipeConfiguration
    {
        public override byte MaxLogLevel => LogLevel.Debug.Level;
    }

    public sealed class FilePipeConfiguration : DefaultFilePipeConfiguration
    {
        public override byte MaxLogLevel => LogLevel.Debug.Level;
    }
}

In this configuration class, you can set which pipes to use by using a new IPipeConfiguration and setting PipeName to the name of the class you wish to use. In the example, we want to increase the log level to allow Debug level logs to be written to both the ConsolePipe and the FilePipe without changing any of the other values, so we use their default configuration types and we override MaxLogLevel.

logging-library's People

Contributors

pustalorc avatar

Stargazers

Paradox avatar Leonid avatar Enes Sadık Özbek avatar  avatar

Watchers

 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.