public static LogDNAOptions AddWebItems(this LogDNAOptions options)
        {
            options.MessageDetailFactory.RegisterHandler(detail =>
            {
                var contextAccessor = new HttpContextAccessor();

                detail.AddOrUpdateProperty("TraceId", contextAccessor.HttpContext?.TraceIdentifier);
                detail.AddOrUpdateProperty("IpAddress",
                                           contextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());
                detail.AddOrUpdateProperty("UserAgent",
                                           contextAccessor.HttpContext?.Request.Headers["User-Agent"].ToString());
                detail.AddOrUpdateProperty("Language",
                                           contextAccessor.HttpContext?.Request.Headers["Accept-Language"].ToString().Split(',')
                                           .FirstOrDefault());
                detail.AddOrUpdateProperty("Method", contextAccessor.HttpContext?.Request.Method);

                try
                {
                    detail.AddOrUpdateProperty("Url", contextAccessor.HttpContext?.Request.GetDisplayUrl());
                }
                catch (NullReferenceException)
                {
                    // Getting a NullReferenceException from the internals of GetDisplayUrl().
                    // This might be something to do with the response having ended but processing
                    // is still occurring. Nothing we can control or avoid.
                }

                detail.AddOrUpdateProperty("Identity", contextAccessor.HttpContext?.User?.Identity?.Name);
            });

            return(options);
        }
Beispiel #2
0
        public void AddNamespaceSuccessful()
        {
            var options = new LogDNAOptions("key")
                          .AddNamespace("Foo", LogLevel.Warning);

            Assert.Contains(options.Namespaces, x => x.Namespace == "Foo" && x.LogLevel == LogLevel.Warning);
        }
        public void LogSimpleType()
        {
            var options    = new LogDNAOptions("key", LogLevel.Critical);
            var mockClient = new Mock <IApiClient>();

            mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line =>
            {
                var detail = GetDetail(line.Content);

                Assert.Equal("name", line.Filename);
                Assert.Equal("message", detail.Message);
                Assert.Equal("CRITICAL", detail.Level);
                Assert.Null(detail.Value);
            });

            var client = mockClient.Object;

            var logger = new LogDNALogger("name", client, options);

            logger.Log(
                LogLevel.Critical,
                new EventId(),
                new FormattedLogValues("message", null),
                null,
                null
                );
        }
        public void LogWrapper()
        {
            var options    = new LogDNAOptions("key", LogLevel.Critical);
            var mockClient = new Mock <IApiClient>();

            mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line =>
            {
                var detail = GetDetail(line.Content);

                Assert.Equal("name", line.Filename);
                Assert.Equal("message", detail.Message);
                Assert.Equal("CRITICAL", detail.Level);
                Assert.NotNull(detail.Value);

                var value = ((JObject)detail.Value).ToObject <KeyValuePair <string, string> >();
                Assert.Equal("key", value.Key);
                Assert.Equal("value", value.Value);
            });

            var client = mockClient.Object;

            var logger = new LogDNALogger("name", client, options);

            logger.Log(
                LogLevel.Critical,
                new EventId(),
                new FormattedLogValues("message", new Wrapper(new KeyValuePair <string, string>("key", "value"))),
                null,
                null
                );
        }
Beispiel #5
0
        public void AddNamespaceNullNamespaceThrowsException()
        {
            var options = new LogDNAOptions("key");

            Assert.Throws <ArgumentNullException>(() =>
            {
                options.AddNamespace(null, LogLevel.Debug);
            });
        }
Beispiel #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var options = new LogDNAOptions(Configuration["IngestionKey"], LogLevel.Debug)
                          .AddWebItems();

            services.AddLogging(loggingBuilder => loggingBuilder.AddLogDNA(options));
        }
        public void CheckEnabled()
        {
            // Arrange
            var options = new LogDNAOptions("key", LogLevel.Debug)
                          .AddNamespace("Microsoft.Something", LogLevel.Warning)
                          .AddNamespace("Newtonsoft.Json", LogLevel.Critical);

            var mockClient = new Mock <IApiClient>();
            var client     = mockClient.Object;

            var logger = new LogDNALogger("Foo", client, options);

            // Should log debug
            Assert.True(logger.IsEnabled(LogLevel.Debug));
            // Should log critical
            Assert.True(logger.IsEnabled(LogLevel.Critical));
            // Shouldn't log trace
            Assert.False(logger.IsEnabled(LogLevel.Trace));
            // Shouldn't log none
            Assert.False(logger.IsEnabled(LogLevel.None));

            logger = new LogDNALogger("Microsoft.Something.Blah", client, options);
            // Shouldn't log debug
            Assert.False(logger.IsEnabled(LogLevel.Debug));
            // Should log critical
            Assert.True(logger.IsEnabled(LogLevel.Critical));
            // Shouldn't log trace
            Assert.False(logger.IsEnabled(LogLevel.Trace));
            // Shouldn't log none
            Assert.False(logger.IsEnabled(LogLevel.None));

            logger = new LogDNALogger("Newtonsoft.Json", client, options);
            // Shouldn't log debug
            Assert.False(logger.IsEnabled(LogLevel.Debug));
            // Should log critical
            Assert.True(logger.IsEnabled(LogLevel.Critical));
            // Shouldn't log trace
            Assert.False(logger.IsEnabled(LogLevel.Trace));
            // Shouldn't log none
            Assert.False(logger.IsEnabled(LogLevel.None));
        }
        public void LogException()
        {
            var options = new LogDNAOptions("key", LogLevel.Trace)
            {
                MaxInnerExceptionDepth = 3
            };

            var mockClient = new Mock <IApiClient>();

            mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line =>
            {
                Assert.Contains("Level 17", line.Content);
                Assert.DoesNotContain("Level 16", line.Content);
            });

            var client = mockClient.Object;

            var logger = new LogDNALogger("name", client, options);

            var ex = CreateTwentyLevelException();

            logger.LogError(ex, ex.Message);
        }
Beispiel #9
0
        public async void TestScopes()
        {
            var scopes     = new List <string>();
            var options    = new LogDNAOptions("key", LogLevel.Trace);
            var mockClient = new Mock <IApiClient>();

            mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line =>
            {
                var detail = GetDetail(line.Content);

                if (!string.IsNullOrEmpty(detail.Scope))
                {
                    scopes.Add(detail.Scope);
                }
            });

            var client = mockClient.Object;
            var logger = new LogDNALogger("name", client, options);

            logger.LogDebug("Starting up..");

            var tasks = new List <Task> {
                DoStuff1(logger), DoStuff2(logger)
            };
            await Task.WhenAll(tasks.ToArray());

            Assert.Equal(6, scopes.Count);
            Assert.Equal("DoStuff1", scopes[0]);
            Assert.Equal("DoStuff2", scopes[1]);
            Assert.Equal("DoStuff3", scopes[2]);
            Assert.Equal("DoStuff1", scopes[3]);
            Assert.Equal("DoStuff3", scopes[4]);
            Assert.Equal("DoStuff2", scopes[5]);

            logger.LogDebug("Finished!");
        }