11using System ;
2- using System . Configuration ;
32using System . IO ;
43using Xunit ;
54using Serilog . Events ;
@@ -10,25 +9,28 @@ namespace Serilog.Tests.AppSettings.Tests
109{
1110 public class AppSettingsTests
1211 {
13- static AppSettingsTests ( )
12+ static string GetConfigPath ( )
1413 {
14+ const string testsConfig = "tests.config" ;
15+ if ( File . Exists ( testsConfig ) )
16+ return Path . GetFullPath ( testsConfig ) ;
1517 var basePath = AppDomain . CurrentDomain . BaseDirectory ;
16- var config = Path . GetFullPath ( Path . Combine ( basePath , "app.config" ) ) ;
17- if ( ! File . Exists ( config ) )
18- throw new InvalidOperationException ( $ "Can't find app.config in { basePath } ") ;
18+ return Path . GetFullPath ( Path . Combine ( basePath , testsConfig ) ) ;
19+ }
1920
20- AppDomain . CurrentDomain . SetData ( "APP_CONFIG_FILE" , config ) ;
21+ [ Fact ]
22+ public void TheTestConfigFileExists ( )
23+ {
24+ var config = GetConfigPath ( ) ;
25+ Assert . True ( File . Exists ( config ) , "Can't find the test configuration file" ) ;
2126 }
2227
2328 [ Fact ]
2429 public void EnvironmentVariableExpansionIsApplied ( )
2530 {
26- // Make sure we have the expected key in the App.config
27- Assert . Equal ( "%PATH%" , ConfigurationManager . AppSettings [ "serilog:enrich:with-property:Path" ] ) ;
28-
2931 LogEvent evt = null ;
3032 var log = new LoggerConfiguration ( )
31- . ReadFrom . AppSettings ( )
33+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
3234 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
3335 . CreateLogger ( ) ;
3436
@@ -45,18 +47,14 @@ public void CanUseCustomPrefixToConfigureSettings()
4547 const string prefix1 = "custom1" ;
4648 const string prefix2 = "custom2" ;
4749
48- // Make sure we have the expected keys in the App.config
49- Assert . Equal ( "Warning" , ConfigurationManager . AppSettings [ prefix1 + ":serilog:minimum-level" ] ) ;
50- Assert . Equal ( "Error" , ConfigurationManager . AppSettings [ prefix2 + ":serilog:minimum-level" ] ) ;
51-
5250 var log1 = new LoggerConfiguration ( )
5351 . WriteTo . Observers ( o => { } )
54- . ReadFrom . AppSettings ( prefix1 )
52+ . ReadFrom . AppSettings ( prefix1 , filePath : GetConfigPath ( ) )
5553 . CreateLogger ( ) ;
5654
5755 var log2 = new LoggerConfiguration ( )
5856 . WriteTo . Observers ( o => { } )
59- . ReadFrom . AppSettings ( prefix2 )
57+ . ReadFrom . AppSettings ( prefix2 , filePath : GetConfigPath ( ) )
6058 . CreateLogger ( ) ;
6159
6260 Assert . False ( log1 . IsEnabled ( LogEventLevel . Information ) ) ;
@@ -70,25 +68,22 @@ public void CanUseCustomPrefixToConfigureSettings()
7068 public void CustomPrefixCannotContainColon ( )
7169 {
7270 Assert . Throws < ArgumentException > ( ( ) =>
73- new LoggerConfiguration ( ) . ReadFrom . AppSettings ( "custom1:custom2" ) ) ;
71+ new LoggerConfiguration ( ) . ReadFrom . AppSettings ( "custom1:custom2" , filePath : GetConfigPath ( ) ) ) ;
7472 }
7573
7674 [ Fact ]
7775 public void CustomPrefixCannotBeSerilog ( )
7876 {
7977 Assert . Throws < ArgumentException > ( ( ) =>
80- new LoggerConfiguration ( ) . ReadFrom . AppSettings ( "serilog" ) ) ;
78+ new LoggerConfiguration ( ) . ReadFrom . AppSettings ( "serilog" , filePath : GetConfigPath ( ) ) ) ;
8179 }
8280
8381 [ Fact ]
8482 public void ThreadIdEnricherIsApplied ( )
8583 {
86- // Make sure we have the expected key in the App.config
87- Assert . NotNull ( ConfigurationManager . AppSettings [ "serilog:enrich:WithThreadId" ] ) ;
88-
8984 LogEvent evt = null ;
9085 var log = new LoggerConfiguration ( )
91- . ReadFrom . AppSettings ( )
86+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
9287 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
9388 . CreateLogger ( ) ;
9489
@@ -102,12 +97,9 @@ public void ThreadIdEnricherIsApplied()
10297 [ Fact ]
10398 public void MachineNameEnricherIsApplied ( )
10499 {
105- // Make sure we have the expected key in the App.config
106- Assert . NotNull ( ConfigurationManager . AppSettings [ "serilog:enrich:WithMachineName" ] ) ;
107-
108100 LogEvent evt = null ;
109101 var log = new LoggerConfiguration ( )
110- . ReadFrom . AppSettings ( )
102+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
111103 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
112104 . CreateLogger ( ) ;
113105
@@ -121,12 +113,9 @@ public void MachineNameEnricherIsApplied()
121113 [ Fact ]
122114 public void EnrivonmentUserNameEnricherIsApplied ( )
123115 {
124- // Make sure we have the expected key in the App.config
125- Assert . NotNull ( ConfigurationManager . AppSettings [ "serilog:enrich:WithEnvironmentUserName" ] ) ;
126-
127116 LogEvent evt = null ;
128117 var log = new LoggerConfiguration ( )
129- . ReadFrom . AppSettings ( )
118+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
130119 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
131120 . CreateLogger ( ) ;
132121
@@ -140,12 +129,9 @@ public void EnrivonmentUserNameEnricherIsApplied()
140129 [ Fact ]
141130 public void ProcessIdEnricherIsApplied ( )
142131 {
143- // Make sure we have the expected key in the App.config
144- Assert . NotNull ( ConfigurationManager . AppSettings [ "serilog:enrich:WithProcessId" ] ) ;
145-
146132 LogEvent evt = null ;
147133 var log = new LoggerConfiguration ( )
148- . ReadFrom . AppSettings ( )
134+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
149135 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
150136 . CreateLogger ( ) ;
151137
@@ -159,12 +145,9 @@ public void ProcessIdEnricherIsApplied()
159145 [ Fact ]
160146 public void LogContextEnricherIsApplied ( )
161147 {
162- // Make sure we have the expected key in the App.config
163- Assert . NotNull ( ConfigurationManager . AppSettings [ "serilog:enrich:FromLogContext" ] ) ;
164-
165148 LogEvent evt = null ;
166149 var log = new LoggerConfiguration ( )
167- . ReadFrom . AppSettings ( )
150+ . ReadFrom . AppSettings ( filePath : GetConfigPath ( ) )
168151 . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
169152 . CreateLogger ( ) ;
170153
0 commit comments