예제 #1
0
        static Logger()
        {
            try
            {
                LoggingSection section = GetConfigurationSection();

                LoggingProviderCollection providerCollection = LoadAndInitializeProviderCollection(section);

                LoggingProviderBase defaultProvider = GetDefaultProvider(section, providerCollection);

                InitializeFallbackProviders(providerCollection);

                CompleteInitialization(providerCollection, defaultProvider);

                CircularReferenceFinder.Validate(providerCollection);

                Logger.providers = providerCollection;
                Logger.provider  = defaultProvider;
            }
            catch (ProviderException pex)
            {
                // When a ProviderException or ConfigurationException is thrown, we store those and throw them
                // when one of the public methods of Logger is called. This way the original exceptions are
                // thrown and not a TypeInitializeException that wraps the original.
                InitializationException = pex;
            }
            catch (ConfigurationException ceex)
            {
                InitializationException = ceex;
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WindowsEventLogLoggingProvider"/> class.
        /// </summary>
        /// <param name="threshold">The <see cref="LoggingEventType"/> logging threshold. The threshold limits
        /// the number of event logged. <see cref="LoggingProviderBase.Threshold">Threshold</see> for more
        /// information.</param>
        /// <param name="logName">The name of the log where the source's entries are written to. Possible
        /// values include: Application, System, or a custom event log.</param>
        /// <param name="source">The source name to register and use when writing to the event log. This is
        /// the source name by which the application is registered on the local computer.</param>
        /// <param name="fallbackProvider">The optional fallback provider.</param>
        public WindowsEventLogLoggingProvider(LoggingEventType threshold, string logName, string source,
                                              LoggingProviderBase fallbackProvider) : base(threshold, fallbackProvider)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Length == 0)
            {
                throw new ArgumentException(SR.ValueShouldNotBeAnEmptyString(), "source");
            }

            if (logName == null)
            {
                throw new ArgumentNullException("logName");
            }

            if (logName.Length == 0)
            {
                throw new ArgumentException(SR.ValueShouldNotBeAnEmptyString(), "logName");
            }

            this.source  = source;
            this.logName = logName;
        }
예제 #3
0
        private static void InitializeProvider(LoggingProviderBase provider, ProviderSettings settings)
        {
            string providerName = settings.Name;

            NameValueCollection configuration = BuildProviderConfiguration(settings);

            provider.Initialize(providerName, configuration);
        }
예제 #4
0
 private static void CompleteInitialization(LoggingProviderCollection providers,
                                            LoggingProviderBase defaultProvider)
 {
     foreach (LoggingProviderBase provider in providers)
     {
         provider.CompleteInitialization(providers, defaultProvider);
     }
 }
예제 #5
0
        private static void ValidateProvider(LoggingProviderBase provider, LoggingProviderChain chain)
        {
            ThrowWhenProviderIsInChain(provider, chain);

            // Note that LoggingProviderChain is immutable. Adding involves creating a new instance.
            var chainWithProvider = chain.Add(provider);

            CheckAllReferencedProviders(provider.GetReferencedProviders(), chainWithProvider);
        }
예제 #6
0
        internal static string ProviderHasNotBeenInitializedCorrectlyCallAnOverloadedConstructor(
            LoggingProviderBase provider)
        {
            string name = GetShortTypeNameForOwnTypes(provider.GetType());

            // Note that this message is returned in the case that initialize hasn't been called. Therefore
            // the provider's Name property will be null and supplying it to the message is useless.
            return(GetString("ProviderHasNotBeenInitializedCorrectlyCallAnOverloadedConstructor", name));
        }
        internal override void CompleteInitialization(LoggingProviderCollection configuredProviders,
                                                      LoggingProviderBase defaultProvider)
        {
            // Finish performing implementation-specific provider initialization here (this is 2nd/last part).
            // This operation has to be executed here, because during Initialize this list of configured is
            // providers not yet available.
            this.InitializeProviders(configuredProviders);

            this.SetInitialized(true);
        }
        public MailLoggingProvider(LoggingEventType threshold, string subjectFormatString,
                                   LoggingProviderBase fallbackProvider, params MailAddress[] recipients)
            : base(threshold, fallbackProvider)
        {
            this.To = BuildAddressList(recipients);

            this.ValidateSubjectFormatString(subjectFormatString, InitializationModel.CodeOnly);
            this.SetSubjectFormatString(subjectFormatString);

            this.ValidateDotNetMailConfiguration();
        }
