11using System ;
22using System . Threading . Tasks ;
3+ using App . Extensions ;
34using Lib . Configuration ;
45using Lib . Proxies ;
56using Microsoft . Extensions . Configuration ;
67using Microsoft . Extensions . DependencyInjection ;
78using Microsoft . Extensions . Hosting ;
8- using Microsoft . Extensions . Logging ;
99using Microsoft . Extensions . Options ;
1010
1111namespace App
@@ -14,62 +14,39 @@ public static class Program
1414 {
1515 public static async Task Main ( string [ ] args )
1616 {
17- var hostBuilder = new HostBuilder ( ) ;
18- hostBuilder
17+ using var host = CreateHostBuilder ( args ) . Build ( ) ;
18+ await host . RunAsync ( ) ;
19+ }
20+
21+ public static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
22+ Host . CreateDefaultBuilder ( args )
1923 . ConfigureWebJobs ( builder =>
2024 {
2125 builder . AddAzureStorageCoreServices ( ) ;
22- builder . AddAzureStorage ( ) ;
2326 builder . AddTimers ( ) ;
2427 } )
25- . ConfigureAppConfiguration ( builder =>
28+ . ConfigureAppConfiguration ( ( _ , config ) =>
2629 {
27- builder . AddCommandLine ( args ) ;
28- builder . AddEnvironmentVariables ( ) ;
29- var environment = Environment . GetEnvironmentVariable ( "ENVIRONMENT" ) ?? "DEV" ;
30- builder . AddJsonFile ( "appsettings.json" , optional : true , reloadOnChange : true ) ;
31- builder . AddJsonFile ( $ "appsettings.{ environment } .json", optional : false , reloadOnChange : true ) ;
30+ config . AddJsonFile ( ) ;
31+ config . AddUserSecrets ( ) ;
32+ config . AddEnvironmentVariables ( ) ;
33+ config . AddCommandLine ( args ) ;
3234 } )
33- . ConfigureLogging ( ( context , builder ) =>
35+ . ConfigureLogging ( ( hostingContext , loggingBuilder ) =>
3436 {
35- builder . AddNonGenericLogger ( ) ;
36- builder . AddConsole ( options =>
37- {
38- options . DisableColors = false ;
39- options . TimestampFormat = "[HH:mm:ss:fff] " ;
40- } ) ;
41- builder . AddConfiguration ( context . Configuration . GetSection ( "Logging" ) ) ;
42- var instrumentationKey = context . Configuration [ "Settings:ApplicationInsights:InstrumentationKey" ] ;
43- builder . AddApplicationInsightsWebJobs ( options => options . InstrumentationKey = instrumentationKey ) ;
37+ loggingBuilder . AddLogging ( hostingContext . Configuration ) ;
4438 } )
45- . ConfigureServices ( ( context , services ) =>
39+ . ConfigureServices ( ( hostingContext , services ) =>
4640 {
47- services . Configure < Settings > ( context . Configuration . GetSection ( nameof ( Settings ) ) ) ;
41+ services . Configure < Settings > ( hostingContext . Configuration . GetSection ( " Settings" ) ) ;
4842 services . AddHttpClient < IApiProxy , ApiProxy > ( )
4943 . ConfigureHttpClient ( ( provider , client ) =>
5044 {
51- var settings = provider . GetService < IOptions < Settings > > ( ) . Value ;
52- var baseAddress = settings . ExternalApi . BaseAddress ;
45+ var options = provider . GetRequiredService < IOptions < Settings > > ( ) ;
46+ var baseAddress = options . Value . ExternalApi . BaseAddress ;
5347 client . BaseAddress = new Uri ( baseAddress ) ;
5448 client . DefaultRequestHeaders . Add ( "Accept" , "application/json" ) ;
5549 } ) ;
5650 } ) ;
57-
58- var host = hostBuilder . Build ( ) ;
59- using ( host )
60- {
61- await host . RunAsync ( ) ;
62- }
63- }
64-
65- private static void AddNonGenericLogger ( this ILoggingBuilder loggingBuilder )
66- {
67- var services = loggingBuilder . Services ;
68- services . AddSingleton ( serviceProvider =>
69- {
70- var loggerFactory = serviceProvider . GetRequiredService < ILoggerFactory > ( ) ;
71- return loggerFactory . CreateLogger ( "ContinuousWebJobDemo" ) ;
72- } ) ;
73- }
7451 }
7552}
0 commit comments