11using System ;
22using System . IO ;
3- using System . Reflection ;
43using Microsoft . Extensions . Configuration ;
54using Microsoft . Extensions . DependencyInjection ;
65using Microsoft . Extensions . Hosting ;
@@ -34,7 +33,7 @@ public static IHostBuilder UseNLog(this IHostBuilder builder)
3433 /// <param name="builder"></param>
3534 /// <param name="options">NLogProviderOptions object to configure NLog behavior</param>
3635 /// <returns>IHostBuilder for chaining</returns>
37- public static IHostBuilder UseNLog ( this IHostBuilder builder , NLogProviderOptions options )
36+ public static IHostBuilder UseNLog ( this IHostBuilder builder , NLogProviderOptions ? options )
3837 {
3938 Guard . ThrowIfNull ( builder ) ;
4039#if NETSTANDARD2_0
@@ -89,7 +88,7 @@ public static IHostApplicationBuilder UseNLog(this IHostApplicationBuilder build
8988 /// <param name="builder"></param>
9089 /// <param name="options">NLogProviderOptions object to configure NLog behavior</param>
9190 /// <returns>IHostApplicationBuilder for chaining</returns>
92- public static IHostApplicationBuilder UseNLog ( this IHostApplicationBuilder builder , NLogProviderOptions options )
91+ public static IHostApplicationBuilder UseNLog ( this IHostApplicationBuilder builder , NLogProviderOptions ? options )
9392 {
9493 Guard . ThrowIfNull ( builder ) ;
9594 builder. Services . TryAddNLogLoggingProvider ( ( svc , addlogging ) => svc . AddLogging ( addlogging ) , builder . Configuration , options , ( serviceProvider , config , opt ) => CreateNLogLoggerProvider ( serviceProvider , config , builder . Environment , opt ) ) ;
@@ -119,31 +118,33 @@ public static IHostApplicationBuilder UseNLog(this IHostApplicationBuilder build
119118 }
120119#endif
121120
122- private static void AddNLogLoggerProvider( IServiceCollection services , IConfiguration hostConfiguration , IHostEnvironment hostEnvironment , NLogProviderOptions options , Func < IServiceProvider , IConfiguration , IHostEnvironment , NLogProviderOptions , NLogLoggerProvider > factory )
121+ private static void AddNLogLoggerProvider( IServiceCollection services , IConfiguration ? hostConfiguration , IHostEnvironment ? hostEnvironment , NLogProviderOptions ? options , Func < IServiceProvider , IConfiguration ? , IHostEnvironment ? , NLogProviderOptions , NLogLoggerProvider > factory )
123122 {
124123 services. TryAddNLogLoggingProvider ( ( svc , addlogging ) => svc . AddLogging ( addlogging ) , hostConfiguration , options , ( provider , cfg , opt ) => factory ( provider , cfg , hostEnvironment , opt ) ) ;
125124 }
126125
127- private static NLogLoggerProvider CreateNLogLoggerProvider( IServiceProvider serviceProvider , IConfiguration hostConfiguration , IHostEnvironment hostEnvironment , NLogProviderOptions options )
126+ private static NLogLoggerProvider CreateNLogLoggerProvider( IServiceProvider serviceProvider , IConfiguration ? hostConfiguration , IHostEnvironment ? hostEnvironment , NLogProviderOptions options )
128127 {
129128 return serviceProvider. CreateNLogLoggerProvider ( hostConfiguration , options , LogManager . LogFactory ) ;
130129 }
131130
132- private static NLogLoggerProvider CreateNLogLoggerProvider( IServiceProvider serviceProvider , IConfiguration hostConfiguration , IHostEnvironment hostEnvironment , NLogProviderOptions options , LogFactory logFactory )
131+ private static NLogLoggerProvider CreateNLogLoggerProvider( IServiceProvider serviceProvider , IConfiguration ? hostConfiguration , IHostEnvironment ? hostEnvironment , NLogProviderOptions options , LogFactory logFactory )
133132 {
134133 NLogLoggerProvider provider = serviceProvider. CreateNLogLoggerProvider ( hostConfiguration , options , logFactory ) ;
135134
136135 string nlogConfigFile = string . Empty ;
137- string contentRootPath = hostEnvironment ? . ContentRootPath ;
138- string environmentName = hostEnvironment ? . EnvironmentName ;
136+ var contentRootPath = hostEnvironment ? . ContentRootPath ;
137+ var environmentName = hostEnvironment ? . EnvironmentName ;
139138 if ( ! string . IsNullOrWhiteSpace ( contentRootPath ) || ! string . IsNullOrWhiteSpace ( environmentName ) )
140139 {
141140 provider. LogFactory . Setup ( ) . LoadConfiguration ( cfg =>
142141 {
143142 if ( ! IsLoggingConfigurationLoaded ( cfg . Configuration ) )
144143 {
145144 nlogConfigFile = ResolveEnvironmentNLogConfigFile ( contentRootPath , environmentName ) ;
145+ #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
146146 cfg . Configuration = null ;
147+ #pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
147148 }
148149 } ) ;
149150 }
@@ -157,7 +158,7 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
157158 return provider;
158159 }
159160
160- private static string ResolveEnvironmentNLogConfigFile( string basePath , string environmentName )
161+ private static string ResolveEnvironmentNLogConfigFile( string ? basePath , string ? environmentName )
161162 {
162163 if ( ! string . IsNullOrWhiteSpace ( basePath ) )
163164 {
@@ -182,7 +183,7 @@ private static string ResolveEnvironmentNLogConfigFile(string basePath, string e
182183 if ( ! string . IsNullOrWhiteSpace ( environmentName ) )
183184 return $"nlog.{environmentName}.config" ;
184185
185- return null ;
186+ return string . Empty ;
186187 }
187188
188189 private static bool IsLoggingConfigurationLoaded ( LoggingConfiguration cfg )
0 commit comments