You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ReadMe.md
+11-29Lines changed: 11 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1131,6 +1131,8 @@ app.Run(args);
1131
1131
1132
1132
When passing to a lambda expression or method, the `[FromServices]` attribute is used to distinguish it from command parameters. When passing a class, Constructor Injection can be used, resulting in a simpler appearance. Lambda, method, constructor, filter, etc, all DI supported parameter also supports `[FromKeyedServices]`.
1133
1133
1134
+
`ConfigureServices` is called after command routing is completed and just before the actual parameter parsing process begins, and the generated ServiceProvider is set to the static `ConsoleApp.ServiceProvider`. If you set it without calling `ConfigureServices`, you can use DI without needing to reference `Microsoft.Extensions.DependencyInjection`.
1135
+
1134
1136
Let's try injecting a logger and enabling output to a file. The libraries used are Microsoft.Extensions.Logging and [Cysharp/ZLogger](https://github.com/Cysharp/ZLogger/) (a high-performance logger built on top of MS.E.Logging). If you are referencing `Microsoft.Extensions.Logging`, you can call `ConfigureLogging` from `ConsoleAppBuilder`.
1135
1137
1136
1138
```csharp
@@ -1157,44 +1159,23 @@ public class MyCommand(ILogger<MyCommand> logger)
1157
1159
}
1158
1160
```
1159
1161
1160
-
For building an `IServiceProvider`, `ConfigureServices/ConfigureLogging` uses `Microsoft.Extensions.DependencyInjection.ServiceCollection`. If you want to set a custom ServiceProvider or a ServiceProvider built from Host, or if you want to execute DI with `ConsoleApp.Run`, set it to `ConsoleApp.ServiceProvider`.
`ConsoleApp` has replaceable default logging methods `ConsoleApp.Log` and `ConsoleApp.LogError` used for Help display and exception handling. If using `ILogger<T>`, it's better to replace these as well.
1162
+
`ConsoleApp` has replaceable default logging methods `ConsoleApp.Log` and `ConsoleApp.LogError` used for Help display and exception handling. If using `ILogger<T>`, it's better to replace these as well. `PostConfigureServices(Action<IServiceProvider> configure)` allows you to change own logger.
> I don't recommend using `ConsoleApp.Log` and `ConsoleApp.LogError` directly as an application logging method, as they are intended to be used as output destinations for internal framework output.
1196
1175
> For error handling, it would be better to define your own custom filters for error handling, which would allow you to record more details when handling errors.
1197
1176
1177
+
> Since the help display is executed before routing, `ConfigureServices` is not called. In other words, it is impossible to change the logger for the help display using this method.
1178
+
1198
1179
DI can also be effectively used when reading application configuration from `appsettings.json`. For example, suppose you have the following JSON file.
1199
1180
1200
1181
```json
@@ -1275,6 +1256,7 @@ internal class ServiceProviderScopeFilter(IServiceProvider serviceProvider, Cons
0 commit comments