Exemplo n.º 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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 8
0
        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());
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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;
            }
        }