public void ConfigureFileTarget_Does_Not_Set_Debug_LogLevel_For_Test_Environment()
        {
            var loggingConfiguration = new LoggingConfiguration();

            loggingConfiguration.ConfigureFileTarget("Test");

            loggingConfiguration.ConfiguredNamedTargets.Count.Should().Be(1);
            loggingConfiguration.LoggingRules.Count.Should().Be(1);
            loggingConfiguration.ConfiguredNamedTargets[0].Should().BeOfType(typeof(FileTarget));
            loggingConfiguration.LoggingRules[0].Levels.Should().NotContain(LogLevel.Debug);
        }
        public void ConfigureFileTarget_Configures_File_Target()
        {
            var loggingConfiguration = new LoggingConfiguration();

            loggingConfiguration.ConfigureFileTarget("Local");

            loggingConfiguration.ConfiguredNamedTargets.Count.Should().Be(1);
            loggingConfiguration.LoggingRules.Count.Should().Be(1);
            loggingConfiguration.ConfiguredNamedTargets[0].Should().BeOfType(typeof(FileTarget));

            FileTarget fileTarget = (FileTarget)loggingConfiguration.ConfiguredNamedTargets[0];

            fileTarget.Layout.Should().NotBeNull();
            fileTarget.FileName.Should().NotBeNull();
        }
        public void ConfigureFileTarget_Configures_Log_Archival()
        {
            var loggingConfiguration = new LoggingConfiguration();

            loggingConfiguration.ConfigureFileTarget("Local");

            loggingConfiguration.ConfiguredNamedTargets.Count.Should().Be(1);
            loggingConfiguration.LoggingRules.Count.Should().Be(1);
            loggingConfiguration.LoggingRules[0].Levels[0].Should().Be(LogLevel.Debug);
            loggingConfiguration.ConfiguredNamedTargets[0].Should().BeOfType(typeof(FileTarget));

            FileTarget fileTarget = (FileTarget)loggingConfiguration.ConfiguredNamedTargets[0];

            fileTarget.ArchiveFileName.Should().NotBeNull();
            fileTarget.ArchiveNumbering.Should().Be(ArchiveNumberingMode.Rolling);
            fileTarget.ArchiveEvery.Should().Be(FileArchivePeriod.Day);
            fileTarget.MaxArchiveFiles.Should().BeGreaterThan(1);
        }
        public void ConfigureFileTarget_Configures_With_CustomRules()
        {
            var loggingConfiguration = new LoggingConfiguration();
            var loggingRules         = new List <LoggingRule>()
            {
                new LoggingRule("Service*", LogLevel.Debug, null),
                new LoggingRule("*Controller", LogLevel.Fatal, null),
                new LoggingRule("ErrorHandlingMiddleware", LogLevel.Trace, null)
            };

            loggingConfiguration.ConfigureFileTarget("Local", new NLogConfiguration()
            {
                LogLevel     = LogLevel.Fatal,
                LoggingRules = loggingRules
            });

            loggingConfiguration.LoggingRules.Count.Should().Be(3);
            loggingConfiguration.LoggingRules[0].Levels[0].Should().Be(LogLevel.Debug);
            loggingConfiguration.LoggingRules[0].LoggerNamePattern.Should().Be("Service*");
            loggingConfiguration.LoggingRules[1].Levels[0].Should().Be(LogLevel.Fatal);
            loggingConfiguration.LoggingRules[1].LoggerNamePattern.Should().Be("*Controller");
            loggingConfiguration.LoggingRules[2].Levels[0].Should().Be(LogLevel.Trace);
            loggingConfiguration.LoggingRules[2].LoggerNamePattern.Should().Be("ErrorHandlingMiddleware");
        }
예제 #5
0
        /// <summary>
        /// Creates LoggingConfiguration for NLog based on hosting environment
        /// </summary>
        /// <param name="env">Current hosting environment of the application</param>
        /// <param name="logConfiguration">[optional] Custom configuration for the application</param>
        public static LoggingConfiguration CreateConfiguration(IHostingEnvironment env, ILogConfiguration logConfiguration = null)
        {
            NLogConfiguration nLogConfig = SetUpNLogConfig(env, logConfiguration);

            var loggingConfiguration = new LoggingConfiguration();

            string basePath     = PlatformServices.Default.Application.ApplicationBasePath;
            string logDirectory = $"{basePath}/{GetLogDirectory(env, nLogConfig)}";

            loggingConfiguration.Variables["appName"]      = nLogConfig.AppName;
            loggingConfiguration.Variables["logDirectory"] = logDirectory;

            if (nLogConfig.EnableAllLogs)
            {
                loggingConfiguration.ConfigureAllTarget();
            }

            if (nLogConfig.EnableWebLogs)
            {
                loggingConfiguration.ConfigureFileTarget(GetEnvironmentName(env), logConfiguration);
            }

            return(loggingConfiguration);
        }