/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/commandline/snippets/dependency-injection/csharp/Program.cs
51 строка
2 KB
Pontus Öwre
add using statement to dispose loggerFactory to ensure logs are flushed properly (#35392)
05 окт 2023, 21:53
Не верифицирован
05 окт 2023, 21:53
1ddc436
Код
Авторство
О чём код?
// <all> using System.CommandLine; using System.CommandLine.Binding; using Microsoft.Extensions.Logging; class Program { static async Task Main(string[] args) { var fileOption = new Option<FileInfo?>( name: "--file", description: "An option whose argument is parsed as a FileInfo"); var rootCommand = new RootCommand("Dependency Injection sample"); rootCommand.Add(fileOption); // <sethandler> rootCommand.SetHandler(async (fileOptionValue, logger) => { await DoRootCommand(fileOptionValue!, logger); }, fileOption, new MyCustomBinder()); // </sethandler> await rootCommand.InvokeAsync("--file scl.runtimeconfig.json"); } public static async Task DoRootCommand(FileInfo aFile, ILogger logger) { Console.WriteLine($"File = {aFile?.FullName}"); logger.LogCritical("Test message"); await Task.Delay(1000); } // <binderclass> public class MyCustomBinder : BinderBase<ILogger> { protected override ILogger GetBoundValue( BindingContext bindingContext) => GetLogger(bindingContext); ILogger GetLogger(BindingContext bindingContext) { using ILoggerFactory loggerFactory = LoggerFactory.Create( builder => builder.AddConsole()); ILogger logger = loggerFactory.CreateLogger("LoggerCategory"); return logger; } } // </binderclass> } // </all>