Esempio n. 1
0
 /// <summary>
 /// Refreshes configuration settings for the NlogLogger from the configuration file.
 /// </summary>
 public override void RefreshConfiguration()
 {
     try
     {
         base.RefreshConfiguration();
         LoggingSection         loggingSection         = LoggingSection.GetSection();
         LoggingSettingsElement loggingSettingsElement = loggingSection.LoggingSettings;
         _logFolder            = loggingSettingsElement.LogFolder;
         _forwardToEventLogger = loggingSettingsElement.ForwardToEventLogger;
         // Set eventSources according to the configuration.
         _eventSources = new SortedList <int, EventRange>();
         LoggingSourceCollection eventSourcesCollection = loggingSection.LoggingSettings.Sources;
         foreach (LoggingSourceElement eventSourceElement in eventSourcesCollection)
         {
             string   source    = eventSourceElement.Name;
             int      min       = 0;
             int      max       = 0;
             string   ranges    = eventSourceElement.Range;
             string[] rangeList = ranges.Split(',');
             foreach (string range in rangeList)
             {
                 int pos = range.IndexOf('-');
                 if ((pos <= 0) || (pos >= range.Length - 1))
                 {
                     throw new Exception("Invalid format for event range. Expected hyphen between min and max values.");
                 }
                 else
                 {
                     try
                     {
                         min = Int32.Parse(range.Substring(0, pos));
                         max = Int32.Parse(range.Substring(pos + 1));
                     }
                     catch (Exception exception)
                     {
                         throw new Exception("Invalid format for event range. Could not parse min and max values.", exception);
                     }
                     EventRange eventRange = new EventRange(min, max, source);
                     _eventSources.Add(min, eventRange);
                 }
             }
         }
         // Set the eventIdFilter according to the configuration.
         _eventIdFilter = loggingSection.LoggingSettings.EventIdFilter;
         if (_eventIdFilter != null)
         {
             _eventIdFilter = "," + _eventIdFilter + ",";
         }
     }
     catch (Exception exception)
     {
         Logger.WriteWarning("The configuration for the NlogLogger could not be loaded from the application configuration file. Default values will be used.", 151);
         throw exception;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Creates an ILogger implementation. The instance is a singleton.
        /// </summary>
        /// <returns>A reference to the ILogger implementation.</returns>
        public static ILogger GetLogger()
        {
            string  instanceName = "";
            string  className    = "";
            string  assemblyName = "";
            ILogger logger       = null;

            try
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        // Try to load the logging congiguration section.
                        LoggingSection section = null;
                        try
                        {
                            section = LoggingSection.GetSection();
                        }
                        catch (Exception exception)
                        {
                            _instance = new NullLogger("NullLogger");
                            logger.WriteWarning("Logging is not configured. No logging will be performed.", 152);
                        }
                        // If the logging configuration section was loaded, try to load the logging provider.
                        if (section != null)
                        {
                            try
                            {
                                ClassSpecificationElement spec = section.LoggingProvider;
                                instanceName = spec.Name;
                                className    = spec.Class;
                                assemblyName = spec.Assembly;
                                _instance    = (ILogger)Factory.CreateComponent(instanceName, className, assemblyName);
                                _instance.RefreshConfiguration();
                            }
                            catch (Exception exception)
                            {
                                _instance = new NullLogger("NullLogger");
                                logger.WriteError("Failed to create ILogger implementation. No logging will be performed.", 152);
                            }
                        }
                    }
                    logger = _instance;
                }
            }
            catch (Exception exception)
            {
                logger.WriteError("Failed to create ILogger implementation.: " + exception.ToString(), 152);
                throw exception;
            }
            return(logger);
        }