예제 #9
0
        private static void ThrowWhenProviderIsInChain(LoggingProviderBase providerToValidate,
                                                       LoggingProviderChain providersInChain)
        {
            // Check whether the supplied provider is already directly or indirectly referencing itself.
            bool providerIsSelfReferenced = providersInChain.Contains(providerToValidate);

            if (providerIsSelfReferenced)
            {
                throw new ConfigurationErrorsException(
                          SR.CircularReferenceInLoggingSection(Logger.SectionName, providerToValidate.Name));
            }
        }
예제 #10
0
            internal bool Contains(LoggingProviderBase provider)
            {
                if (this.IsEmpty)
                {
                    return(false);
                }

                if (this.provider == provider)
                {
                    return(true);
                }

                return(this.list.Contains(provider));
            }
예제 #11
0
        // Throws a ConfigurationErrorsException (descendant of ConfigurationException) on failure.
        private static LoggingProviderBase GetDefaultProvider(LoggingSection loggingSection,
                                                              LoggingProviderCollection providerCollection)
        {
            LoggingProviderBase defaultProvider = providerCollection[loggingSection.DefaultProvider];

            if (defaultProvider == null)
            {
                PropertyInformation property = loggingSection.ElementInformation.Properties["defaultProvider"];

                throw new ConfigurationErrorsException(
                          SR.NoDefaultLoggingProviderFound(SectionName), property.Source, property.LineNumber);
            }

            return(defaultProvider);
        }
예제 #12
0
        // Throws a ConfigurationException (or a descendant) on failure.
        private static LoggingProviderCollection LoadAndInitializeProviderCollection(LoggingSection section)
        {
            LoggingProviderCollection providerCollection = new LoggingProviderCollection();

            foreach (ProviderSettings settings in section.Providers)
            {
                LoggingProviderBase loggingProvider = InstantiateLoggingProvider(settings);

                providerCollection.Add(loggingProvider);
            }

            providerCollection.SetReadOnly();

            return(providerCollection);
        }
        /// <summary>Initializes a new instance of the <see cref="SqlLoggingProvider"/> class.</summary>
        /// <param name="threshold">The <see cref="LoggingEventType"/> logging threshold. The threshold limits
        /// the number of event logged. <see cref="LoggingProviderBase.Threshold">Threshold</see> for more
        /// information.</param>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="fallbackProvider">The optional fallback provider.</param>
        /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="threshold"/> has an
        /// invalid value.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="connectionString"/> is a
        /// null reference (Nothing in VB).</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="connectionString"/> is an
        /// empty string.</exception>
        public SqlLoggingProvider(LoggingEventType threshold, string connectionString,
                                  LoggingProviderBase fallbackProvider) : base(threshold, fallbackProvider)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }

            if (connectionString.Length == 0)
            {
                throw new ArgumentException(SR.ValueShouldNotBeAnEmptyString(), "connectionString");
            }

            this.connectionString = connectionString;
        }
예제 #14
0
        /// <summary>Initializes a new instance of the <see cref="LoggingProviderBase"/> class.</summary>
        /// <param name="threshold">The <see cref="LoggingEventType"/> logging threshold. The threshold limits
        /// the number of event logged. <see cref="Threshold"/> for more information.</param>
        /// <param name="fallbackProvider">The optional fallback provider.</param>
        /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="threshold"/> has an
        /// invalid value.</exception>
        protected LoggingProviderBase(LoggingEventType threshold, LoggingProviderBase fallbackProvider)
        {
            if (threshold < LoggingEventType.Debug || threshold > LoggingEventType.Critical)
            {
                throw new InvalidEnumArgumentException("threshold", (int)threshold, typeof(LoggingEventType));
            }

            base.Initialize(this.GetType().Name, null);

            this.threshold = threshold;

            // NOTE: We don't have to check for circular references, because a provider is immutable after
            // creation and can therefore never be circular referencing itself.
            this.FallbackProvider = fallbackProvider;
        }
