Example #1
0
        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);
        }
Example #3
0
        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));
        }
Example #4
0
        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
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        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);
        }
Example #10
0
        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);
        }
Example #11
0
        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;
            }
        }