示例#1
0
        protected static void LoadConfiguration(IApplicationLogSettings settings)
        {
            try
            {
                if (settings == null)
                {
                    throw new LogConfigurationException(LogResources.MissingConfiguration);
                }

                #region Custom Locator

                var providerType = settings.CustomLocatorAdapter;

                if (string.IsNullOrWhiteSpace(providerType))
                {
                    throw new LogConfigurationException(LogResources.Configuration_MissingCustomLocatorProvider);
                }

                var provider = GetServiceLocatorDelegate(providerType);

                LocatorProvider.SetLocator(provider);

                #endregion

                var taskFactory = TaskScheduling.Utility.GetTaskFactory(settings);

                var categories = new Dictionary <string, LoggerElementCollection>();

                foreach (CategoryElement category in settings.Categories)
                {
                    categories[category.Name] = category.Loggers;
                }

                if (!string.IsNullOrWhiteSpace(settings.CustomFormatter))
                {
                    var type = Type.GetType(settings.CustomFormatter);

                    if (type == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomFormatter_NotFound);
                    }

                    var formatter = Activator.CreateInstance(type) as ILogEntryFormatter;

                    if (formatter == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomFormatter_Invalid);
                    }

                    FormattingFactory.SetFormatter(formatter);
                }

                //Set Compression Provider
                Compression.ICompressionProvider compressionProvider;
                switch (settings.CompressionType)
                {
                case CompressionTypeOptions.Zip:
                    compressionProvider = new Compression.GZipCompressionProvider();
                    break;

                case CompressionTypeOptions.Deflate:
                    compressionProvider = new Compression.DeflateCompressionProvider();
                    break;

                case CompressionTypeOptions.Custom:

                    if (string.IsNullOrWhiteSpace(settings.CustomCompressionType))
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_NotFound);
                    }

                    var type = Type.GetType(settings.CustomCompressionType);

                    if (type == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_NotFound);
                    }

                    compressionProvider = Activator.CreateInstance(type) as Compression.ICompressionProvider;

                    if (compressionProvider == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_Invalid);
                    }
                    break;

                default:
                    throw new LogConfigurationException(
                              string.Format(LogResources.CompressionType_NotSupported,
                                            Enum.GetName(typeof(CompressionTypeOptions), settings.CompressionType)));
                }

                Compression.CompressionProviderFactory.CurrentProvider = compressionProvider;

                LogSection  = settings;
                Categories  = categories;
                TaskFactory = taskFactory;

                FailSafeLogFactory.SetFailOverLoggingBehaviour(settings.ReThrowLogExceptions == SwitchOptions.On);
            }
            catch (LogConfigurationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new LogConfigurationException(LogResources.LogConfigurationError_Generic, ex);
            }
        }
示例#2
0
        private void UpdateTagPair(string updatedText, string fullText, ITagPair tagPair, ConversionItem conversionItem)
        {
            var parent = tagPair.Parent;

            if (ContainsTags(updatedText))
            {
                try
                {
                    // Check if element name changed
                    if (TagsChanged(updatedText, fullText))
                    {
                        ReplaceTagPair(updatedText, tagPair, parent, conversionItem);
                    }
                    else
                    {
                        // Check if the attributes changed
                        if (AttributesChanged(updatedText, fullText))
                        {
                            var attributes = XElement.Parse(updatedText).Attributes();
                            var formatting = tagPair.StartTagProperties.Formatting;

                            var match         = Regex.Match(updatedText, "(<.+?>)(.+?)(</.+?>)");
                            var oldTagContent = tagPair.StartTagProperties.TagContent.Split(new[] { ' ', '>' });

                            tagPair.StartTagProperties.DisplayText = match.Groups[1].Value;
                            tagPair.StartTagProperties.TagContent  = match.Groups[1].Value;

                            foreach (var attrib in attributes)
                            {
                                var formattingItem = FormattingFactory.CreateFormatting(attrib.Name.LocalName, attrib.Value);
                                formatting.Add(formattingItem);

                                var metaDataList = new List <KeyValuePair <string, string> >();
                                foreach (var data in tagPair.TagProperties.MetaData)
                                {
                                    foreach (var piece in oldTagContent)
                                    {
                                        var m = Regex.Match(piece, "(.+?)=\"(.+?)\"");
                                        if (m.Success)
                                        {
                                            var updated = data.Value.Replace($"\"{m.Groups[2].Value}\"", $"\"{attrib.Value}\"");

                                            if (!ReferenceEquals(data.Value, updated))
                                            {
                                                metaDataList.Add(new KeyValuePair <string, string>(data.Key, updated));
                                            }
                                        }
                                    }
                                }

                                foreach (var pair in metaDataList)
                                {
                                    tagPair.TagProperties.SetMetaData(pair.Key, pair.Value);
                                }
                            }
                        }

                        if (ContentChanged(updatedText, fullText))
                        {
                            UpdateTagContent(updatedText, tagPair, conversionItem);
                        }
                    }
                }
                catch (ArgumentException)
                {
                    Reporter.Report(this, ErrorLevel.Error, "Error parsing updated text. Xml invalid.", $"Error: {updatedText}");
                }
            }
            else
            {
                var itext = CreateIText(updatedText);
                parent.Add(itext);

                tagPair.RemoveFromParent();
            }
        }