public void SetUp()
		{
			configurationSource = new DictionaryConfigurationSourceWithHandlersQuery();

			InstrumentationConfigurationSection instrumentationConfig = new InstrumentationConfigurationSection(true, true, true);
			configurationSource.Add(InstrumentationConfigurationSection.SectionName, instrumentationConfig);

			settings = new LoggingSettings();
			configurationSource.Add(LoggingSettings.SectionName, settings);

			settings.SpecialTraceSources.ErrorsTraceSource
				= new TraceSourceData("error", SourceLevels.Off);

			TraceSourceData traceSourceData = new TraceSourceData("blocking", SourceLevels.All);
			traceSourceData.TraceListeners.Add(new TraceListenerReferenceData("listener1"));
			settings.TraceSources.Add(traceSourceData);
			traceSourceData = new TraceSourceData("nonblocking", SourceLevels.All);
			traceSourceData.TraceListeners.Add(new TraceListenerReferenceData("listener2"));
			settings.TraceSources.Add(traceSourceData);

			TraceListenerData traceListenerData = new CustomTraceListenerData("listener1", typeof(MockBlockingCustomTraceListener), "init 1");
			settings.TraceListeners.Add(traceListenerData);
			traceListenerData = new MockTraceListenerData("listener2");
			settings.TraceListeners.Add(traceListenerData);

			reflectionCache = new ConfigurationReflectionCache();

			MockTraceListener.Reset();
			MockBlockingCustomTraceListener.Reset();
		}
        ConfigurationSection GetLoggingSettings()
        {
            LoggingSettings loggingConfiguration = null;

            if (!_cache.ContainsKey(LoggingSettingsKey))
            {
                lock (_sync)
                {
                    if (!_cache.ContainsKey(LoggingSettingsKey))
                    {
                        loggingConfiguration = new LoggingSettings();

                        loggingConfiguration.Name = "Logging Application Block";
                        loggingConfiguration.TracingEnabled = true;
                        loggingConfiguration.DefaultCategory = "General";
                        loggingConfiguration.LogWarningWhenNoCategoriesMatch = true;

                        var customTraceListenerData = new CustomTraceListenerData
                                                          {
                                                              ListenerDataType =
                                                                  Type.GetType(
                                                                  "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"),
                                                              TraceOutputOptions = TraceOptions.None,
                                                              Filter = SourceLevels.All,
                                                              Type =
                                                                  Type.GetType(
                                                                  "NCommons.Logging.EnterpriseLibrary.Specs.TraceListenerSpy, NCommons.Logging.EnterpriseLibrary.Specs"),
                                                              Name = "Custom Trace Listener",
                                                              Formatter = "Text Formatter"
                                                          };

                        loggingConfiguration.TraceListeners.Add(customTraceListenerData);

                        var textFormatterData = new TextFormatterData
                                                    {
                                                        Name = "Text Formatter",
                                                        Type =
                                                            Type.GetType(
                                                            "Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"),
                                                        Template = "{message}"
                                                    };

                        loggingConfiguration.Formatters.Add(textFormatterData);

                        loggingConfiguration.SpecialTraceSources.AllEventsTraceSource.TraceListeners.Add(
                            new TraceListenerReferenceData("Custom Trace Listener"));
                        loggingConfiguration.SpecialTraceSources.AllEventsTraceSource.DefaultLevel =
                            SourceLevels.All;
                        loggingConfiguration.SpecialTraceSources.AllEventsTraceSource.Name = "All Events";

                        _cache[LoggingSettingsKey] = loggingConfiguration;
                    }
                }
            }

            return _cache[LoggingSettingsKey];
        }
        public void CreationOfCustomTraceListenerWithoutRequiredSignatureConstructorThrows()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListenerWithInvalidConstructor), initializationData);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.TraceListeners.Add(listenerData);
            TraceListener listener = GetListener("listener", helper.configurationSource);
        }
 public void Given()
 {
     listenerData =
         new CustomTraceListenerData(
              "custom trace listener",
              typeof(System.Diagnostics.TextWriterTraceListener),
              "someInitData")
         {
             Formatter = "formatter"
         };
 }
 public void Given()
 {
     listenerData =
         new CustomTraceListenerData(
              "custom trace listener",
              typeof(MockCustomTraceListener),
              "someInitData")
         {
             Formatter = "formatter"
         };
 }
