Exemple #1
0
        private void InitializeLogging()
        {
            var currentConfig = LogManager.Configuration;

            if (currentConfig == null)
            {
                var binDirectory =
                    new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)).LocalPath;
                var configFile = Path.Combine(binDirectory, "NLog.config");

                if (File.Exists(configFile))
                {
                    var newConfig = new XmlLoggingConfiguration(configFile);
                    LogManager.Configuration = newConfig;
                    currentConfig            = LogManager.Configuration;
                }
                else
                {
                    var localPath  = GetLocalPath();
                    var logDirPath = Path.Combine(localPath, "logs");

                    SimpleConfigurator.ConfigureForFileLogging(Path.Combine(logDirPath, "ApplicationLog.log"));
                }
            }

            UpdateConfig(currentConfig);

            LogManager.Configuration = currentConfig;
        }
        /// <summary>
        ///     Initialize the logging environment for Azure; including
        ///     automatically rewriting log file paths for compatibility with
        ///     local storage and setting up private variables.
        /// </summary>
        public void InitializeForCloud()
        {
            // Attach the diagnostic monitor trace listener to the list of master
            // System.Diagnostics trace listeners
            Trace.Listeners.Clear();
            if (RoleEnvironment.IsAvailable)
            {
                Trace.Listeners.Add(new DiagnosticMonitorTraceListener());
            }
            else
            {
                Trace.Listeners.Add(new DefaultTraceListener());
            }
            Trace.WriteLine("Initializing NLog configuration for Azure");

            // Replace log file and role name settings in the configuration
            var currentCfg = LogManager.Configuration;

            if (currentCfg == null)
            {
                // Depending on the deployment environment (i.e. Azure emulator) the NLog library
                // may not properly auto-load the NLog.config file.
                var binDirectory = new Uri(
                    Path.GetDirectoryName(
                        Assembly.GetExecutingAssembly().GetName().CodeBase)).LocalPath;
                var configFile = Path.Combine(binDirectory, "NLog.config");

                // Check for NLog.config in the local directory.
                if (File.Exists(configFile))
                {
                    var newConfig = new XmlLoggingConfiguration(configFile);
                    LogManager.Configuration = newConfig;
                    currentCfg = LogManager.Configuration;
                }
                else
                {
                    // Set basic configuration and log the error
                    var localPath  = Path.GetTempPath();
                    var logDirPath = Path.Combine(localPath, "logs");
                    SimpleConfigurator.ConfigureForFileLogging(
                        Path.Combine(logDirPath, "ApplicationLog.log"));
                    Trace.TraceWarning(
                        "Warning: no NLog configuration section found in web.config or NLog.config; falling back to basic configuration");
                    return;
                }
            }

            Trace.WriteLine("Resetting NLog configuration");
            UpdateConfigForCloud(currentCfg);
            LogManager.Configuration = currentCfg;
        }
Exemple #3
0
        protected JsonListCommand()
        {
            HasOption("ll|logLevel=", "Set LogLevel for NLog (Warn, Info, Debug)", u =>
            {
                try
                {
                    Log.Level = LogLevel.FromString(u);
                    SimpleConfigurator.ConfigureForConsoleLogging(Log.Level);
                }
                catch
                {
                    Console.WriteLine("Unable to parse {0} to a LogLevel", u);
                }
            });

            HasOption("lf|logFile=", "File for NLog", u => SimpleConfigurator.ConfigureForFileLogging(u, Log.Level));
        }