public void MultiplePartitionKeysTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(BlobStorageTargetTest);
            var cloudBlobService  = new CloudBlobServiceMock();
            var blobStorageTarget = new BlobStorageTarget(cloudBlobService);

            blobStorageTarget.ConnectionString = "${var:ConnectionString}";
            blobStorageTarget.Container        = "${level}";
            blobStorageTarget.BlobName         = "${logger}";
            blobStorageTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(blobStorageTarget);
            logFactory.Configuration = logConfig;
            for (int i = 0; i < 50; ++i)
            {
                logFactory.GetLogger("Test1").Info("Hello");
                logFactory.GetLogger("Test2").Debug("Goodbye");
            }
            logFactory.Flush();
            Assert.Equal(2, cloudBlobService.AppendBlob.Count);   // Two partitions
            Assert.NotEqual(string.Empty, cloudBlobService.PeekLastAppendBlob("info", "Test1"));
            Assert.NotEqual(string.Empty, cloudBlobService.PeekLastAppendBlob("debug", "Test2"));
        }
        public void SingleLogEventTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(QueueStorageTargetTest);
            var cloudQueueService  = new CloudQueueServiceMock();
            var queueStorageTarget = new QueueStorageTarget(cloudQueueService);

            queueStorageTarget.ConnectionString = "${var:ConnectionString}";
            queueStorageTarget.QueueName        = "${logger}";
            queueStorageTarget.Layout           = "${message}";
            queueStorageTarget.QueueMetadata.Add(new TargetPropertyWithContext()
            {
                Name = "MyMeta", Layout = "MyMetaValue"
            });
            logConfig.AddRuleForAllLevels(queueStorageTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("test").Info("Hello World");
            logFactory.Flush();
            Assert.Equal(nameof(QueueStorageTargetTest), cloudQueueService.ConnectionString);
            Assert.Single(cloudQueueService.QueueMetadata);
            Assert.Contains(new KeyValuePair <string, string>("MyMeta", "MyMetaValue"), cloudQueueService.QueueMetadata);
            Assert.Single(cloudQueueService.MessagesAdded);   // One queue
            Assert.Equal("Hello World", cloudQueueService.PeekLastAdded("test"));
        }
        public void SingleLogEventTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(BlobStorageTargetTest);
            var cloudBlobService  = new CloudBlobServiceMock();
            var blobStorageTarget = new BlobStorageTarget(cloudBlobService);

            blobStorageTarget.ConnectionString = "${var:ConnectionString}";
            blobStorageTarget.Container        = "${level}";
            blobStorageTarget.BlobName         = "${logger}";
            blobStorageTarget.Layout           = "${message}";
            blobStorageTarget.BlobTags.Add(new TargetPropertyWithContext()
            {
                Name = "MyTag", Layout = "MyTagValue"
            });
            blobStorageTarget.BlobMetadata.Add(new TargetPropertyWithContext()
            {
                Name = "MyMetadata", Layout = "MyMetadataValue"
            });
            logConfig.AddRuleForAllLevels(blobStorageTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello World");
            logFactory.Flush();
            Assert.Equal(nameof(BlobStorageTargetTest), cloudBlobService.ConnectionString);
            Assert.Single(cloudBlobService.BlobMetadata);
            Assert.Contains(new KeyValuePair <string, string>("MyMetadata", "MyMetadataValue"), cloudBlobService.BlobMetadata);
            Assert.Single(cloudBlobService.BlobTags);
            Assert.Contains(new KeyValuePair <string, string>("MyTag", "MyTagValue"), cloudBlobService.BlobTags);
            Assert.Single(cloudBlobService.AppendBlob);   // One partition
            Assert.Equal("Hello World" + System.Environment.NewLine, cloudBlobService.PeekLastAppendBlob("info", "Test"));
        }
예제 #4
0
        protected void ConfigureNLog(Targets.Target target = null)
        {
            target = target ?? new Targets.MemoryTarget("output")
            {
                Layout = "${message}"
            };
            var nlogConfig = new Config.LoggingConfiguration(LoggerProvider.LogFactory);

            nlogConfig.AddRuleForAllLevels(target);
            if (target is Targets.Wrappers.WrapperTargetBase wrapperTarget)
            {
                nlogConfig.AddTarget(wrapperTarget.WrappedTarget);
            }
            LoggerProvider.LogFactory.Configuration = nlogConfig;
        }
