Example #1
0
        public static LoggingConfiguration CreateConfiguration(IHostingEnvironment env, IConfigurationSection configSection)
        {
            var defaultLogLevel = configSection.GetValue <string>("LogLevel:Default");

            Console.WriteLine($"NLOG Default Logging Level = {defaultLogLevel} ");
            NLogConfiguration nLogConfig = SetUpNLogConfig(env, null);

            nLogConfig.LogLevel = LogLevel.FromString(defaultLogLevel) ?? nLogConfig.LogLevel;

            var logLevelSection = configSection.GetSection("LogLevel");

            nLogConfig.LoggingRules = logLevelSection.GetChildren()
                                      .Select(n => BuildLoggingRule(n.Key, n.Value, nLogConfig.LogLevel)).ToList();
            return(CreateConfiguration(env, nLogConfig));
        }
Example #2
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);
        }