Skip to content

ConnyConsole is an example console CLI project that uses DI, Serilog for logging, execution cancellation and argument parsing based on Microsoft's System.CommandLine library.

License

Notifications You must be signed in to change notification settings

HaGGi13/ConnyConsole

Repository files navigation

Build and analyze Quality Gate Status

ConnyConsole

ConnyConsole is a console CLI project that uses System.CommandLine from Microsoft for argument parsing to collect some experience with this library.

Table of content

Features

This chapter gives a rough overview what was implemented as example implementation.

Business code

Please note that some the features listed are based on or inspired by the article series A Beginner's Guide to .NET's HostBuilder: Part 1 and following.

  • Console startup implemented with Host.CreateDefaultBuilder that enables/contains following:
    • Dependency injection configuration via ConfigureServices and extension method to keep Program.cs simple;
      • Own extension method AddConfiguration registers all dependencies incl.:
        • Logger configuration with injected configuration;
        • Configuration registered for options pattern;
    • Loads configuration from specific subdirectory Config that contains appsettings.json and appsettings.Development.json:
      • Current environment resolved from injected HostBuilderContext to use related appsettings.json file;
      • appsettings files intentionally in subdirectory Config to have to define them specifically for loading, to cover case as example;
  • Serilog set up as logger:
    • Startup-logger and injectable logger based on configuration files;
    • Logging into on console and file in defined format;
    • Serilog can throw strange/not relatable exceptions during configuration, when JSON config is wrong, such exceptions will be printed on console;
  • Current dummy logic executed async with graceful and enforceable cancellation;
    • First [Ctrl] + [C] or [Ctrl] + [Break] initiates graceful cancellation;
      • Waits until logic finishes or configurable timeout reached and closes application;
    • Second [Ctrl] + [C] or [Ctrl] + [Break] initiates immediate enforced cancellation;
      • Application exists immediately;
    • All that magic happens in ConsoleCancellationTokenSource class;
    • Console cancellation event is registered in App class;
  • Console application icon defined (check *.csproj file);

Test code

As an application is only as good as it was tested, this chapter gives some insights how the console application tests were implemented.

  • Unit tests implemented with following libraries/frameworks:
  • Graceful + enforced cancellation are tested with simulated [Ctrl] + [C] keys pressed (ConsoleCancellationTokenSourceTests.cs);
  • Dependency injection extension method incl. lifetime checks;
    • Lifetime check helps to notice fast if a lifetime was changed by accident or just to highlight that it was changed in general;
  • Async dummy logic execution;

Pipeline

This chapter provides an overview what the current build pipeline provides.

  • GitVersion integrated for auto SemVer (Semantic Versioning) based on Git history;
  • SonarQube (cloud, free plan) integrated;
  • Build console application with Release configuration;
  • Run tests;
    • Collect test run results as *.trx file;
    • Collect code coverage in OpenCover format, later on used to publish on SonarQube;
    • Both files, coverage file and test result file, are published as build artifacts;
    • Passed, failed and skipped tests listed as part of run summary, realized with GitHubActionsTestLogger package;

References/documentation:

Following are some used articles listed.

About

ConnyConsole is an example console CLI project that uses DI, Serilog for logging, execution cancellation and argument parsing based on Microsoft's System.CommandLine library.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages