55using Microsoft . Extensions . Hosting ;
66using NLog . Config ;
77using NLog . Extensions . Logging ;
8+ #if NETSTANDARD2_0
9+ using IHostEnvironment = Microsoft . Extensions . Hosting . IHostingEnvironment ;
10+ #endif
811
912namespace NLog . Extensions . Hosting
1013{
@@ -35,18 +38,18 @@ public static IHostBuilder UseNLog(this IHostBuilder builder)
3538 public static IHostBuilder UseNLog ( this IHostBuilder builder , NLogProviderOptions options )
3639 {
3740 if ( builder == null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
38- builder . ConfigureServices ( ( builderContext , services ) => AddNLogLoggerProvider ( services , builderContext . Configuration , options , CreateNLogLoggerProvider ) ) ;
41+ builder . ConfigureServices ( ( builderContext , services ) => AddNLogLoggerProvider ( services , builderContext . Configuration , builderContext . HostingEnvironment , options , CreateNLogLoggerProvider ) ) ;
3942 return builder ;
4043 }
4144
42- private static void AddNLogLoggerProvider ( IServiceCollection services , IConfiguration configuration , NLogProviderOptions options , Func < IServiceProvider , IConfiguration , NLogProviderOptions , NLogLoggerProvider > factory )
45+ private static void AddNLogLoggerProvider ( IServiceCollection services , IConfiguration configuration , IHostEnvironment hostEnvironment , NLogProviderOptions options , Func < IServiceProvider , IConfiguration , IHostEnvironment , NLogProviderOptions , NLogLoggerProvider > factory )
4346 {
4447 ConfigurationItemFactory . Default . RegisterItemsFromAssembly ( typeof ( ConfigureExtensions ) . GetTypeInfo ( ) . Assembly ) ;
4548 LogManager . AddHiddenAssembly ( typeof ( ConfigureExtensions ) . GetTypeInfo ( ) . Assembly ) ;
46- services . TryAddNLogLoggingProvider ( ( svc , addlogging ) => svc . AddLogging ( addlogging ) , configuration , options , factory ) ;
49+ services . TryAddNLogLoggingProvider ( ( svc , addlogging ) => svc . AddLogging ( addlogging ) , configuration , options , ( provider , cfg , opt ) => factory ( provider , cfg , hostEnvironment , opt ) ) ;
4750 }
4851
49- private static NLogLoggerProvider CreateNLogLoggerProvider ( IServiceProvider serviceProvider , IConfiguration configuration , NLogProviderOptions options )
52+ private static NLogLoggerProvider CreateNLogLoggerProvider ( IServiceProvider serviceProvider , IConfiguration configuration , IHostEnvironment hostEnvironment , NLogProviderOptions options )
5053 {
5154 NLogLoggerProvider provider = new NLogLoggerProvider ( options ) ;
5255
@@ -63,6 +66,12 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
6366 provider . TryLoadConfigurationFromSection ( configuration ) ;
6467 }
6568
69+ var contentRootPath = hostEnvironment ? . ContentRootPath ;
70+ if ( ! string . IsNullOrEmpty ( contentRootPath ) )
71+ {
72+ TryLoadConfigurationFromContentRootPath ( provider . LogFactory , contentRootPath ) ;
73+ }
74+
6675 return provider ;
6776 }
6877
@@ -75,5 +84,32 @@ private static IConfiguration SetupConfiguration(IServiceProvider serviceProvide
7584 }
7685 return configuration ;
7786 }
87+
88+ private static void TryLoadConfigurationFromContentRootPath ( LogFactory logFactory , string contentRootPath )
89+ {
90+ logFactory . Setup ( ) . LoadConfiguration ( config =>
91+ {
92+ if ( config . Configuration . LoggingRules . Count == 0 && config . Configuration . AllTargets . Count == 0 )
93+ {
94+ var standardPath = System . IO . Path . Combine ( contentRootPath , "NLog.config" ) ;
95+ if ( System . IO . File . Exists ( standardPath ) )
96+ {
97+ config . Configuration = new XmlLoggingConfiguration ( standardPath , config . LogFactory ) ;
98+ }
99+ else
100+ {
101+ var lowercasePath = System . IO . Path . Combine ( contentRootPath , "nlog.config" ) ;
102+ if ( System . IO . File . Exists ( lowercasePath ) )
103+ {
104+ config . Configuration = new XmlLoggingConfiguration ( lowercasePath , config . LogFactory ) ;
105+ }
106+ else
107+ {
108+ config . Configuration = null ; // Perform default loading
109+ }
110+ }
111+ }
112+ } ) ;
113+ }
78114 }
79115}
0 commit comments