Code Monkey home page Code Monkey logo

jstalnac.common.logging's Introduction

JStalnac.Common.Logging

A small logging utility for .Net C#

One file, easy to include!

Allows logging on different levels and also to a file.

Requirements

  • .NET Standard 2.1
  • Pastel
    • Install via
      • .Net CLI:
        • dotnet add package Pastel
      • Package Manager
        • Install-Package Pastel

Usage

Create a new Logger:

// Like this
var logger = Logger.GetLogger("My Logger");
// or this
Logger.GetLogger(this);
// or like this
new Logger("My Logger");

Writing a log message

// Through a shortcut
logger.Info("Hello World!");

// Or directly
logger.Write("Hello World!", LogLevel.Information);

// You can also write exceptions
try
{
    throw new Exception("A very serious error");
}
catch (Exception ex)
{
    logger.Error("An error occured: ", ex);
}

// Objects through their own ToString() methods
logger.Info(1);

// Log levels that are too low won't be logged
logger.Debug("Shh");

// Output:
//    [04/07/2020 16:51:14Z+03:00] [Main] [Information] Hello World!
//    [04/07/2020 16:51:14Z+03:00] [Main] [Information] Hello World!
//    [04/07/2020 16:51:14Z+03:00] [Main] [Error] An error occured:
//    [04/07/2020 16:51:14Z+03:00] [Main] [Error] System.Exception: A very serious error
//    [04/07/2020 16:51:14Z+03:00] [Main] [Error]    at LogTest.Program.Main(String[] args) in ...
//    [04/07/2020 16:51:14Z+03:00] [Main] [Information] 1

Set the minumum log level

Logger.SetLogLevel(LogLevel.Information);

Set log file

Logger.SetLogFile("logs/log.txt");

Set datetime format

Logger.SetDateTimeFormat("G");

logger.Info("Bye bye!");

// Output:
//    [07/04/2020 17:00:13] [Main] [Information] Bye bye!
// Funny that you can see when I wrote this

Creating a new console window (Windows only)

Useful in GUI applications that don't have consoles.

Logger.CreateConsole();

Using Microsoft.Extensions.Logging

Example program

// using ...

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create host
            using (var host = CreateHostBuilder().Build())
            {
                var logger = Logger.GetLogger<Program>();

                // Start
                logger.Info("Starting host");
                host.Start();

                // Get logger
                var log = host.Services.GetRequiredService<ILogger<Program>>();
                log.LogInformation("Hello World!");

                host.WaitForShutdown();
            }
        }

        public static IHostBuilder CreateHostBuilder()
            => Host.CreateDefaultBuilder()
                .ConfigureLogging(options =>
                {
                    // Clear the default logger provider
                    options.ClearProviders();
                    // Add custom logging
                    options.AddJStalnacLogging();
                });
    }
}

// Output:
//      [16/10/2020 17:57:36Z+03:00] [Program] [Info] Starting host
//      [16/10/2020 17:57:36Z+03:00] [Microsoft.Hosting.Lifetime] [Info] Application started. Press Ctrl+C to shut down.
//      [16/10/2020 17:57:36Z+03:00] [Microsoft.Hosting.Lifetime] [Info] Hosting environment: Production
//      [16/10/2020 17:57:36Z+03:00] [Microsoft.Hosting.Lifetime] [Info] Content root path: ...
//      [16/10/2020 17:57:36Z+03:00] [Test.Program] [Info] Hello World!
//      [16/10/2020 17:57:42Z+03:00] [Microsoft.Hosting.Lifetime] [Info] Application is shutting down...

jstalnac.common.logging's People

Contributors

jstalnac avatar

Watchers

 avatar  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.