예제 #6
0
        public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            TraceListener createdObject = base.Assemble(context, objectConfiguration, configurationSource, reflectionCache);

            if (createdObject is CustomTraceListener)
            {
                CustomTraceListenerData castedObjectConfiguration
                    = (CustomTraceListenerData)objectConfiguration;
                ILogFormatter formatter = GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache);
                ((CustomTraceListener)createdObject).Formatter = formatter;
            }
            return(createdObject);
        }
		public void CanCreateInstanceFromGivenConfiguration()
		{
			CustomTraceListenerData listenerData
				= new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);

			MockLogObjectsHelper helper = new MockLogObjectsHelper();
			TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, listenerData, helper.configurationSource,reflectionCache);

			Assert.IsNotNull(listener);
			Assert.AreEqual("listener", listener.Name);
			Assert.AreEqual(typeof(MockCustomTraceListener), listener.GetType());
			Assert.AreEqual(initializationData, ((MockCustomTraceListener)listener).initData);
		}
        public void CanCreateInstanceFromGivenName()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.TraceListeners.Add(listenerData);
            TraceListener listener = GetListener("listener", helper.configurationSource);

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(typeof(MockCustomTraceListener), listener.GetType());
            Assert.AreEqual(initializationData, ((MockCustomTraceListener)listener).initData);
        }
        public void CanCreateInstanceFromConfigurationFile()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);
            LoggingSettings loggingSettings = new LoggingSettings();
            loggingSettings.TraceListeners.Add(listenerData);

            TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings), reflectionCache);

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(typeof(MockCustomTraceListener), listener.GetType());
            Assert.AreEqual(initializationData, ((MockCustomTraceListener)listener).initData);
            Assert.IsNull(((MockCustomTraceListener)listener).Formatter);
            Assert.AreEqual(attributeValue, ((MockCustomTraceListener)listener).Attribute);
        }
        public void CanCreateInstanceFromGivenConfigurationWithAttributes()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.TraceListeners.Add(listenerData);

            TraceListener listener = GetListener("listener", helper.configurationSource);

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);

            var innerListener = (MockCustomTraceListener)listener;

            Assert.AreEqual(initializationData, innerListener.initData);
            Assert.AreEqual(attributeValue, innerListener.Attribute);
        }
        public void CanDeserializeSerializedConfiguration()
        {
            string name = "name";
            string initData = "init data";
            string attributeName = "attribute";
            string attributeValue = "value";

            CustomTraceListenerData data = new CustomTraceListenerData(name, typeof(MockCustomTraceListener), initData, TraceOptions.Callstack);
            data.SetAttributeValue(attributeName, attributeValue);

            LoggingSettings settings = new LoggingSettings();
            settings.TraceListeners.Add(data);

            IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();
            sections[LoggingSettings.SectionName] = settings;
            IConfigurationSource configurationSource
                = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

            LoggingSettings roSettigs = (LoggingSettings)configurationSource.GetSection(LoggingSettings.SectionName);

            Assert.AreEqual(1, roSettigs.TraceListeners.Count);
            Assert.IsNotNull(roSettigs.TraceListeners.Get(name));
            Assert.AreEqual(TraceOptions.Callstack, ((CustomTraceListenerData)roSettigs.TraceListeners.Get(name)).TraceOutputOptions);
            Assert.AreSame(typeof(CustomTraceListenerData), roSettigs.TraceListeners.Get(name).GetType());
            Assert.AreSame(typeof(CustomTraceListenerData), roSettigs.TraceListeners.Get(name).ListenerDataType);
            Assert.AreSame(typeof(MockCustomTraceListener), roSettigs.TraceListeners.Get(name).Type);
            Assert.AreEqual(attributeValue, ((CustomTraceListenerData)roSettigs.TraceListeners.Get(name)).Attributes[attributeName]);
        }
 public void WhenCreatingInstanceUsingDefaultContructor_ThenListenerDataTypeIsSet()
 {
     var listener = new CustomTraceListenerData();
     Assert.AreEqual(typeof(CustomTraceListenerData), listener.ListenerDataType);
 }
 public void Given()
 {
     listenerData = new CustomTraceListenerData("someName",
                                                typeof(MockCustomTraceListenerWithoutExpectedConstructor),
                                                "initData");
 }
