|
| 1 | +# ConsoleAppTemplate |
| 2 | + |
| 3 | +A robust template for building .NET console applications with modern development best practices, including dependency injection, configuration management, logging, and extensible command-line parsing. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Overview](#overview) |
| 10 | +- [Features](#features) |
| 11 | +- [Getting Started](#getting-started) |
| 12 | + - [Prerequisites](#prerequisites) |
| 13 | + - [Installation](#installation) |
| 14 | + - [Configuration](#configuration) |
| 15 | +- [Usage](#usage) |
| 16 | +- [Project Structure](#project-structure) |
| 17 | +- [Customization](#customization) |
| 18 | + - [Commands and Subcommands](#commands-and-subcommands) |
| 19 | + - [Configuration Files](#configuration-files) |
| 20 | + - [Logging](#logging) |
| 21 | +- [Contributing](#contributing) |
| 22 | +- [License](#license) |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +**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). |
| 29 | + |
| 30 | +This template is designed for scalability, maintainability, and ease-of-use, whether for small scripts or complex automation tools. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Features |
| 35 | + |
| 36 | +- **Command Line Parsing:** Easily define commands, subcommands, and options. |
| 37 | +- **Structured Logging:** Integrated Serilog with console and file sinks, plus support for enrichment. |
| 38 | +- **Dependency Injection:** Built-in .NET DI with easy configuration. |
| 39 | +- **Flexible Configuration:** Supports single or environment-specific JSON configuration files. |
| 40 | +- **Error Handling:** Robust error catching and exit codes for integration in automation pipelines. |
| 41 | +- **Extensible:** Easily add new commands, services, or configuration sections. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Getting Started |
| 46 | + |
| 47 | +### Prerequisites |
| 48 | + |
| 49 | +- [.NET 6.0 SDK or later](https://dotnet.microsoft.com/en-us/download) |
| 50 | +- (Optional) [Visual Studio](https://visualstudio.microsoft.com/) or [VS Code](https://code.visualstudio.com/) |
| 51 | + |
| 52 | +### Installation |
| 53 | + |
| 54 | +Install the template from NuGet: |
| 55 | + |
| 56 | +```sh |
| 57 | +dotnet new install <PackageID> |
| 58 | +``` |
| 59 | +> Replace `<PackageID>` with the actual NuGet package identifier for ConsoleAppTemplate. |
| 60 | +
|
| 61 | +Create a new project using this template: |
| 62 | + |
| 63 | +```sh |
| 64 | +dotnet new console-app-template -n MyConsoleApp |
| 65 | +cd MyConsoleApp |
| 66 | +``` |
| 67 | + |
| 68 | +**Review Next Steps:** |
| 69 | +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. |
| 70 | +> If you create your project using Visual Studio, the `Instructions.md` file will open automatically to help guide you through initial configuration and customization. |
| 71 | +
|
| 72 | +Restore packages and build: |
| 73 | + |
| 74 | +```sh |
| 75 | +dotnet restore |
| 76 | +dotnet build |
| 77 | +``` |
| 78 | + |
| 79 | +### Configuration |
| 80 | + |
| 81 | +The template uses JSON files for application and environment configuration. |
| 82 | + |
| 83 | +- **appsettings.json**: Main config file (default for all environments) |
| 84 | +- **appsettings.Development.json, appsettings.Production.json, etc.**: Environment-specific files (optional) |
| 85 | + |
| 86 | +The configuration system will load the appropriate file based on the `DOTNET_ENVIRONMENT` variable. |
| 87 | + |
| 88 | +**To set the environment (example for Windows):** |
| 89 | +```sh |
| 90 | +set DOTNET_ENVIRONMENT=Development |
| 91 | +``` |
| 92 | +Or for Linux/macOS: |
| 93 | +```sh |
| 94 | +export DOTNET_ENVIRONMENT=Development |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Usage |
| 100 | + |
| 101 | +Run the application from the root directory: |
| 102 | + |
| 103 | +```sh |
| 104 | +dotnet run |
| 105 | +``` |
| 106 | + |
| 107 | +You can also publish the app and run the executable: |
| 108 | + |
| 109 | +```sh |
| 110 | +dotnet publish -c Release |
| 111 | +./bin/Release/net6.0/MyConsoleApp.exe [options] |
| 112 | +``` |
| 113 | + |
| 114 | +### Command Line Options |
| 115 | + |
| 116 | +The template supports a main command and subcommands. To view help: |
| 117 | + |
| 118 | +```sh |
| 119 | +dotnet run -- --help |
| 120 | +``` |
| 121 | + |
| 122 | +Add new subcommands by creating classes and registering them in `Program.cs`. |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## Project Structure |
| 127 | + |
| 128 | +``` |
| 129 | +MyConsoleApp/ |
| 130 | +├── Program.cs |
| 131 | +├── AppSettings.json |
| 132 | +├── AppSettings.Development.json |
| 133 | +├── AppSettings.Production.json |
| 134 | +├── Instructions.md |
| 135 | +└── ... |
| 136 | +``` |
| 137 | + |
| 138 | +- **Program.cs**: Entry point with main logic and configuration. |
| 139 | +- **AppSettings*.json**: Application configuration files. |
| 140 | +- **Instructions.md**: In-depth development and customization notes. |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## Customization |
| 145 | + |
| 146 | +### Commands and Subcommands |
| 147 | + |
| 148 | +- Define your main command in `Program.cs` using attributes. |
| 149 | +- Add subcommands by creating new classes and registering them with `[Subcommand(typeof(MyCommand))]`. |
| 150 | + |
| 151 | +Example: |
| 152 | +```csharp |
| 153 | +[Subcommand(typeof(MyNewCommand))] |
| 154 | +``` |
| 155 | + |
| 156 | +### Configuration Files |
| 157 | + |
| 158 | +- **Single File**: Use `appsettings.json` for all environments. |
| 159 | +- **Per Environment**: Use `appsettings.{Environment}.json` files. |
| 160 | + |
| 161 | +See [Instructions.md](src/ConsoleAppTemplate/Content/Instructions.md) for detailed setup. |
| 162 | + |
| 163 | +### Logging |
| 164 | + |
| 165 | +- Logging is configured via the `Serilog` section in your `AppSettings*.json`. |
| 166 | +- Add or remove log sinks and adjust minimum levels as needed. |
| 167 | +- Default logs to both console and file (see `AppSettings.json`). |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Contributing |
| 172 | + |
| 173 | +Contributions are welcome! Please fork the repo and submit a pull request for improvements or bug fixes. |
| 174 | + |
| 175 | +1. Fork the repository |
| 176 | +2. Create a new branch (`git checkout -b feature/my-feature`) |
| 177 | +3. Commit your changes (`git commit -am 'Add new feature'`) |
| 178 | +4. Push to the branch (`git push origin feature/my-feature`) |
| 179 | +5. Open a pull request |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## License |
| 184 | + |
| 185 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## References & Further Reading |
| 190 | + |
| 191 | +- [Serilog Documentation](https://serilog.net/) |
| 192 | +- [McMaster.Extensions.CommandLineUtils](https://github.com/natemcmaster/CommandLineUtils) |
| 193 | +- [Microsoft.Extensions.Hosting](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting) |
| 194 | +- [.NET Generic Host](https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host) |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## Support |
| 199 | + |
| 200 | +For questions, please open an issue on GitHub. |
| 201 | + |
| 202 | +``` |
| 203 | +For more in-depth developer notes and template usage, see [src/ConsoleAppTemplate/Content/Instructions.md](src/ConsoleAppTemplate/Content/Instructions.md). |
| 204 | +``` |
0 commit comments