public void LoadConfigFile_NetCoreUnpublished_UseEntryDirectory() { // Arrange var d = Path.DirectorySeparatorChar; var tmpDir = Path.GetTempPath(); var appEnvMock = new AppEnvironmentMock(f => true, f => null) { AppDomainBaseDirectory = Path.Combine(tmpDir, "BaseDir"), #if NETSTANDARD AppDomainConfigurationFile = string.Empty, // NetCore style #else AppDomainConfigurationFile = Path.Combine(tmpDir, "EntryDir", "Entry.exe.config"), #endif CurrentProcessFilePath = Path.Combine(tmpDir, "ProcessDir", "Process.exe"),// NetCore dotnet.exe EntryAssemblyLocation = Path.Combine(tmpDir, "EntryDir"), EntryAssemblyFileName = "Entry.dll" }; var fileLoader = new LoggingConfigurationFileLoader(appEnvMock); // Act var result = fileLoader.GetDefaultCandidateConfigFilePaths().ToList(); // Assert base-directory + entry-directory + nlog-assembly-directory AssertResult(tmpDir, "EntryDir", "Entry", result); }
public void LoadConfigFile_NetCoreSingleFilePublish_IgnoreTmpDirectory() { // Arrange var tmpDir = "/var/tmp/"; var appEnvMock = new AppEnvironmentMock(f => true, f => null) { AppDomainBaseDirectory = Path.Combine(tmpDir, "BaseDir"), #if NETSTANDARD AppDomainConfigurationFile = string.Empty, // NetCore style #else AppDomainConfigurationFile = Path.Combine(tmpDir, "ProcessDir", "Entry.exe.config"), #endif CurrentProcessFilePath = Path.Combine(tmpDir, "ProcessDir", "Entry.exe"), // NetCore published exe EntryAssemblyLocation = Path.Combine(tmpDir, "TempProcessDir"), UserTempFilePath = "/tmp/", EntryAssemblyFileName = "Entry.dll" }; var fileLoader = new LoggingConfigurationFileLoader(appEnvMock); // Act var result = fileLoader.GetDefaultCandidateConfigFilePaths().ToList(); // Assert base-directory + process-directory + nlog-assembly-directory AssertResult(tmpDir, "TempProcessDir", "ProcessDir", "Entry", result); }
public void SetupBuilderLoadConfigurationFromFileOptionalFalseTest() { // Arrange var xmlFile = new System.IO.StringReader("<nlog autoshutdown='false'></nlog>"); var appEnv = new Mocks.AppEnvironmentMock(f => false, f => System.Xml.XmlReader.Create(xmlFile)); var configLoader = new LoggingConfigurationFileLoader(appEnv); var logFactory = new LogFactory(configLoader); // Act / Assert Assert.Throws <System.IO.FileNotFoundException>(() => logFactory.Setup().LoadConfigurationFromFile("NLog.config", optional: false)); }
public void SetupBuilderLoadConfigurationFromFileMissingTest() { // Arrange var appEnv = new Mocks.AppEnvironmentMock(f => false, f => null); var configLoader = new LoggingConfigurationFileLoader(appEnv); var logFactory = new LogFactory(configLoader); // Act logFactory.Setup().LoadConfigurationFromFile(optional: true); // Assert // no Exception }
public void SetupBuilderLoadNLogConfigFromFileNotExistsTest() { // Arrange var xmlFile = new System.IO.StringReader("<nlog autoshutdown='false'></nlog>"); var appEnv = new Mocks.AppEnvironmentMock(f => false, f => System.Xml.XmlReader.Create(xmlFile)); var configLoader = new LoggingConfigurationFileLoader(appEnv); var logFactory = new LogFactory(configLoader); // Act logFactory.Setup().LoadConfigurationFromFile("NLog.config", optional: true); // Assert Assert.Null(logFactory.Configuration); }
public void SetupBuilderLoadConfigurationFromFileTest() { // Arrange var xmlFile = new System.IO.StringReader("<nlog autoshutdown='false'></nlog>"); var appEnv = new Mocks.AppEnvironmentMock(f => true, f => System.Xml.XmlReader.Create(xmlFile)); var configLoader = new LoggingConfigurationFileLoader(appEnv); var logFactory = new LogFactory(configLoader); // Act logFactory.Setup().LoadConfigurationFromFile(); // Assert Assert.False(logFactory.AutoShutdown); }
public void GetConfigFile_absolutePath_loads(string filename, string accepts, string expected, string baseDir) { // Arrange var appEnvMock = new AppEnvironmentMock(f => f == accepts, f => null) { AppDomainBaseDirectory = baseDir }; var fileLoader = new LoggingConfigurationFileLoader(appEnvMock); // Act var result = fileLoader.GetConfigFile(filename); // Assert Assert.Equal(expected, result); }
public void GetConfigFile_absolutePath_loads(string filename, string accepts, string expected, string baseDir) { // Arrange var appEnvMock = new AppEnvironmentMock(f => f == accepts, f => System.Xml.XmlReader.Create(new StringReader(@"<nlog autoreload=""true""></nlog>"))) { AppDomainBaseDirectory = baseDir }; var fileLoader = new LoggingConfigurationFileLoader(appEnvMock); var logFactory = new LogFactory(fileLoader); // Act var result = fileLoader.Load(logFactory, filename); // Assert Assert.Equal(expected, result?.FileNamesToWatch.First()); }
public void LoadConfigFile_EmptyEnvironment_UseCurrentDirectory() { // Arrange var appEnvMock = new AppEnvironmentMock(f => true, f => null); var fileLoader = new LoggingConfigurationFileLoader(appEnvMock); // Act var result = fileLoader.GetDefaultCandidateConfigFilePaths().ToList(); // Assert loading from current-directory and from nlog-assembly-directory if (NLog.Internal.PlatformDetector.IsWin32) { Assert.Equal(2, result.Count); // Case insensitive } Assert.Equal("NLog.config", result.First(), StringComparer.OrdinalIgnoreCase); Assert.Contains("NLog.dll.nlog", result.Last(), StringComparison.OrdinalIgnoreCase); }
public void SetupBuilderLoadConfigurationFromXmlPatchTest() { // Arrange var xmlFile = new System.IO.StringReader("<nlog autoshutdown='true'></nlog>"); var appEnv = new Mocks.AppEnvironmentMock(f => true, f => System.Xml.XmlReader.Create(xmlFile)); var configLoader = new LoggingConfigurationFileLoader(appEnv); var logFactory = new LogFactory(configLoader); // Act logFactory.Setup(). LoadConfigurationFromXml("<nlog autoshutdown='false'></nlog>"). LoadConfigurationFromFile(). // No effect, since config already loaded LoadConfiguration(b => { b.Configuration.Variables["Hello"] = "World"; }); // Assert Assert.False(logFactory.AutoShutdown); Assert.Single(logFactory.Configuration.Variables); }
public void GetConfigFile_absolutePath_loads(string filename, string accepts, string expected, string baseDir) { // Arrange var fileMock = new FileMock(f => f == accepts); var fileLoader = new LoggingConfigurationFileLoader(fileMock); var appDomain = LogFactory.CurrentAppDomain; try { LogFactory.CurrentAppDomain = new AppDomainMock(baseDir); // Act var result = fileLoader.GetConfigFile(filename); // Assert Assert.Equal(expected, result); } finally { //restore LogFactory.CurrentAppDomain = appDomain; } }