public void CanCreatePoliciesForProviderWithFormatter()
        {
            FormattedDatabaseTraceListenerData listenerData
                = new FormattedDatabaseTraceListenerData("listener", "write", "add", "database", "formatter");
            listenerData.TraceOutputOptions = TraceOptions.Callstack | TraceOptions.ProcessId;
            listenerData.Filter = SourceLevels.Error;
            loggingSettings.TraceListeners.Add(listenerData);

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

            connectionStringsSection.ConnectionStrings.Add(
                new ConnectionStringSettings("database", "foo=bar;", "System.Data.SqlClient"));

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

                Assert.IsNotNull(createdObject);
                Assert.AreEqual(listenerData.TraceOutputOptions, createdObject.TraceOutputOptions);
                Assert.IsNotNull(createdObject.Filter);
                Assert.IsInstanceOfType(createdObject.Filter, typeof(EventTypeFilter));
                Assert.AreEqual(listenerData.Filter, ((EventTypeFilter)createdObject.Filter).EventType);
                Assert.IsNotNull(createdObject.Formatter);
                Assert.AreSame(typeof(TextFormatter), createdObject.Formatter.GetType());
                Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
            }
        }
Ejemplo n.º 2
0
        public ILogFormatter Assemble(IBuilderContext context, FormatterData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            TextFormatterData castedObjectConfiguration = (TextFormatterData)objectConfiguration;
            ILogFormatter     createdObject             = new TextFormatter(castedObjectConfiguration.Template);

            return(createdObject);
        }
        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 CanCreateLogFormatter()
        {
            FormatterData data = new TextFormatterData("name", "template");
            loggingSettings.Formatters.Add(data);

            InitializeContainer();

            TextFormatter createdObject = (TextFormatter)container.Resolve<ILogFormatter>("name");

            Assert.IsNotNull(createdObject);
            Assert.AreEqual("template", createdObject.Template);
        }
		public void CanCreatePoliciesForTextFormatter()
		{
			FormatterData data = new TextFormatterData("name", "template");
			loggingSettings.Formatters.Add(data);

		    using (var container = CreateContainer())
		    {
		        TextFormatter createdObject = (TextFormatter)container.Resolve<ILogFormatter>("name");

		        Assert.IsNotNull(createdObject);
		        Assert.AreEqual("template", createdObject.Template);
		    }
		}
