public void CanDeserializeSerializedConfiguration()
		{
			string name = "name";
			string write = "write";
			string add = "add";
			string database = "database";
			string formatter = "formatter";

			TraceListenerData data = new FormattedDatabaseTraceListenerData(name, write, add, database, formatter, TraceOptions.Callstack);

			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, roSettigs.TraceListeners.Get(name).TraceOutputOptions);
			Assert.AreSame(typeof(FormattedDatabaseTraceListenerData), roSettigs.TraceListeners.Get(name).GetType());
			Assert.AreSame(typeof(FormattedDatabaseTraceListenerData), roSettigs.TraceListeners.Get(name).ListenerDataType);
			Assert.AreSame(typeof(FormattedDatabaseTraceListener), roSettigs.TraceListeners.Get(name).Type);
			Assert.AreEqual(add, ((FormattedDatabaseTraceListenerData)roSettigs.TraceListeners.Get(name)).AddCategoryStoredProcName);
			Assert.AreEqual(database, ((FormattedDatabaseTraceListenerData)roSettigs.TraceListeners.Get(name)).DatabaseInstanceName);
			Assert.AreEqual(formatter, ((FormattedDatabaseTraceListenerData)roSettigs.TraceListeners.Get(name)).Formatter);
			Assert.AreEqual(write, ((FormattedDatabaseTraceListenerData)roSettigs.TraceListeners.Get(name)).WriteLogStoredProcName);
		}
        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);
            }
        }
 public void Setup()
 {
     listenerData =
         new FormattedDatabaseTraceListenerData("listener", "write", "add", "database", "formatter")
         {
             Filter = SourceLevels.Warning,
             TraceOutputOptions = TraceOptions.Callstack | TraceOptions.DateTime
         };
 }
            public SendToDatabaseTraceListenerBuilder(ILoggingConfigurationSendTo context, string listenerName)
                : base(context)
            {
                databaseTraceListener = new FormattedDatabaseTraceListenerData
                {
                    Name = listenerName
                };

                base.AddTraceListenerToSettingsAndCategory(databaseTraceListener);
            }
		public void ListenerDataIsCreatedCorrectly()
		{
			FormattedDatabaseTraceListenerData listenerData = new FormattedDatabaseTraceListenerData("listener", "WriteLog", "AddCategory", "LoggingDb", "formatter");

			Assert.AreSame(typeof(FormattedDatabaseTraceListener), listenerData.Type);
			Assert.AreSame(typeof(FormattedDatabaseTraceListenerData), listenerData.ListenerDataType);
			Assert.AreEqual("listener", listenerData.Name);
			Assert.AreEqual("WriteLog", listenerData.WriteLogStoredProcName);
			Assert.AreEqual("AddCategory", listenerData.AddCategoryStoredProcName);
			Assert.AreEqual("LoggingDb", listenerData.DatabaseInstanceName);
			Assert.AreEqual("formatter", listenerData.Formatter);
		}
        public void CanCreateInstanceFromGivenName()
        {
            FormattedDatabaseTraceListenerData listenerData = new FormattedDatabaseTraceListenerData("listener", "WriteLog", "AddCategory", "LoggingDb", "formatter");
            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template"));
            helper.loggingSettings.TraceListeners.Add(listenerData);

            TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", helper.configurationSource, reflectionCache);

            Assert.IsNotNull(listener);
            Assert.AreEqual(listener.GetType(), typeof(FormattedDatabaseTraceListener));
            Assert.IsNotNull(((FormattedDatabaseTraceListener)listener).Formatter);
            Assert.AreEqual(((FormattedDatabaseTraceListener)listener).Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("foobar template", ((TextFormatter)((FormattedDatabaseTraceListener)listener).Formatter).Template);
        }
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds a <see cref="FormattedDatabaseTraceListener"/> based on an instance of <see cref="FormattedDatabaseTraceListenerData"/>.
        /// </summary>
        /// <seealso cref="TraceListenerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="FormattedDatabaseTraceListenerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="FormattedDatabaseTraceListener"/>.</returns>
        public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            FormattedDatabaseTraceListenerData castedObjectConfiguration
                = (FormattedDatabaseTraceListenerData)objectConfiguration;

            Data.Database database =
                (Data.Database)context.HeadOfChain.BuildUp(context, typeof(Data.Database), null, castedObjectConfiguration.DatabaseInstanceName);
            ILogFormatter formatter
                = LogFormatterCustomFactory.Instance.Create(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache);

            TraceListener createdObject
                = new FormattedDatabaseTraceListener(
                      database,
                      castedObjectConfiguration.WriteLogStoredProcName,
                      castedObjectConfiguration.AddCategoryStoredProcName,
                      formatter);

            return(createdObject);
        }
        public override TraceListener Assemble(IBuilderContext context,
                                               TraceListenerData objectConfiguration,
                                               IConfigurationSource configurationSource,
                                               ConfigurationReflectionCache reflectionCache)
        {
            FormattedDatabaseTraceListenerData castedObjectConfiguration
                = (FormattedDatabaseTraceListenerData)objectConfiguration;
            IBuilderContext databaseContext
                = context.CloneForNewBuild(
                      NamedTypeBuildKey.Make <Data.Database>(castedObjectConfiguration.DatabaseInstanceName), null);

            Data.Database database
                = (Data.Database)databaseContext.Strategies.ExecuteBuildUp(databaseContext);
            ILogFormatter formatter
                = GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache);
            TraceListener createdObject
                = new FormattedDatabaseTraceListener(
                      database,
                      castedObjectConfiguration.WriteLogStoredProcName,
                      castedObjectConfiguration.AddCategoryStoredProcName,
                      formatter);

            return(createdObject);
        }
        public void CanCreateInstanceFromGivenName()
        {
            FormattedDatabaseTraceListenerData listenerData =
                new FormattedDatabaseTraceListenerData("listener", "WriteLog", "AddCategory", "LoggingDb", "formatter")
                {
                    TraceOutputOptions = TraceOptions.Callstack | TraceOptions.DateTime,
                    Filter = SourceLevels.Information
                };
            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "some template"));
            helper.loggingSettings.TraceListeners.Add(listenerData);

            var listener = (FormattedDatabaseTraceListener)GetListener("listener", helper.configurationSource);

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(TraceOptions.Callstack | TraceOptions.DateTime, listener.TraceOutputOptions);
            Assert.IsNotNull(listener.Filter);
            Assert.AreEqual(SourceLevels.Information, ((EventTypeFilter)listener.Filter).EventType);
            Assert.AreEqual("WriteLog", listener.WriteLogStoredProcName);
            Assert.AreEqual("AddCategory", listener.AddCategoryStoredProcName);
            Assert.AreEqual(@"server=(localdb)\v11.0;database=Logging;Integrated Security=true", listener.Database.ConnectionString);
            Assert.IsNotNull(listener.Formatter);
            Assert.AreEqual(listener.Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("some template", ((TextFormatter)listener.Formatter).Template);
        }