Code Monkey home page Code Monkey logo

spectre.console.extensions.logging's Introduction

Spectre.Cli.Extensions.DependencyInjection

A highly opioninated logger implementation for Microsoft.Extensions.Logging that uses Spectre.Console to write to the console.

Getting started

Once you've installed both Spectre.Console and this package, just add the SpectreConsoleLogger using the normal Add extension methods. If you're creating loggers manually:

using (var factory = LoggerFactory.Create(b => b.AddSpectreConsole())) {
    _logger = factory.CreateLogger("SampleCategory");
}

Or if you're using dependency injection:

//in your startup code
services.AddLogging(builder => builder.AddSpectreConsole());

//in your classes
public MyClass(ILogger<MyClass> logger) {
    _logger = logger;
}

Since we're using Spectre.Console underneath, that means you can also include markup in your log messages and they will be rendered on the console:

_logger.LogWarning($"No files found at [italic red]{path}[/]!. Continuing with [underline]default files only.[/]");

Obviously your other logging providers may not support this markup!

Inline Logger

There's also an alternative logger implementation that sacrifices some detail for a terser, more human-readable output: SpectreInlineLogger. You can use this version by changing the call to AddSpectreConsole() to AddInlineSpectreConsole(). This (highly opinionated!) implementation is intended less as an application logging facility and more as a console log/host for command-line apps.

Configuration

There's also some configuration options available from the SpectreConsoleLoggerConfiguration class. You can optionally pass an instance of the configuration, or a configuration action, in the call to AddSpectreConsole().

Examples

To see Spectre.Console in action, install the dotnet-example global tool.

> dotnet tool install -g dotnet-example

Now you can list available examples in this repository:

> dotnet example

┌─────────┬────────────────────────────────┬──────────────────────────────────────────────────────────────┐
│ Name    │ Path                           │ Description                                                  │
├─────────┼────────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Default │ samples/Default/Default.csproj │ Demonstrates the default output of the SpectreConsoleLogger. │
│ Inline  │ samples/Inline/Inline.csproj   │ Demonstrates the terser output of the SpectreInlineLogger.   │
└─────────┴────────────────────────────────┴──────────────────────────────────────────────────────────────┘

And to run an example:

> dotnet example default
info: SampleCategory[0]
      Sample application starting up...
trce: SampleCategory[1234]
      Use a familiar format for logging messages
...

spectre.console.extensions.logging's People

Contributors

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