예제 #14
0
 internal CustomTraceListenerDataHelper(CustomTraceListenerData helpedCustomProviderData)
     : base(helpedCustomProviderData)
 {
     propertiesCollection.Add(formatterProperty);
 }
        public void CanCreateInstanceWithoutInitializationDataFromConfigurationFile()
        {
            CustomTraceListenerData listenerData = new CustomTraceListenerData();
            listenerData.Name = "listener";
            listenerData.Type = typeof(MockCustomTraceListener);
            listenerData.ListenerDataType = typeof(CustomTraceListenerData);
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);
            LoggingSettings loggingSettings = new LoggingSettings();
            loggingSettings.TraceListeners.Add(listenerData);

            TraceListener listener = 
                GetListener("listener\u200cimplementation", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings));

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);

            var innerListener = (MockCustomTraceListener)(
                (AttributeSettingTraceListenerWrapper)listener).InnerTraceListener;

            Assert.IsNull(innerListener.initData);
            Assert.IsNull(innerListener.Formatter);
            Assert.AreEqual(attributeValue, innerListener.Attribute);
        }
        public void CanCreateInstanceWithFormatterFromConfigurationFile()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);
            listenerData.Formatter = "formatter";
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);
            LoggingSettings loggingSettings = new LoggingSettings();
            loggingSettings.TraceListeners.Add(listenerData);
            loggingSettings.Formatters.Add(new TextFormatterData("formatter", "template"));

            TraceListener listener = 
                GetListener("listener\u200cimplementation", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings));

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);

            var innerListener = (MockCustomTraceListener)(
                (AttributeSettingTraceListenerWrapper)listener).InnerTraceListener;

            Assert.AreEqual(initializationData, innerListener.initData);
            Assert.IsNotNull(innerListener.Formatter);
            Assert.AreSame(typeof(TextFormatter), innerListener.Formatter.GetType());
            Assert.AreEqual("template", ((TextFormatter)(innerListener.Formatter)).Template);
            Assert.AreEqual(attributeValue, innerListener.Attribute);
        }
        public void CanCreatePoliciesForCustomTraceListenerWithFormatter()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), "", TraceOptions.Callstack);
            listenerData.Formatter = "formatter";
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);
            loggingSettings.TraceListeners.Add(listenerData);

            FormatterData formatterData = new TextFormatterData("formatter", "template");
            loggingSettings.Formatters.Add(formatterData);

            using (var container = CreateContainer())
            {
                var createdObject =
                    container.Resolve<TraceListener>("listener\u200cimplementation") as AttributeSettingTraceListenerWrapper;

                Assert.IsNotNull(createdObject);
                Assert.AreEqual("listener\u200cimplementation", createdObject.Name);
                Assert.AreEqual(TraceOptions.Callstack, createdObject.TraceOutputOptions);
                Assert.AreEqual(typeof(MockCustomTraceListener), createdObject.InnerTraceListener.GetType());
                Assert.AreEqual(null, ((MockCustomTraceListener)createdObject.InnerTraceListener).initData);	// configured with "", but set to null
                Assert.AreEqual(attributeValue, ((MockCustomTraceListener)createdObject.InnerTraceListener).Attribute);
                Assert.IsNotNull(((MockCustomTraceListener)createdObject.InnerTraceListener).Formatter);
                Assert.AreSame(typeof(TextFormatter), ((MockCustomTraceListener)createdObject.InnerTraceListener).Formatter.GetType());
                Assert.AreEqual("template", ((TextFormatter)((MockCustomTraceListener)createdObject.InnerTraceListener).Formatter).Template);
            }
        }
		public void CreationOfCustomTraceListenerWithoutRequiredSignatureConstructorThrows()
		{
			CustomTraceListenerData listenerData
				= new CustomTraceListenerData("listener", typeof(MockCustomTraceListenerWithInvalidConstructor), initializationData);

			MockLogObjectsHelper helper = new MockLogObjectsHelper();
			TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, listenerData, helper.configurationSource,reflectionCache);
		}
        public void CanCreateInstanceFromConfigurationFile()
        {
            CustomTraceListenerData listenerData
                = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData);
            listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue);
            LoggingSettings loggingSettings = new LoggingSettings();
            loggingSettings.TraceListeners.Add(listenerData);

            TraceListener listener =
                GetListener("listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings));

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);

            var innerListener = (MockCustomTraceListener)listener;

            Assert.AreEqual(initializationData, innerListener.initData);
            Assert.IsNull(innerListener.Formatter);
            Assert.AreEqual(attributeValue, innerListener.Attribute);
        }
		internal CustomTraceListenerDataHelper(CustomTraceListenerData helpedCustomProviderData)
			: base(helpedCustomProviderData)
		{
			propertiesCollection.Add(formatterProperty);
		}