Beispiel #1
0
        public bool AddDiagnosticArea(DiagnosticsArea area)
        {
            bool retVal = false;

            var existingArea = configuredAreas[area.Name];

            if (existingArea == null)
            {
                configuredAreas.Add(area);
            }
            else
            {
                foreach (DiagnosticsCategory c in area.DiagnosticsCategories)
                {
                    var existingCategory = existingArea.DiagnosticsCategories[c.Name];
                    if (existingCategory == null)
                    {
                        existingArea.DiagnosticsCategories.Add(c);
                        retVal = true;
                    }
                }
            }

            return(retVal);
        }
        /// <summary>
        /// Registers the logger source.
        /// </summary>
        /// <param name="LoggingArea">The logging area.</param>
        /// <param name="categories">The area categories.</param>
        public static void RegisterLoggerSource(string LoggingArea, string[] categories)
        {
            DiagnosticsArea _newArea = new DiagnosticsArea(LoggingArea);

            foreach (string _cn in categories)
            {
                _newArea.DiagnosticsCategories.Add(new DiagnosticsCategory(_cn, EventSeverity.Verbose, TraceSeverity.Verbose));
            }
            IConfigManager            configMgr        = SharePointServiceLocator.GetCurrent().GetInstance <IConfigManager>();
            DiagnosticsAreaCollection _configuredAreas = new DiagnosticsAreaCollection(configMgr);
            var existingArea = _configuredAreas[_newArea.Name];

            if (existingArea == null)
            {
                _configuredAreas.Add(_newArea);
            }
            else
            {
                foreach (DiagnosticsCategory _dc in _newArea.DiagnosticsCategories)
                {
                    DiagnosticsCategory existingCategory = existingArea.DiagnosticsCategories[_dc.Name];
                    if (existingCategory == null)
                    {
                        existingArea.DiagnosticsCategories.Add(_dc);
                    }
                }
            }
            _configuredAreas.SaveConfiguration();
        }
        public void RegisterAreas_SkipRegisterIfEventSourceDoesExist()
        {
            //Arrange
            var             areas = new DiagnosticsAreaCollection();
            DiagnosticsArea area  = new DiagnosticsArea("test");

            areas.Add(area);

            MEventLog.SourceExistsString = (s) => true;

            string sourceName  = null;
            string logName     = null;
            int    registerCnt = 0;

            MEventLog.CreateEventSourceStringString = (s, l) =>
            {
                sourceName = s;
                logName    = l;
                registerCnt++;
            };

            // Act
            DiagnosticsAreaEventSource.RegisterAreas(areas);

            // Assert
            Assert.IsTrue(registerCnt == 0);
            Assert.IsNull(sourceName);
            Assert.IsNull(logName);
        }
        public void RegisterAreas_EventSourceDoesntExist()
        {
            //Arrange
            var             areas = new DiagnosticsAreaCollection();
            DiagnosticsArea area  = new DiagnosticsArea("test");

            areas.Add(area);

            MEventLog.SourceExistsString = (s) => false;

            string[] sourceNames = new string[2];
            int      sourceCnt   = 0;

            string [] logNames = new string [2];
            MEventLog.CreateEventSourceStringString = (s, l) =>
            {
                if (sourceCnt < 2)
                {
                    sourceNames[sourceCnt] = s;
                    logNames[sourceCnt]    = l;
                }

                sourceCnt++;
            };

            // Act
            DiagnosticsAreaEventSource.RegisterAreas(areas);

            // Assert
            Assert.IsTrue(sourceCnt == 2);
            Assert.AreEqual <string>(area.Name, sourceNames[0]);
            Assert.AreEqual <string>(Constants.EventLogName, logNames[0]);
            Assert.AreEqual <string>(DiagnosticsArea.DefaultSPDiagnosticsArea.Name, sourceNames[1]);
            Assert.AreEqual <string>(Constants.EventLogName, logNames[1]);
        }
        public void EnsureConfiguredAreasRegistered_RegistersConfiguredAreas()
        {
            //Arrange
            var areas      = new DiagnosticsAreaCollection();
            var newLocator = new ActivatingServiceLocator();

            SharePointServiceLocator.ReplaceCurrentServiceLocator(newLocator);
            newLocator.RegisterTypeMapping <IConfigManager, MockConfigManager>();
            var sourceNames = new List <string>();
            var logNames    = new List <string>();

            DiagnosticsArea area = new DiagnosticsArea("test");

            areas.Add(area);

            MEventLog.SourceExistsString = (s) => false;

            MEventLog.CreateEventSourceStringString = (s, l) =>
            {
                sourceNames.Add(s);
                logNames.Add(l);
            };

            // Act
            DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();

            // Assert
            Assert.AreEqual(3, sourceNames.Count);
            Assert.AreEqual(MockConfigManager.Area1Name, sourceNames[0]);
            Assert.AreEqual(MockConfigManager.Area2Name, sourceNames[1]);
            Assert.AreEqual <string>(Constants.EventLogName, logNames[0]);
            Assert.AreEqual <string>(Constants.EventLogName, logNames[1]);
            Assert.AreEqual(DiagnosticsArea.DefaultSPDiagnosticsArea.Name, sourceNames[2]);
            Assert.AreEqual <string>(Constants.EventLogName, logNames[2]);
        }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var configManager = SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();

            var configuredAreas = new DiagnosticsAreaCollection(configManager);

            foreach (var newArea in Areas)
            {
                var existingArea = configuredAreas[newArea.Name];

                if (existingArea == null)
                {
                    configuredAreas.Add(newArea);
                }
                else
                {
                    foreach (var category in newArea.DiagnosticsCategories)
                    {
                        var existingCategory = existingArea.DiagnosticsCategories[category.Name];
                        if (existingCategory == null)
                        {
                            existingArea.DiagnosticsCategories.Add(category);
                        }
                    }
                }
            }

            configuredAreas.SaveConfiguration();
            DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();
        }
        public void Add_WithDefaultConstructor()
        {
            //Arrange
            var target   = new DiagnosticsAreaCollection();
            var expected = new DiagnosticsArea(TestsConstants.TestGuidName);

            //Act
            target.Add(expected);

            //Assert
            Assert.AreEqual <DiagnosticsArea>(target[0], expected);
        }
        public void Add_AreaWithConfigMgrConstructorWithConfigData()
        {
            //Arrange
            var configMgr = new MockConfigManager();
            var target    = new DiagnosticsAreaCollection(configMgr);
            var expected  = new DiagnosticsArea(TestsConstants.TestGuidName);

            //Act
            target.Add(expected);

            //Assert
            Assert.AreEqual <DiagnosticsArea>(target[TestsConstants.TestGuidName], expected);
        }
        public void Add_DuplicateArea_ThrowsInvalidOperationException()
        {
            //Arrange
            var target = new DiagnosticsAreaCollection();
            var area   = new DiagnosticsArea(MockConfigManager.Area1Name);

            target.Add(area);
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                target.Add(area);
            }
            catch (InvalidOperationException)
            {
                expectedExceptionThrown = true;
            }

            //Assert
            Assert.IsTrue(expectedExceptionThrown);
        }
        public void SerializeDeserialize_WithAreasAndCategories()
        {
            //Arrange
            var target   = new ConfigSettingSerializer();
            var expected = new DiagnosticsAreaCollection();


            var area1 = new DiagnosticsArea(TestArea1Name);

            area1.DiagnosticsCategories.Add(new DiagnosticsCategory(TestCategory11Name, EventSeverity.ErrorCritical, TraceSeverity.Medium));
            area1.DiagnosticsCategories.Add(new DiagnosticsCategory(TestCategory12Name, EventSeverity.Information, TraceSeverity.Verbose));
            var area2 = new DiagnosticsArea(TestArea2Name);

            area2.DiagnosticsCategories.Add(new DiagnosticsCategory(TestCategory21Name, EventSeverity.Information, TraceSeverity.Monitorable));
            area2.DiagnosticsCategories.Add(new DiagnosticsCategory(TestCategory22Name, EventSeverity.Warning, TraceSeverity.Medium));
            expected.Add(area1);
            expected.Add(area2);

            //Act
            string stringValue      = target.Serialize(expected.GetType(), expected);
            var    conversionResult = target.Deserialize(expected.GetType(), stringValue) as DiagnosticsAreaCollection;

            //Assert
            Assert.AreEqual(expected.Count, conversionResult.Count);
            Assert.AreEqual(expected[0].DiagnosticsCategories.Count, conversionResult[0].DiagnosticsCategories.Count);
            Assert.AreEqual(expected[0].DiagnosticsCategories[0].Name, conversionResult[0].DiagnosticsCategories[0].Name);
            Assert.AreEqual(expected[0].DiagnosticsCategories[1].Name, conversionResult[0].DiagnosticsCategories[1].Name);
            Assert.AreEqual(expected[0].DiagnosticsCategories[0].EventSeverity, conversionResult[0].DiagnosticsCategories[0].EventSeverity);
            Assert.AreEqual(expected[0].DiagnosticsCategories[0].TraceSeverity, conversionResult[0].DiagnosticsCategories[0].TraceSeverity);
            Assert.AreEqual(expected[0].DiagnosticsCategories[1].EventSeverity, conversionResult[0].DiagnosticsCategories[1].EventSeverity);
            Assert.AreEqual(expected[0].DiagnosticsCategories[1].TraceSeverity, conversionResult[0].DiagnosticsCategories[1].TraceSeverity);
            Assert.AreEqual(expected[1].DiagnosticsCategories.Count, conversionResult[1].DiagnosticsCategories.Count);
            Assert.AreEqual(expected[1].DiagnosticsCategories[0].Name, conversionResult[1].DiagnosticsCategories[0].Name);
            Assert.AreEqual(expected[1].DiagnosticsCategories[1].Name, conversionResult[1].DiagnosticsCategories[1].Name);
            Assert.AreEqual(expected[1].DiagnosticsCategories[0].EventSeverity, conversionResult[1].DiagnosticsCategories[0].EventSeverity);
            Assert.AreEqual(expected[1].DiagnosticsCategories[0].TraceSeverity, conversionResult[1].DiagnosticsCategories[0].TraceSeverity);
            Assert.AreEqual(expected[1].DiagnosticsCategories[1].EventSeverity, conversionResult[1].DiagnosticsCategories[1].EventSeverity);
            Assert.AreEqual(expected[1].DiagnosticsCategories[1].TraceSeverity, conversionResult[1].DiagnosticsCategories[1].TraceSeverity);
        }
        public void Indexer_FindByStringIndexer()
        {
            //Arrange
            const string AreaName = "{E6C24B3D-8A81-4E0A-AAC9-85E5A5B9C09A}";
            var          target   = new DiagnosticsAreaCollection();
            var          expected = new DiagnosticsArea(AreaName);

            target.Add(expected);

            //Act
            DiagnosticsArea foundArea = target[AreaName];

            //Assert
            Assert.IsNotNull(foundArea);
            Assert.AreEqual <DiagnosticsArea>(expected, foundArea);
        }
        public void Add_WithConfigMgrConstructorWithoutConfigData()
        {
            //Arrange
            var configMgr = new SIConfigManager();
            var propBag   = new BIPropertyBag();

            configMgr.ContainsKeyInPropertyBagStringIPropertyBag = (key, bag) => false;
            configMgr.GetPropertyBagConfigLevel = (level) => propBag;

            var target   = new DiagnosticsAreaCollection(configMgr);
            var expected = new DiagnosticsArea(TestsConstants.TestGuidName);

            //Act
            target.Add(expected);

            //Assert
            Assert.AreEqual <DiagnosticsArea>(target[0], expected);
        }
        public void Add_WithDefaultArea_ThrowsInvalidOperationException()
        {
            //Arrange
            bool expectedExceptionThrown = false;
            var  target = new DiagnosticsAreaCollection();
            var  area   = new DiagnosticsArea(Constants.DefaultAreaName);

            //Act
            try
            {
                target.Add(area);
            }
            catch (InvalidOperationException)
            {
                expectedExceptionThrown = true;
            }

            Assert.IsTrue(expectedExceptionThrown, "adding default area to areas collection failed");
        }
        public void Add_AreaWithNullName_ThrowsArgumentNullException()
        {
            //Arrange
            var  target = new DiagnosticsAreaCollection();
            var  area   = new DiagnosticsArea();
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                target.Add(area);
            }
            catch (ArgumentNullException)
            {
                expectedExceptionThrown = true;
            }

            Assert.IsTrue(expectedExceptionThrown, "Adding area with a null area name did not throw exception");
        }
        public void Add_NullArea_ThrowsArgumentNullException()
        {
            //Arrange
            var  target = new DiagnosticsAreaCollection();
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                target.Add(null);
            }
            catch (ArgumentNullException)
            {
                expectedExceptionThrown = true;
            }

            //Assert
            Assert.IsTrue(expectedExceptionThrown, "add with null area did not throw exception");
        }
        public void Indexer_UpdateWithDefaultArea_ThrowsInvalidOperationException()
        {
            //Arrange
            bool expectedExceptionThrown = false;
            var  target = new DiagnosticsAreaCollection();
            var  area   = new DiagnosticsArea(Constants.DefaultAreaName);

            target.Add(new DiagnosticsArea("foobar"));

            //Act
            try
            {
                target[0] = area;
            }
            catch (InvalidOperationException)
            {
                expectedExceptionThrown = true;
            }

            Assert.IsTrue(expectedExceptionThrown, "updating to default area to areas collection failed");
        }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication app = properties.Feature.Parent as SPWebApplication;

            IConfigManager configMgr = SharePointServiceLocator.GetCurrent().GetInstance<IConfigManager>();

            DiagnosticsAreaCollection configuredAreas = new DiagnosticsAreaCollection(configMgr);

            try
            {
                foreach (DiagnosticsArea newArea in MyAreas)
                {
                    var existingArea = configuredAreas[newArea.Name];

                    if (existingArea == null)
                    {
                        configuredAreas.Add(newArea);
                    }
                    else
                    {
                        throw new SPException("Diagnostic area already exists");
                    }
                }
                configuredAreas.SaveConfiguration();
                DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();
            }
            catch (Exception) {} // TODO - uninstall diagnostic areas

            // Get the ServiceLocatorConfig service from the service locator.
            IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
            IServiceLocatorConfig serviceLocatorConfig =
                serviceLocator.GetInstance<IServiceLocatorConfig>();

            try
            {
                var testInstance = new LoupeLog();
                testInstance.LogInfo("Registered type mapping for Loupe");
                serviceLocatorConfig.RegisterTypeMapping<ILogging, LoupeLog>();
            }
            catch (FileNotFoundException)
            {
                var testInstance = new NaverticaLog();
                testInstance.LogInfo("Registered type mapping for NaverticaLog");
                serviceLocatorConfig.RegisterTypeMapping<ILogging, NaverticaLog>();
            }

            SharePointServiceLocator.Reset();
        }