Ejemplo n.º 6
0
        public void CanCreateFormatterFromFactory()
        {
            FormatterData data = new TextFormatterData("ignore", "template");
            LoggingSettings settings = new LoggingSettings();
            settings.Formatters.Add(data);
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
            configurationSource.Add(LoggingSettings.SectionName, settings);

            ILogFormatter formatter = LogFormatterCustomFactory.Instance.Create(context, "ignore", configurationSource, reflectionCache);

            Assert.IsNotNull(formatter);
            Assert.AreEqual(formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("template", ((TextFormatter)formatter).Template);
        }
 public void Given()
 {
     this.formatterData = new TextFormatterData("formatterName", "someTemplate");
 }
Ejemplo n.º 8
0
		public void CanCreateTextFormatterFromUnnamedDataBug1883()
		{
			string template = "{message}";
			TextFormatterData data = new TextFormatterData(template);

			ILogFormatter formatter = LogFormatterCustomFactory.Instance.Create(context, data, null, reflectionCache);

			Assert.AreSame(typeof(TextFormatter), formatter.GetType());
			Assert.AreEqual(template, ((TextFormatter)formatter).Template);
		}
        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 CanCreatePoliciesForRollingFlatFileTraceListenerWithFormatter()
        {
            RollingFlatFileTraceListenerData listenerData
                = new RollingFlatFileTraceListenerData(
                    "listener",
                    Path.Combine(Environment.CurrentDirectory, "test.log"),
                    "header",
                    "footer",
                    100,
                    "pattern",
                    RollFileExistsBehavior.Overwrite,
                    RollInterval.Midnight,
                    TraceOptions.Callstack | TraceOptions.ProcessId,
                    "formatter");
            listenerData.Filter = SourceLevels.Error;
            loggingSettings.TraceListeners.Add(listenerData);

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

            using (var container = CreateContainer())
            {
                RollingFlatFileTraceListener createdObject =
                    (RollingFlatFileTraceListener)((ReconfigurableTraceListenerWrapper)container.Resolve<TraceListener>("listener")).InnerTraceListener;

                Assert.IsNotNull(createdObject);
                Assert.AreEqual("listener\u200cimplementation", createdObject.Name);
                Assert.AreEqual(listenerData.TraceOutputOptions, createdObject.TraceOutputOptions);
                Assert.IsNotNull(createdObject.Filter);
                Assert.IsInstanceOfType(createdObject.Filter, typeof(EventTypeFilter));
                Assert.AreEqual(listenerData.Filter, ((EventTypeFilter)createdObject.Filter).EventType);
                Assert.IsNotNull(createdObject.Formatter);
                Assert.AreSame(typeof(TextFormatter), createdObject.Formatter.GetType());
                Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
                Assert.AreEqual(listenerData.FileName, ((FileStream)((StreamWriter)createdObject.Writer).BaseStream).Name);
            }
        }
        public void CanCreatePoliciesForFormattedEventLogTraceListenerWithFormatter()
        {
            FormattedEventLogTraceListenerData listenerData
                = new FormattedEventLogTraceListenerData("listener", CommonUtil.EventLogSourceName, CommonUtil.EventLogName, "machine", "formatter");
            listenerData.TraceOutputOptions = TraceOptions.Callstack | TraceOptions.ProcessId;
            listenerData.Filter = SourceLevels.Error;
            loggingSettings.TraceListeners.Add(listenerData);

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

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

                Assert.IsNotNull(createdObject);
                Assert.AreEqual(listenerData.TraceOutputOptions, createdObject.TraceOutputOptions);
                Assert.IsNotNull(createdObject.Filter);
                Assert.IsInstanceOfType(createdObject.Filter, typeof(EventTypeFilter));
                Assert.AreEqual(listenerData.Filter, ((EventTypeFilter)createdObject.Filter).EventType);
                Assert.IsNotNull(createdObject.Formatter);
                Assert.AreSame(typeof(TextFormatter), createdObject.Formatter.GetType());
                Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
                Assert.AreEqual(CommonUtil.EventLogSourceName, ((EventLogTraceListener)createdObject.InnerListener).EventLog.Source);
                Assert.AreEqual(CommonUtil.EventLogName, ((EventLogTraceListener)createdObject.InnerListener).EventLog.Log);
                Assert.AreEqual("machine", ((EventLogTraceListener)createdObject.InnerListener).EventLog.MachineName);
            }
        }
        public void CanCreatePoliciesForEmailTraceListenerWithFormatter()
        {
            EmailTraceListenerData listenerData
                = new EmailTraceListenerData("listener", "to address", "from address", "starter", "ender", "smtp", 25, "formatter");
            listenerData.TraceOutputOptions = TraceOptions.Callstack | TraceOptions.ProcessId;
            listenerData.Filter = SourceLevels.Error;
            loggingSettings.TraceListeners.Add(listenerData);

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

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

                Assert.IsNotNull(createdObject);
                Assert.AreEqual(listenerData.TraceOutputOptions, createdObject.TraceOutputOptions);
                Assert.IsNotNull(createdObject.Filter);
                Assert.IsInstanceOfType(createdObject.Filter, typeof(EventTypeFilter));
                Assert.AreEqual(listenerData.Filter, ((EventTypeFilter)createdObject.Filter).EventType);
                Assert.IsNotNull(createdObject.Formatter);
                Assert.AreSame(typeof(TextFormatter), createdObject.Formatter.GetType());
                Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
            }
        }
        public void Given()
        {
            formatter = new TextFormatterData("formatterName", "someTemplate");

            registration = formatter.GetRegistrations().First();
        }
        public void TraceListenerIsSingletonInContainer()
        {
            FormatterData data = new TextFormatterData("formattername", "template");
            loggingSettings.Formatters.Add(data);
            TraceListenerData traceListenerData = new FlatFileTraceListenerData("name", "filename.log", "formattername");
            loggingSettings.TraceListeners.Add(traceListenerData);

            InitializeContainer();

            TraceListener createdObject1 = container.Resolve<TraceListener>("name");
            TraceListener createdObject2 = container.Resolve<TraceListener>("name");

            Assert.AreSame(createdObject1, createdObject2);
        }
        public void CanCreateTraceListenerWithReferenceToFormatter()
        {
            FormatterData data = new TextFormatterData("formattername", "template");
            loggingSettings.Formatters.Add(data);
            TraceListenerData traceListenerData = new FlatFileTraceListenerData("name", "filename.log", "formattername");
            traceListenerData.Filter = SourceLevels.Critical;
            traceListenerData.TraceOutputOptions = TraceOptions.ProcessId;
            loggingSettings.TraceListeners.Add(traceListenerData);

            InitializeContainer();

            FlatFileTraceListener createdObject =
                (FlatFileTraceListener)container.Resolve<TraceListener>("name\u200cimplementation");

            Assert.IsNotNull(createdObject);
            Assert.AreEqual("name\u200cimplementation", createdObject.Name);
            Assert.AreEqual(SourceLevels.Critical, ((EventTypeFilter)createdObject.Filter).EventType);
            Assert.AreEqual(TraceOptions.ProcessId, createdObject.TraceOutputOptions);
            Assert.IsNotNull(createdObject.Formatter);
            Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
        }