예제 #15
0
        private static void InitializeFallbackProvider(LoggingProviderBase provider,
                                                       LoggingProviderCollection providers)
        {
            if (provider.FallbackProviderName != null)
            {
                // Fetch the fallback provider with the defined name from the providers collection
                LoggingProviderBase fallbackProvider = providers[provider.FallbackProviderName];

                // Throw an exception when that provider could not be found.
                if (fallbackProvider == null)
                {
                    throw new ProviderException(
                              SR.InvalidFallbackProviderPropertyInConfig(SectionName, provider));
                }

                // Initialize the provider's fallback provider with the found provider.
                provider.FallbackProvider = fallbackProvider;
            }
        }
        protected FileLoggingProviderBase(LoggingEventType threshold, string path,
                                          LoggingProviderBase fallbackProvider)
            : base(threshold, fallbackProvider)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException(SR.ValueShouldNotBeAnEmptyString(), "path");
            }

            this.path = GetFullCanonicalPath(path);

            this.CheckAuthorizationsByCreatingFile();

            this.SetInitialized(true);
        }
        /// <summary>Initializes a new instance of the <see cref="CompositeLoggingProvider"/> class.</summary>
        /// <param name="threshold">The <see cref="LoggingEventType"/> logging threshold. The threshold limits
        /// the number of event logged. <see cref="LoggingProviderBase.Threshold">Threshold</see> for more
        /// information.</param>
        /// <param name="fallbackProvider">The optional fallback provider.</param>
        /// <param name="providers">The providers to which events will be forwarded.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="providers"/> argument is
        /// a null reference (Nothing in VB).</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="providers"/> contains
        /// duplicate elements, null references or no elements.</exception>
        /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="threshold"/> has an
        /// invalid value.</exception>
        public CompositeLoggingProvider(LoggingEventType threshold, LoggingProviderBase fallbackProvider,
                                        IEnumerable <LoggingProviderBase> providers)
            : base(threshold, fallbackProvider)
        {
            if (providers == null)
            {
                throw new ArgumentNullException("providers");
            }

            // Make a copy to prevent changes during or after validation.
            var providerList = new List <LoggingProviderBase>(providers);

            if (providerList.Contains(null))
            {
                throw new ArgumentException(SR.CollectionMustNotContainNullElements(), "providers");
            }

            // We can't use HashSet<T> here, because it is a .NET 3.5 call and we need to stay compatible
            // with .NET 2.0.
            var set = new Dictionary <object, object>();

            foreach (var provider in providerList)
            {
                // Add provider.
                set[provider] = null;
            }

            if (set.Count != providerList.Count)
            {
                throw new ArgumentException(SR.CollectionMustNotContainDuplicates(), "providers");
            }

            if (providerList.Count == 0)
            {
                throw new ArgumentException(SR.CollectionShouldContainAtleastOneElement(), "providers");
            }

            this.providers = new ReadOnlyCollection <LoggingProviderBase>(providerList.ToArray());
        }
 public XmlFileLoggingProvider(LoggingEventType threshold, string path,
                               LoggingProviderBase fallbackProvider)
     : base(threshold, path, fallbackProvider)
 {
 }
예제 #19
0
 // Exception messages for LoggingProviderBase class
 internal static string InvalidFallbackProviderPropertyInConfig(string sectionName,
                                                                LoggingProviderBase provider)
 {
     return(GetString("InvalidFallbackProviderPropertyInConfig", sectionName,
                      provider.GetType().FullName, provider.Name, provider.FallbackProviderName));
 }
예제 #20
0
 private LoggingProviderChain(LoggingProviderBase provider, LoggingProviderChain list)
 {
     this.provider = provider;
     this.list     = list;
 }
예제 #21
0
 internal LoggingProviderChain Add(LoggingProviderBase provider)
 {
     return(new LoggingProviderChain(provider, this));
 }
예제 #22
0
 internal virtual void CompleteInitialization(LoggingProviderCollection configuredProviders,
                                              LoggingProviderBase defaultProvider)
 {
     // Default implementation is empty.
 }
 /// <summary>Initializes a new instance of the <see cref="CompositeLoggingProvider"/> class.</summary>
 /// <param name="threshold">The <see cref="LoggingEventType"/> logging threshold. The threshold limits
 /// the number of event logged. <see cref="LoggingProviderBase.Threshold">Threshold</see> for more
 /// information.</param>
 /// <param name="fallbackProvider">The optional fallback provider.</param>
 /// <param name="providers">The providers to which events will be forwarded.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="providers"/> argument is
 /// a null reference (Nothing in VB).</exception>
 /// <exception cref="ArgumentException">Thrown when the <paramref name="providers"/> contains
 /// duplicate elements, null references or no elements.</exception>
 /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="threshold"/> has an
 /// invalid value.</exception>
 public CompositeLoggingProvider(LoggingEventType threshold, LoggingProviderBase fallbackProvider,
                                 params LoggingProviderBase[] providers)
     : this(threshold, fallbackProvider, (IEnumerable <LoggingProviderBase>)providers)
 {
 }