A robust template for building .NET console applications with modern development best practices, including dependency injection, configuration management, logging, and extensible command-line parsing.
ConsoleAppTemplate provides a solid foundation for .NET console applications. It leverages best practices such as structured logging (via Serilog), dependency injection, configuration via JSON files, and powerful command-line parsing (with McMaster.Extensions.CommandLineUtils).
This template is designed for scalability, maintainability, and ease-of-use, whether for small scripts or complex automation tools.
- Command Line Parsing: Easily define commands, subcommands, and options.
- Structured Logging: Integrated Serilog with console and file sinks, plus support for enrichment.
- Dependency Injection: Built-in .NET DI with easy configuration.
- Flexible Configuration: Supports single or environment-specific JSON configuration files.
- Error Handling: Robust error catching and exit codes for integration in automation pipelines.
- Extensible: Easily add new commands, services, or configuration sections.
- .NET 6.0 SDK or later
- (Optional) Visual Studio or VS Code
Install the template from NuGet:
dotnet new install <PackageID>Replace
<PackageID>with the actual NuGet package identifier for ConsoleAppTemplate.
Create a new project using this template:
dotnet new console-app-template -n MyConsoleApp
cd MyConsoleAppReview Next Steps:
After creating your project, be sure to review the Instructions.md file included in the root of your new project directory. This file provides detailed, project-specific setup steps and guidance for customizing your application.
If you create your project using Visual Studio, the
Instructions.mdfile will open automatically to help guide you through initial configuration and customization.
Restore packages and build:
dotnet restore
dotnet buildThe template uses JSON files for application and environment configuration.
- appsettings.json: Main config file (default for all environments)
- appsettings.Development.json, appsettings.Production.json, etc.: Environment-specific files (optional)
The configuration system will load the appropriate file based on the DOTNET_ENVIRONMENT variable.
To set the environment (example for Windows):
set DOTNET_ENVIRONMENT=DevelopmentOr for Linux/macOS:
export DOTNET_ENVIRONMENT=DevelopmentRun the application from the root directory:
dotnet runYou can also publish the app and run the executable:
dotnet publish -c Release
./bin/Release/net6.0/MyConsoleApp.exe [options]The template supports a main command and subcommands. To view help:
dotnet run -- --helpAdd new subcommands by creating classes and registering them in Program.cs.
MyConsoleApp/
├── Program.cs
├── AppSettings.json
├── AppSettings.Development.json
├── AppSettings.Production.json
├── Instructions.md
└── ...
- Program.cs: Entry point with main logic and configuration.
- AppSettings.json*: Application configuration files.
- Instructions.md: In-depth development and customization notes.
- Define your main command in
Program.csusing attributes. - Add subcommands by creating new classes and registering them with
[Subcommand(typeof(MyCommand))].
Example:
[Subcommand(typeof(MyNewCommand))]- Single File: Use
appsettings.jsonfor all environments. - Per Environment: Use
appsettings.{Environment}.jsonfiles.
See Instructions.md for detailed setup.
- Logging is configured via the
Serilogsection in yourAppSettings*.json. - Add or remove log sinks and adjust minimum levels as needed.
- Default logs to both console and file (see
AppSettings.json).
Contributions are welcome! Please fork the repo and submit a pull request for improvements or bug fixes.
- Fork the repository
- Create a new branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/my-feature) - Open a pull request
This project is licensed under the MIT License. See the LICENSE file for details.
- Serilog Documentation
- McMaster.Extensions.CommandLineUtils
- Microsoft.Extensions.Hosting
- .NET Generic Host
For questions, please open an issue on GitHub.
For more in-depth developer notes and template usage, see [src/ConsoleAppTemplate/Content/Instructions.md](src/ConsoleAppTemplate/Content/Instructions.md).