@@ -68,17 +68,18 @@ public void UseNLog_withConfiguration_WorksWithNLog()
6868 [ Fact ]
6969 public void AddNLog_withShutdownOnDispose_worksWithNLog ( )
7070 {
71+ var logFactory = new LogFactory ( ) ; // Isolated to avoid disposing global LogManager
7172 var someParam = new NLogProviderOptions { ShutdownOnDispose = true } ;
72- var actual = new HostBuilder ( ) . ConfigureLogging ( l => l . AddNLog ( someParam ) ) . Build ( ) ;
73+ var actual = new HostBuilder ( ) . ConfigureLogging ( l => l . AddNLog ( someParam , svc => logFactory ) ) . Build ( ) ;
7374 try
7475 {
75- TestHostingResult ( actual , false ) ;
76- Assert . NotNull ( LogManager . Configuration ) ;
76+ TestHostingResult ( actual , false , logFactory ) ;
77+ Assert . NotNull ( logFactory . Configuration ) ;
7778 }
7879 finally
7980 {
8081 actual . Dispose ( ) ;
81- Assert . Null ( LogManager . Configuration ) ;
82+ Assert . Null ( logFactory . Configuration ) ;
8283 }
8384 }
8485
@@ -89,12 +90,14 @@ public void UseNLog_withAddNLog_worksWithNLog()
8990 TestHostingResult ( actual , true ) ;
9091 }
9192
92- private static void TestHostingResult ( IHost host , bool resetConfiguration )
93+ private static void TestHostingResult ( IHost host , bool resetConfiguration , LogFactory logFactory = null )
9394 {
95+ logFactory ??= LogManager . LogFactory ;
96+
9497 try
9598 {
9699 var nlogTarget = new Targets . MemoryTarget ( ) { Name = "Output" } ;
97- NLog . LogManager . Setup ( ) . LoadConfiguration ( c => c . ForLogger ( ) . WriteTo ( nlogTarget ) ) ;
100+ logFactory . Setup ( ) . LoadConfiguration ( c => c . ForLogger ( ) . WriteTo ( nlogTarget ) ) ;
98101
99102 var loggerFactory = host . Services . GetService < ILoggerFactory > ( ) ;
100103 Assert . NotNull ( loggerFactory ) ;
@@ -106,7 +109,7 @@ private static void TestHostingResult(IHost host, bool resetConfiguration)
106109 finally
107110 {
108111 if ( resetConfiguration )
109- LogManager . Configuration = null ;
112+ logFactory . Configuration = null ;
110113 }
111114 }
112115
@@ -202,21 +205,22 @@ public void IHostApplicationBuilder_UseNLog_withConfiguration_WorksWithNLog()
202205 [ Fact ]
203206 public void IHostApplicationBuilder_AddNLog_withShutdownOnDispose_worksWithNLog ( )
204207 {
208+ var logFactory = new LogFactory ( ) ; // Isolated to avoid disposing global LogManager
205209 var someParam = new NLogProviderOptions { ShutdownOnDispose = true } ;
206210
207211 var builder = new HostApplicationBuilder ( ) ;
208- builder . Logging . AddNLog ( someParam ) ;
212+ builder . Logging . AddNLog ( someParam , svc => logFactory ) ;
209213
210214 var actual = builder . Build ( ) ;
211215 try
212216 {
213- TestHostingResult ( actual , false ) ;
214- Assert . NotNull ( LogManager . Configuration ) ;
217+ TestHostingResult ( actual , false , logFactory ) ;
218+ Assert . NotNull ( logFactory . Configuration ) ;
215219 }
216220 finally
217221 {
218222 actual . Dispose ( ) ;
219- Assert . Null ( LogManager . Configuration ) ;
223+ Assert . Null ( logFactory . Configuration ) ;
220224 }
221225 }
222226
@@ -299,4 +303,4 @@ public void IHostApplicationBuilder_UseNLog_ShouldLogToNLog()
299303 }
300304#endif
301305 }
302- }
306+ }
0 commit comments