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 CanCreateInstanceFromGivenName()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(EventLogTraceListener), "Entlib Tests");

            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(listener.GetType(), typeof(EventLogTraceListener));
            Assert.AreEqual("Entlib Tests", ((EventLogTraceListener)listener).EventLog.Source);
        }
        public void CanCreateInstanceFromGivenName()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(DelimitedListTraceListener), "log.txt");

            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(DelimitedListTraceListener), listener.GetType());
        }
        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 CanCreateCategoryFilterFromEmptyCategoryConfiguration()
        {
            var categoryEntries = new NamedElementCollection<CategoryFilterEntry>();
            var filterData = new CategoryFilterData("category", categoryEntries, CategoryFilterMode.DenyAllExceptAllowed);

            var helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(CategoryFilter));
            Assert.AreEqual(0, ((CategoryFilter)filter).CategoryFilters.Count);
            Assert.AreEqual(CategoryFilterMode.DenyAllExceptAllowed, ((CategoryFilter)filter).CategoryFilterMode);
        }
        public void CanCreateInstanceFromGivenName()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(TextWriterTraceListener), "log.txt");
            listenerData.TraceOutputOptions = TraceOptions.Callstack;

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

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

            Assert.IsNotNull(listener);
            Assert.AreEqual(listener.GetType(), typeof(TextWriterTraceListener));
            Assert.AreEqual("listener\u200cimplementation", listener.Name);
            Assert.AreEqual(TraceOptions.Callstack, listener.TraceOutputOptions);
        }
        public void CanCreateInstanceFromGivenName()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(DelimitedListTraceListener), "log.txt");

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

            var container = EnterpriseLibraryContainer.CreateDefaultContainer(helper.configurationSource);

            TraceListener listener = container.GetInstance<TraceListener>("listener\u200cimplementation");

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);
            Assert.AreEqual(typeof(DelimitedListTraceListener), listener.GetType());
        }
        public void CanCreateInstanceFromGivenConfigurationWithAttributes()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(DelimitedListTraceListener), "log.txt");
            listenerData.SetAttributeValue("delimiter", "||");

            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 = (DelimitedListTraceListener)listener;
            Assert.AreEqual("||", innerListener.Delimiter);
        }
        public void CanBuildCustomLogFilterFromGivenConfiguration()
        {
            CustomLogFilterData filterData
                = new CustomLogFilterData("custom", typeof(MockCustomLogFilter));
            filterData.SetAttributeValue(MockCustomProviderBase.AttributeKey, "value1");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = 
                EnterpriseLibraryContainer.CreateDefaultContainer(helper.configurationSource)
                    .GetInstance<ILogFilter>(filterData.Name);

            Assert.IsNotNull(filter);
            Assert.AreSame(typeof(MockCustomLogFilter), filter.GetType());
            Assert.AreEqual("value1", ((MockCustomLogFilter)filter).customValue);
        }
        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 CanCreateInstanceFromGivenConfigurationWithAttributes()
        {
            SystemDiagnosticsTraceListenerData listenerData
                = new SystemDiagnosticsTraceListenerData("listener", typeof(DelimitedListTraceListener), "log.txt");
            listenerData.SetAttributeValue("delimiter", "||");

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

            var container = EnterpriseLibraryContainer.CreateDefaultContainer(helper.configurationSource);

            TraceListener listener = container.GetInstance<TraceListener>("listener\u200cimplementation");

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

            var innerListener =
                (DelimitedListTraceListener)((AttributeSettingTraceListenerWrapper)listener).InnerTraceListener;
            Assert.AreEqual("||", innerListener.Delimiter);
        }
        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 = GetListener("listener\u200cimplementation", helper.configurationSource);

            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);
        }
        public void CanCreatePriorityFilterFromConfiguration()
        {
            PriorityFilterData filterData = new PriorityFilterData(1000);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(PriorityFilter));
            Assert.AreEqual(1000, ((PriorityFilter)filter).MinimumPriority);
        }
        public void PriorityFilterMaximumPriotDefaultsToMaxIntWhenNotSpecified()
        {
            PriorityFilterData filterData = new PriorityFilterData(1000);
            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(PriorityFilter));
            Assert.AreEqual(int.MaxValue, ((PriorityFilter)filter).MaximumPriority);
        }
        public void PriorityFilterShouldNotLogWhenPriotityIsAboveMaxPriority()
        {
            var filterData = new PriorityFilterData(0)
            {
                MaximumPriority = 100
            };

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(PriorityFilter));
            Assert.IsTrue(((PriorityFilter)filter).ShouldLog(100));
            Assert.IsFalse(((PriorityFilter)filter).ShouldLog(101));
        }
        public void CanCreateLogEnabledFilterFromConfiguration()
        {
            LogEnabledFilterData filterData = new LogEnabledFilterData(true);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(LogEnabledFilter));
            Assert.AreEqual(true, ((LogEnabledFilter)filter).Enabled);
        }
        public void CanCreateInstanceFromGivenName()
        {
            TraceListenerData listenerData = new MsmqTraceListenerData("listener", CommonUtil.MessageQueuePath, formatterName);

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

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

            Assert.IsNotNull(listener);
            Assert.AreEqual(listener.GetType(), typeof(MsmqTraceListener));

            MsmqTraceListener msmqTraceListener = listener as MsmqTraceListener;
            Assert.AreEqual("listener", listener.Name);
            Assert.IsNotNull(msmqTraceListener.Formatter);
            Assert.AreEqual(msmqTraceListener.Formatter.GetType(), typeof(BinaryLogFormatter));
            Assert.AreEqual(CommonUtil.MessageQueuePath, msmqTraceListener.QueuePath);
        }
        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);
        }
        public void CanCreateCategoryFilterFromNonEmptyCategoryConfiguration()
        {
            NamedElementCollection<CategoryFilterEntry> categoryEntries = new NamedElementCollection<CategoryFilterEntry>();
            categoryEntries.Add(new CategoryFilterEntry("category1"));
            categoryEntries.Add(new CategoryFilterEntry("category2"));
            categoryEntries.Add(new CategoryFilterEntry("category3"));
            CategoryFilterData filterData = new CategoryFilterData("category", categoryEntries, CategoryFilterMode.AllowAllExceptDenied);

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.LogFilters.Add(filterData);

            ILogFilter filter = GetFilter(filterData.Name, helper.configurationSource);

            Assert.IsNotNull(filter);
            Assert.AreEqual(filter.GetType(), typeof(CategoryFilter));
            Assert.AreEqual(3, ((CategoryFilter)filter).CategoryFilters.Count);
            Assert.IsTrue(((CategoryFilter)filter).CategoryFilters.Contains("category1"));
            Assert.IsTrue(((CategoryFilter)filter).CategoryFilters.Contains("category2"));
            Assert.IsTrue(((CategoryFilter)filter).CategoryFilters.Contains("category3"));
            Assert.IsFalse(((CategoryFilter)filter).CategoryFilters.Contains("category4"));
            Assert.AreEqual(CategoryFilterMode.AllowAllExceptDenied, ((CategoryFilter)filter).CategoryFilterMode);
        }
        public void CanCreateInstanceFromGivenName()
        {


            FormattedEventLogTraceListenerData listenerData = new FormattedEventLogTraceListenerData("listener", "unknown source", "formatter");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "some template"));
            helper.loggingSettings.TraceListeners.Add(listenerData);

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

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(FormattedEventLogTraceListener));
            FormattedEventLogTraceListener castedListener = (FormattedEventLogTraceListener)listener;
            Assert.IsNotNull(castedListener.Formatter);
            Assert.AreEqual("unknown source", ((EventLogTraceListener)castedListener.InnerListener).EventLog.Source);
            Assert.AreEqual(FormattedEventLogTraceListener.DefaultMachineName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.MachineName);
            Assert.AreEqual(castedListener.Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("some template", ((TextFormatter)castedListener.Formatter).Template);
            
            try
            {
                Assert.AreEqual(FormattedEventLogTraceListener.DefaultLogName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.Log);
            }
            catch (SecurityException ex)
            {
                Assert.Inconclusive("In order to run the tests, please run Visual Studio as Administrator.\r\n{0}", ex.ToString());
            }
        }
        public void CanCreateInstanceFromGiveName()
        {
            FlatFileTraceListenerData listenerData = new FlatFileTraceListenerData("listener", "log.txt", "---header---", "+++footer+++", "formatter");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.TraceListeners.Add(listenerData);
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "some template"));

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

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(FlatFileTraceListener));
            Assert.IsNotNull(((FlatFileTraceListener)listener).Formatter);
            Assert.AreEqual(((FlatFileTraceListener)listener).Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("some template", ((TextFormatter)((FlatFileTraceListener)listener).Formatter).Template);
        }
        public void CanCreateInstanceFromGivenName()
        {
            FormattedEventLogTraceListenerData listenerData = new FormattedEventLogTraceListenerData("listener", "unknown source", "formatter");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template"));
            helper.loggingSettings.TraceListeners.Add(listenerData);

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

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(FormattedEventLogTraceListener));
            FormattedEventLogTraceListener castedListener = (FormattedEventLogTraceListener)listener;
            Assert.IsNotNull(castedListener.Formatter);
            Assert.AreEqual("unknown source", ((EventLogTraceListener)castedListener.InnerListener).EventLog.Source);
            Assert.AreEqual(FormattedEventLogTraceListener.DefaultLogName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.Log);
            Assert.AreEqual(FormattedEventLogTraceListener.DefaultMachineName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.MachineName);
            Assert.AreEqual(castedListener.Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("foobar template", ((TextFormatter)castedListener.Formatter).Template);
        }
        public void CanCreateInstanceFromGivenConfiguration()
        {
            RollingFlatFileTraceListenerData listenerData = new RollingFlatFileTraceListenerData("listener", "log.txt", "header", "footer", 10, "yyyy-MM-dd", RollFileExistsBehavior.Increment,
                                                                                                 RollInterval.Minute, TraceOptions.DateTime, "SimpleTextFormat");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.Formatters.Add(new TextFormatterData("SimpleTextFormat", "foobar template"));
            helper.loggingSettings.TraceListeners.Add(listenerData);

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

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(RollingFlatFileTraceListener));
        }
        public void CanCreateInstanceFromGivenName()
        {
            EmailTraceListenerData listenerData =
                new EmailTraceListenerData("listener", "[email protected]", "*****@*****.**", "EntLib-Logging:",
                                           "has occurred", "smtphost", 25, "formatter");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();
            helper.loggingSettings.TraceListeners.Add(listenerData);
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template"));

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

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener\u200cimplementation", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(EmailTraceListener));
            Assert.IsNotNull(((EmailTraceListener)listener).Formatter);
            Assert.AreEqual(((EmailTraceListener)listener).Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("foobar template", ((TextFormatter)((EmailTraceListener)listener).Formatter).Template);
        }
        public void CreationWithMissingFormatterDataThrows()
        {
            MockLogObjectsHelper helper = new MockLogObjectsHelper();

            LogFormatterCustomFactory.Instance.Create(context, "formatter", helper.configurationSource, reflectionCache);
        }