예제 #5
0
        private static (Logger, LoggerFactoryMock) CreateLoggerFactoryMock(out MicrosoftILoggerTarget target)
        {
            var logFactory        = new LogFactory();
            var logConfig         = new Config.LoggingConfiguration();
            var loggerFactoryMock = new LoggerFactoryMock();

            target = new MicrosoftILoggerTarget(loggerFactoryMock)
            {
                Layout = "${message}"
            };
            logConfig.AddRuleForAllLevels(target);
            logFactory.Configuration = logConfig;
            var logger = logFactory.GetCurrentClassLogger();

            return(logger, loggerFactoryMock);
        }
예제 #6
0
        public void Dispose_HappyPath_FlushLogFactory()
        {
            // Arrange
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.AddTarget(new Targets.MemoryTarget("output"));
            logConfig.AddRuleForAllLevels(new Targets.Wrappers.BufferingTargetWrapper("buffer", logConfig.FindTargetByName("output")));
            logFactory.Configuration = logConfig;
            var provider = new NLogLoggerProvider(null, logFactory);

            // Act
            provider.CreateLogger("test").LogInformation("Hello");
            provider.Dispose();

            // Assert
            Assert.Single(logFactory.Configuration.FindTargetByName <Targets.MemoryTarget>("output").Logs);
        }
예제 #7
0
        public void SingleLogEventTest()
        {
            NLog.LogManager.ThrowConfigExceptions = true;
            NLog.LayoutRenderers.LayoutRenderer.Register("AzureAccessToken", typeof(AccessTokenLayoutRenderer));
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);
            var memTarget  = new NLog.Targets.MemoryTarget("Test")
            {
                Layout = "${AzureAccessToken:ResourceName=https\\://database.windows.net/}"
            };

            logConfig.AddRuleForAllLevels(memTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("test").Info("Hello World");
            Assert.Single(memTarget.Logs);   // One queue
            logFactory.Configuration = null;
            Assert.True(WaitForTokenTimersStoppedAndGarbageCollected());
        }
예제 #8
0
        public void AddProvider_StateUnderTest_ExpectedBehavior()
        {
            // Arrange
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.AddRuleForAllLevels(new Targets.MemoryTarget("output"));
            logFactory.Configuration = logConfig;
            var provider      = new NLogLoggerProvider(null, logFactory);
            var loggerFactory = new NLogLoggerFactory(provider);

            // Act
            ILoggerProvider newProvider = new NLogLoggerProvider();

            loggerFactory.AddProvider(newProvider);
            loggerFactory.CreateLogger("test").LogInformation("Hello");

            // Assert
            Assert.Single(logFactory.Configuration.FindTargetByName <Targets.MemoryTarget>("output").Logs);
        }
        public void EventDataPropertiesTest()
        {
            var logFactory      = new LogFactory();
            var logConfig       = new Config.LoggingConfiguration(logFactory);
            var eventHubService = new EventHubServiceMock();
            var eventHubTarget  = new EventHubTarget(eventHubService);

            eventHubTarget.ConnectionString = "LocalEventHub";
            eventHubTarget.PartitionKey     = "${logger}";
            eventHubTarget.Layout           = "${message}";
            eventHubTarget.ContextProperties.Add(new Targets.TargetPropertyWithContext("Level", "${level}"));
            logConfig.AddRuleForAllLevels(eventHubTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello");
            logFactory.Flush();
            Assert.Single(eventHubService.EventDataSent);
            Assert.Equal("Hello", eventHubService.PeekLastSent("Test"));
            Assert.Single(eventHubService.EventDataSent.First().Value.First().Properties);
            Assert.Equal(LogLevel.Info.ToString(), eventHubService.EventDataSent.First().Value.First().Properties["Level"]);
        }
        public void DynamicTableEntityTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(TableStorageTargetTest);
            var cloudTableService  = new CloudTableServiceMock();
            var queueStorageTarget = new TableStorageTarget(cloudTableService);

            queueStorageTarget.ContextProperties.Add(new TargetPropertyWithContext("ThreadId", "${threadid}"));
            queueStorageTarget.ConnectionString = "${var:ConnectionString}";
            queueStorageTarget.TableName        = "${logger}";
            queueStorageTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(queueStorageTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello");
            logFactory.Flush();
            Assert.Single(cloudTableService.BatchExecuted);
            Assert.Equal(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), cloudTableService.PeekLastAdded("Test").Cast <DynamicTableEntity>().First()["ThreadId"].ToString());
        }
예제 #11
0
        public void SingleLogEventTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(TableStorageTargetTest);
            var cloudTableService  = new CloudTableServiceMock();
            var queueStorageTarget = new TableStorageTarget(cloudTableService);

            queueStorageTarget.ConnectionString = "${var:ConnectionString}";
            queueStorageTarget.TableName        = "${logger}";
            queueStorageTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(queueStorageTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("test").Info("Hello World");
            logFactory.Flush();
            Assert.Equal(nameof(TableStorageTargetTest), cloudTableService.ConnectionString);
            Assert.Single(cloudTableService.BatchExecuted);   // One queue
            Assert.Equal("test", cloudTableService.PeekLastAdded("test").First().PartitionKey);
        }
예제 #12
0
        public void ServiceBusUserPropertiesTest()
        {
            var logFactory       = new LogFactory();
            var logConfig        = new Config.LoggingConfiguration(logFactory);
            var serviceBusMock   = new ServiceBusMock();
            var serviceBusTarget = new ServiceBusTarget(serviceBusMock);

            serviceBusTarget.ConnectionString = "LocalEventHub";
            serviceBusTarget.QueueName        = "${shortdate}";
            serviceBusTarget.PartitionKey     = "${logger}";
            serviceBusTarget.Layout           = "${message}";
            serviceBusTarget.ContextProperties.Add(new Targets.TargetPropertyWithContext("Level", "${level}"));
            logConfig.AddRuleForAllLevels(serviceBusTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello");
            logFactory.Flush();
            Assert.Single(serviceBusMock.MessageDataSent);
            Assert.Equal("Hello", serviceBusMock.PeekLastMessageBody());
            Assert.Single(serviceBusMock.MessageDataSent.First().First().UserProperties);
            Assert.Equal(LogLevel.Info.ToString(), serviceBusMock.MessageDataSent.First().First().UserProperties["Level"]);
        }
예제 #13
0
        public void SingleLogEventTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(ServiceBusTargetTest);
            var eventHubService  = new ServiceBusMock();
            var serviceBusTarget = new ServiceBusTarget(eventHubService);

            serviceBusTarget.ConnectionString = "${var:ConnectionString}";
            serviceBusTarget.QueueName        = "${shortdate}";
            serviceBusTarget.PartitionKey     = "${logger}";
            serviceBusTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(serviceBusTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello World");
            logFactory.Flush();
            Assert.Equal(nameof(ServiceBusTargetTest), eventHubService.ConnectionString);
            Assert.Single(eventHubService.MessageDataSent);    // One partition
            Assert.Equal("Hello World", eventHubService.PeekLastMessageBody());
        }
예제 #14
0
        public void DynamicTableEntityOverrideTimestampTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(TableStorageTargetTest);
            var cloudTableService  = new CloudTableServiceMock();
            var queueStorageTarget = new TableStorageTarget(cloudTableService);

            queueStorageTarget.ContextProperties.Add(new TargetPropertyWithContext("LogTimeStamp", "${shortdate}"));
            queueStorageTarget.ConnectionString = "${var:ConnectionString}";
            queueStorageTarget.TableName        = "${logger}";
            queueStorageTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(queueStorageTarget);
            logFactory.Configuration = logConfig;
            logFactory.GetLogger("Test").Info("Hello");
            logFactory.Flush();
            Assert.Single(cloudTableService.BatchExecuted);
            var firstEntity = cloudTableService.PeekLastAdded("Test").Cast <DynamicTableEntity>().First();

            Assert.Equal(DateTime.Now.Date.ToString("yyyy-MM-dd"), firstEntity["LogTimeStamp"].StringValue);
        }
        public void MultiplePartitionKeysTest()
        {
            var logFactory      = new LogFactory();
            var logConfig       = new Config.LoggingConfiguration(logFactory);
            var eventHubService = new EventHubServiceMock();
            var eventHubTarget  = new EventHubTarget(eventHubService);

            eventHubTarget.ConnectionString = "LocalEventHub";
            eventHubTarget.PartitionKey     = "${logger}";
            eventHubTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(eventHubTarget);
            logFactory.Configuration = logConfig;
            for (int i = 0; i < 50; ++i)
            {
                logFactory.GetLogger("Test1").Info("Hello");
                logFactory.GetLogger("Test2").Debug("Goodbye");
            }
            logFactory.Flush();
            Assert.Equal(2, eventHubService.EventDataSent.Count);   // Two partitions
            Assert.Equal(50, eventHubService.EventDataSent["Test1"].Count);
            Assert.Equal(50, eventHubService.EventDataSent["Test2"].Count);
        }
        public void EventDataBulkBigBatchSize()
        {
            var logFactory      = new LogFactory();
            var logConfig       = new Config.LoggingConfiguration(logFactory);
            var eventHubService = new EventHubServiceMock();
            var eventHubTarget  = new EventHubTarget(eventHubService);

            eventHubTarget.OverflowAction         = Targets.Wrappers.AsyncTargetWrapperOverflowAction.Grow;
            eventHubTarget.ConnectionString       = "LocalEventHub";
            eventHubTarget.PartitionKey           = "${logger}";
            eventHubTarget.Layout                 = "${message}";
            eventHubTarget.TaskDelayMilliseconds  = 200;
            eventHubTarget.BatchSize              = 200;
            eventHubTarget.IncludeEventProperties = true;
            eventHubTarget.RetryCount             = 1;
            logConfig.AddRuleForAllLevels(eventHubTarget);
            logFactory.Configuration = logConfig;
            var logger = logFactory.GetLogger(nameof(EventDataBulkBigBatchSize));

            for (int i = 0; i < 11000; ++i)
            {
                if (i % 1000 == 0)
                {
                    System.Threading.Thread.Sleep(1);
                }
                logger.Info("Hello {Counter}", i);
            }
            logFactory.Flush();

            Assert.Single(eventHubService.EventDataSent);
            Assert.Equal(11000, eventHubService.EventDataSent.First().Value.Count);
            var previous = -1;

            foreach (var item in eventHubService.EventDataSent.First().Value)
            {
                Assert.True((int)item.Properties["Counter"] > previous, $"{(int)item.Properties["Counter"]} > {previous}");
                previous = (int)item.Properties["Counter"];
            }
        }
예제 #17
0
        public void MultiplePartitionKeysTest()
        {
            var logFactory       = new LogFactory();
            var logConfig        = new Config.LoggingConfiguration(logFactory);
            var serviceBusMock   = new ServiceBusMock();
            var serviceBusTarget = new ServiceBusTarget(serviceBusMock);

            serviceBusTarget.ConnectionString = "LocalEventHub";
            serviceBusTarget.QueueName        = "${shortdate}";
            serviceBusTarget.PartitionKey     = "${logger}";
            serviceBusTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(serviceBusTarget);
            logFactory.Configuration = logConfig;
            for (int i = 0; i < 50; ++i)
            {
                logFactory.GetLogger("Test1").Info("Hello");
                logFactory.GetLogger("Test2").Debug("Goodbye");
            }
            logFactory.Flush();
            Assert.Equal(2, serviceBusMock.MessageDataSent.Count);   // Two partitions
            Assert.Equal(50, serviceBusMock.MessageDataSent[0].Count);
            Assert.Equal(50, serviceBusMock.MessageDataSent[1].Count);
        }
예제 #18
0
        public void MultiplePartitionKeysTest()
        {
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.Variables["ConnectionString"] = nameof(TableStorageTargetTest);
            var cloudTableService  = new CloudTableServiceMock();
            var queueStorageTarget = new TableStorageTarget(cloudTableService);

            queueStorageTarget.ConnectionString = "${var:ConnectionString}";
            queueStorageTarget.TableName        = "${logger}";
            queueStorageTarget.Layout           = "${message}";
            logConfig.AddRuleForAllLevels(queueStorageTarget);
            logFactory.Configuration = logConfig;
            for (int i = 0; i < 50; ++i)
            {
                logFactory.GetLogger("Test1").Info("Hello");
                logFactory.GetLogger("Test2").Debug("Goodbye");
            }
            logFactory.Flush();
            Assert.Equal(2, cloudTableService.BatchExecuted.Count);   // Two partitions
            Assert.Equal(50, cloudTableService.PeekLastAdded("Test1").Count());
            Assert.Equal(50, cloudTableService.PeekLastAdded("Test2").Count());
        }