From 227f564098aedfcaa8e7eafd453b518b089e1e3d Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Fri, 2 Jan 2015 13:52:04 -0800 Subject: [PATCH] Logging functional tests using ElmLogger --- Mvc.sln | 15 ++ .../Logging/LoggingActionSelectionTest.cs | 101 +++++++++++ .../Logging/LoggingAssert.cs | 96 +++++++++++ .../Logging/LoggingExtensions.cs | 161 ++++++++++++++++++ .../Logging/LoggingStartupTest.cs | 124 ++++++++++++++ ...Microsoft.AspNet.Mvc.FunctionalTests.kproj | 9 +- .../TestHelper.cs | 2 +- .../project.json | 4 +- .../Controllers/HomeController.cs | 15 ++ .../ElmLogSerializerMiddleware.cs | 117 +++++++++++++ .../LoggingWebSite/LoggingWebSite.kproj | 25 +++ .../Models/ActivityContextDto.cs | 15 ++ .../LoggingWebSite/Models/LogInfoDto.cs | 24 +++ .../LoggingWebSite/Models/RequestInfoDto.cs | 31 ++++ .../LoggingWebSite/Models/ScopeNodeDto.cs | 18 ++ .../Properties/debugSettings.json | 3 + test/WebSites/LoggingWebSite/Startup.cs | 47 +++++ .../LoggingWebSite/Views/Home/Index.cshtml | 1 + .../Views/Shared/_Layout.cshtml | 30 ++++ test/WebSites/LoggingWebSite/project.json | 28 +++ .../WebSites/LoggingWebSite/wwwroot/readme.md | 1 + 21 files changed, 863 insertions(+), 4 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingActionSelectionTest.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingAssert.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingExtensions.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs create mode 100644 test/WebSites/LoggingWebSite/Controllers/HomeController.cs create mode 100644 test/WebSites/LoggingWebSite/ElmLogSerializerMiddleware.cs create mode 100644 test/WebSites/LoggingWebSite/LoggingWebSite.kproj create mode 100644 test/WebSites/LoggingWebSite/Models/ActivityContextDto.cs create mode 100644 test/WebSites/LoggingWebSite/Models/LogInfoDto.cs create mode 100644 test/WebSites/LoggingWebSite/Models/RequestInfoDto.cs create mode 100644 test/WebSites/LoggingWebSite/Models/ScopeNodeDto.cs create mode 100644 test/WebSites/LoggingWebSite/Properties/debugSettings.json create mode 100644 test/WebSites/LoggingWebSite/Startup.cs create mode 100644 test/WebSites/LoggingWebSite/Views/Home/Index.cshtml create mode 100644 test/WebSites/LoggingWebSite/Views/Shared/_Layout.cshtml create mode 100644 test/WebSites/LoggingWebSite/project.json create mode 100644 test/WebSites/LoggingWebSite/wwwroot/readme.md diff --git a/Mvc.sln b/Mvc.sln index a47da8f452..5c1f9b359a 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -114,6 +114,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ValueProvidersWebSite", "te EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ActionResultsWebSite", "test\WebSites\ActionResultsWebSite\ActionResultsWebSite.kproj", "{0A6BB4C0-48D3-4E7F-952B-B8917345E075}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LoggingWebSite", "test\WebSites\LoggingWebSite\LoggingWebSite.kproj", "{0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -622,6 +624,18 @@ Global {0A6BB4C0-48D3-4E7F-952B-B8917345E075}.Release|Mixed Platforms.Build.0 = Release|Any CPU {0A6BB4C0-48D3-4E7F-952B-B8917345E075}.Release|x86.ActiveCfg = Release|Any CPU {0A6BB4C0-48D3-4E7F-952B-B8917345E075}.Release|x86.Build.0 = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|x86.ActiveCfg = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Debug|x86.Build.0 = Debug|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|Any CPU.Build.0 = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|x86.ActiveCfg = Release|Any CPU + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -677,5 +691,6 @@ Global {A853B2BA-4449-4908-A416-5A3C027FC22B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {14F79E79-AE79-48FA-95DE-D794EF4EABB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {0A6BB4C0-48D3-4E7F-952B-B8917345E075} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} + {0AD78AB5-D67C-49BC-81B1-0C51BFA82B5E} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} EndGlobalSection EndGlobal diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingActionSelectionTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingActionSelectionTest.cs new file mode 100644 index 0000000000..23140ecc3d --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingActionSelectionTest.cs @@ -0,0 +1,101 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +#if ASPNET50 // Since Json.net serialization fails in CoreCLR +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using LoggingWebSite; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Mvc.Logging; +using Microsoft.AspNet.TestHost; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class LoggingActionSelectionTest + { + private readonly IServiceProvider _services = TestHelper.CreateServices(nameof(LoggingWebSite)); + private readonly Action _app = new LoggingWebSite.Startup().Configure; + + [Fact] + public async Task Successful_ActionSelection_Logged() + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + var requestTraceId = Guid.NewGuid().ToString(); + + // Act + var response = await client.GetAsync(string.Format( + "http://localhost/home/index?{0}={1}", + LoggingExtensions.RequestTraceIdQueryKey, + requestTraceId)); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var responseData = await response.Content.ReadAsStringAsync(); + Assert.Equal("Home.Index", responseData); + + var logs = await GetLogsAsync(client, requestTraceId); + var scopeNode = logs.FindScope(nameof(MvcRouteHandler) + ".RouteAsync"); + + Assert.NotNull(scopeNode); + var logInfo = scopeNode.Messages.OfDataType() + .FirstOrDefault(); + + Assert.NotNull(logInfo); + Assert.NotNull(logInfo.State); + + dynamic actionSelection = logInfo.State; + Assert.True((bool)actionSelection.ActionSelected); + Assert.True((bool)actionSelection.ActionInvoked); + Assert.True((bool)actionSelection.Handled); + } + + [Fact] + public async Task Failed_ActionSelection_Logged() + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + var requestTraceId = Guid.NewGuid().ToString(); + + // Act + var response = await client.GetAsync(string.Format( + "http://localhost/InvalidController/InvalidAction?{0}={1}", + LoggingExtensions.RequestTraceIdQueryKey, + requestTraceId)); + + // Assert + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + + var logs = await GetLogsAsync(client, requestTraceId); + var scopeNode = logs.FindScope(nameof(MvcRouteHandler) + ".RouteAsync"); + + Assert.NotNull(scopeNode); + var logInfo = scopeNode.Messages.OfDataType() + .FirstOrDefault(); + Assert.NotNull(logInfo); + + dynamic actionSelection = logInfo.State; + Assert.False((bool)actionSelection.ActionSelected); + Assert.False((bool)actionSelection.ActionInvoked); + Assert.False((bool)actionSelection.Handled); + } + + private async Task> GetLogsAsync(HttpClient client, + string requestTraceId) + { + var responseData = await client.GetStringAsync("http://localhost/logs"); + var logActivities = JsonConvert.DeserializeObject>(responseData); + return logActivities.FilterByRequestTraceId(requestTraceId); + } + + } +} +#endif \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingAssert.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingAssert.cs new file mode 100644 index 0000000000..f218e507d6 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingAssert.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using LoggingWebSite; +using Xunit.Sdk; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public static class LoggingAssert + { + /// + /// Compares two trees and verifies if the scope nodes are equal + /// + /// + /// + /// + public static bool ScopesEqual(ScopeNodeDto expected, ScopeNodeDto actual) + { + // To enable diagnosis, here a flat-list(pe-order traversal based) of + // these trees is provided. + if (!AreScopesEqual(expected, actual)) + { + var expectedScopes = new List(); + var actualScopes = new List(); + + TraverseScopeTree(expected, expectedScopes); + TraverseScopeTree(actual, actualScopes); + + throw new EqualException(expected: string.Join(", ", expectedScopes), + actual: string.Join(", ", actualScopes)); + } + + return true; + } + + /// + /// Compares two trees and verifies if the scope nodes are equal + /// + /// + /// + /// + private static bool AreScopesEqual(ScopeNodeDto root1, ScopeNodeDto root2) + { + if (root1 == null && root2 == null) + { + return true; + } + + if (root1 == null || root2 == null) + { + return false; + } + + if (!string.Equals(root1.State?.ToString(), root2.State?.ToString(), StringComparison.OrdinalIgnoreCase) + || root1.Children.Count != root2.Children.Count) + { + return false; + } + + bool isChildScopeEqual = true; + for (int i = 0; i < root1.Children.Count; i++) + { + isChildScopeEqual = AreScopesEqual(root1.Children[i], root2.Children[i]); + + if (!isChildScopeEqual) + { + break; + } + } + + return isChildScopeEqual; + } + + /// + /// Traverses the scope node sub-tree and collects the list scopes + /// + /// + /// + private static void TraverseScopeTree(ScopeNodeDto root, List scopes) + { + if (root == null) + { + return; + } + + scopes.Add(root.State?.ToString()); + + foreach (var childScope in root.Children) + { + TraverseScopeTree(childScope, scopes); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingExtensions.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingExtensions.cs new file mode 100644 index 0000000000..e647a38c60 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingExtensions.cs @@ -0,0 +1,161 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using LoggingWebSite; +using Microsoft.AspNet.WebUtilities; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public static class LoggingExtensions + { + public const string RequestTraceIdQueryKey = "RequestTraceId"; + + /// + /// Gets a scope node with the given name + /// + /// + /// + /// A scope node if found, else null + public static ScopeNodeDto FindScope(this IEnumerable activities, + string scopeName) + { + ScopeNodeDto node = null; + + foreach (var activity in activities) + { + if (activity.RepresentsScope) + { + node = GetScope(activity.Root, scopeName); + + // Ideally we do not expect multiple scopes with the same name + // to exist in the logs, so we break on the first found scope node. + // Note: The logs can contain multiple scopes with the same name across + // different requests, but the tests are expected to filter the logs by request + // (ex: using request trace id) and then find the scope by name. + if (node != null) + { + return node; + } + } + } + + return node; + } + + /// + /// Gets all the logs messages matching the given data type + /// + /// + /// + public static IEnumerable GetLogsByDataType(this IEnumerable activities) + { + var logInfos = new List(); + foreach (var activity in activities) + { + if (!activity.RepresentsScope) + { + var logInfo = activity.Root.Messages.OfDataType() + .FirstOrDefault(); + + if (logInfo != null) + { + logInfos.Add(logInfo); + } + } + else + { + GetLogsByDataType(activity.Root, logInfos); + } + } + + return logInfos; + } + + /// + /// Filters for logs activties created during application startup + /// + /// + /// + public static IEnumerable FilterByStartup(this IEnumerable activities) + { + return activities.Where(activity => activity.RequestInfo == null); + } + + /// + /// Filters log activities based on the given request. + /// + /// + /// The "RequestTraceId" query parameter value + /// + public static IEnumerable FilterByRequestTraceId(this IEnumerable activities, + string requestTraceId) + { + return activities.Where(activity => activity.RequestInfo != null + && string.Equals(GetQueryValue(activity.RequestInfo.Query, RequestTraceIdQueryKey), + requestTraceId, + StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Filters the log messages based on the given data type + /// + /// + /// + public static IEnumerable OfDataType(this IEnumerable logInfos) + { + return logInfos.Where(logInfo => logInfo.StateType != null + && logInfo.StateType.Equals(typeof(T))); + } + + /// + /// Traverses through the log node tree and gets the log messages whose StateType + /// matches the supplied data type. + /// + /// + /// + private static void GetLogsByDataType(ScopeNodeDto node, IList logInfoDtos) + { + foreach (var logInfo in node.Messages.OfDataType()) + { + logInfoDtos.Add(logInfo); + } + + foreach (var scopeNode in node.Children) + { + GetLogsByDataType(scopeNode, logInfoDtos); + } + } + + private static ScopeNodeDto GetScope(ScopeNodeDto root, string scopeName) + { + if (string.Equals(root.State?.ToString(), + scopeName, + StringComparison.OrdinalIgnoreCase)) + { + return root; + } + + foreach (var childNode in root.Children) + { + var foundNode = GetScope(childNode, scopeName); + + if (foundNode != null) + { + return foundNode; + } + } + + return null; + } + + private static string GetQueryValue(string query, string key) + { + var queryString = QueryHelpers.ParseQuery(query); + + return queryString[key]; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs new file mode 100644 index 0000000000..dbb285ee25 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Logging/LoggingStartupTest.cs @@ -0,0 +1,124 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +#if ASPNET50 // Since Json.net serialization fails in CoreCLR +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using LoggingWebSite; +using LoggingWebSite.Controllers; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Mvc.Logging; +using Microsoft.AspNet.TestHost; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class LoggingStartupTest + { + private readonly IServiceProvider _services = TestHelper.CreateServices(nameof(LoggingWebSite)); + private readonly Action _app = new LoggingWebSite.Startup().Configure; + + [Fact] + public async Task AssemblyValues_LoggedAtStartup() + { + // Arrange and Act + var logs = await GetLogsByDataTypeAsync(); + + // Assert + Assert.NotEmpty(logs); + foreach (var log in logs) + { + dynamic assembly = log.State; + Assert.NotNull(assembly); + Assert.Equal( + "LoggingWebSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + assembly.AssemblyName.ToString()); + } + } + + [Fact] + public async Task IsControllerValues_LoggedAtStartup() + { + // Arrange and Act + var logs = await GetLogsByDataTypeAsync(); + + // Assert + Assert.NotEmpty(logs); + foreach (var log in logs) + { + dynamic isController = log.State; + if (string.Equals(typeof(HomeController).AssemblyQualifiedName, isController.Type.ToString())) + { + Assert.Equal( + ControllerStatus.IsController, + Enum.Parse(typeof(ControllerStatus), isController.Status.ToString())); + } + else + { + Assert.NotEqual(ControllerStatus.IsController, + Enum.Parse(typeof(ControllerStatus), isController.Status.ToString())); + } + } + } + + [Fact] + public async Task ControllerModelValues_LoggedAtStartup() + { + // Arrange and Act + var logs = await GetLogsByDataTypeAsync(); + + // Assert + Assert.Single(logs); + dynamic controller = logs.First().State; + Assert.Equal("Home", controller.ControllerName.ToString()); + Assert.Equal(typeof(HomeController).AssemblyQualifiedName, controller.ControllerType.ToString()); + Assert.Equal("Index", controller.Actions[0].ActionName.ToString()); + Assert.Empty(controller.ApiExplorer.IsVisible); + Assert.Empty(controller.ApiExplorer.GroupName.ToString()); + Assert.Empty(controller.Attributes); + Assert.Empty(controller.Filters); + Assert.Empty(controller.ActionConstraints); + Assert.Empty(controller.RouteConstraints); + Assert.Empty(controller.AttributeRoutes); + } + + [Fact] + public async Task ActionDescriptorValues_LoggedAtStartup() + { + // Arrange and Act + var logs = await GetLogsByDataTypeAsync(); + + // Assert + Assert.Single(logs); + dynamic action = logs.First().State; + Assert.Equal("Index", action.Name.ToString()); + Assert.Empty(action.Parameters); + Assert.Empty(action.FilterDescriptors); + Assert.Equal("action", action.RouteConstraints[0].RouteKey.ToString()); + Assert.Equal("controller", action.RouteConstraints[1].RouteKey.ToString()); + Assert.Empty(action.RouteValueDefaults); + Assert.Empty(action.ActionConstraints.ToString()); + Assert.Empty(action.HttpMethods.ToString()); + Assert.Empty(action.Properties); + Assert.Equal("Home", action.ControllerName.ToString()); + } + + private async Task> GetLogsByDataTypeAsync() + { + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + + var response = await client.GetStringAsync("http://localhost/logs"); + + var activityDtos = JsonConvert.DeserializeObject>(response); + + var logs = activityDtos.FilterByStartup().GetLogsByDataType(); + + return logs; + } + } +} +#endif \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj index 93c5f46e8a..247e087053 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj @@ -1,4 +1,4 @@ - + 14.0 @@ -14,4 +14,9 @@ 2.0 - + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs index d0e72ba54e..3086318a46 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs @@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests services.AddInstance( typeof(ILoggerFactory), - NullLoggerFactory.Instance); + new LoggerFactory()); if (newServices != null) { diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index 13f7590d1c..6b889106c6 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -16,6 +16,7 @@ "FiltersWebSite": "1.0.0", "FormatterWebSite": "1.0.0", "InlineConstraintsWebSite": "1.0.0", + "LoggingWebSite": "1.0.0", "ModelBindingWebSite": "1.0.0", "MvcSample.Web": "1.0.0", "PrecompilationWebSite": "1.0.0", @@ -35,7 +36,8 @@ "Microsoft.AspNet.TestHost": "1.0.0-*", "Microsoft.AspNet.Mvc.TestConfiguration": "1.0.0", "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*", - "xunit.runner.kre": "1.0.0-*" + "xunit.runner.kre": "1.0.0-*", + "Microsoft.AspNet.WebUtilities": "1.0.0-*" }, "commands": { "test": "xunit.runner.kre" diff --git a/test/WebSites/LoggingWebSite/Controllers/HomeController.cs b/test/WebSites/LoggingWebSite/Controllers/HomeController.cs new file mode 100644 index 0000000000..d27ef6ec79 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Controllers/HomeController.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Mvc; + +namespace LoggingWebSite.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/ElmLogSerializerMiddleware.cs b/test/WebSites/LoggingWebSite/ElmLogSerializerMiddleware.cs new file mode 100644 index 0000000000..8225fe8daa --- /dev/null +++ b/test/WebSites/LoggingWebSite/ElmLogSerializerMiddleware.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Diagnostics.Elm; +using Microsoft.AspNet.Http; +using Newtonsoft.Json; + +namespace LoggingWebSite +{ + public class ElmLogSerializerMiddleware + { + private readonly RequestDelegate _next; + + public ElmLogSerializerMiddleware(RequestDelegate next) + { + _next = next; + } + + public Task Invoke(HttpContext context, ElmStore elmStore) + { + var currentRequest = context.Request; + + var logActivities = GetLogDetails(elmStore); + + context.Response.StatusCode = 200; + context.Response.ContentType = "application/json"; + + var serializer = JsonSerializer.Create(); + using (var writer = new JsonTextWriter(new StreamWriter(stream: context.Response.Body, + encoding: Encoding.UTF8, + bufferSize: 1024, + leaveOpen: true))) + { + serializer.Serialize(writer, logActivities); + } + + return Task.FromResult(true); + } + + private IEnumerable GetLogDetails(ElmStore elmStore) + { + var activities = new List(); + foreach (var activity in elmStore.GetActivities().Reverse()) + { + var rootScopeNodeDto = new ScopeNodeDto(); + CopyScopeNodeTree(activity.Root, rootScopeNodeDto); + + activities.Add(new ActivityContextDto() + { + RequestInfo = GetRequestInfoDto(activity.HttpInfo), + Id = activity.Id, + RepresentsScope = activity.RepresentsScope, + Root = rootScopeNodeDto + }); + } + + return activities; + } + + private RequestInfoDto GetRequestInfoDto(HttpInfo httpInfo) + { + if (httpInfo == null) return null; + + return new RequestInfoDto() + { + ContentType = httpInfo.ContentType, + Cookies = httpInfo.Cookies.ToArray(), + Headers = httpInfo.Headers.ToArray(), + Query = httpInfo.Query.Value, + Host = httpInfo.Host.Value, + Method = httpInfo.Method, + Path = httpInfo.Path.Value, + Protocol = httpInfo.Protocol, + RequestID = httpInfo.RequestID, + Scheme = httpInfo.Scheme, + StatusCode = httpInfo.StatusCode + }; + } + + private LogInfoDto GetLogInfoDto(LogInfo logInfo) + { + return new LogInfoDto() + { + EventID = logInfo.EventID, + Exception = logInfo.Exception, + LoggerName = logInfo.Name, + LogLevel = logInfo.Severity, + State = logInfo.State, + StateType = logInfo.State?.GetType() + }; + } + + private void CopyScopeNodeTree(ScopeNode root, ScopeNodeDto rootDto) + { + rootDto.LoggerName = root.Name; + rootDto.State = root.State; + rootDto.StateType = root.State?.GetType(); + + foreach (var logInfo in root.Messages) + { + rootDto.Messages.Add(GetLogInfoDto(logInfo)); + } + + foreach (var scopeNode in root.Children) + { + ScopeNodeDto childDto = new ScopeNodeDto(); + + CopyScopeNodeTree(scopeNode, childDto); + + rootDto.Children.Add(childDto); + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/LoggingWebSite.kproj b/test/WebSites/LoggingWebSite/LoggingWebSite.kproj new file mode 100644 index 0000000000..a513d0edcc --- /dev/null +++ b/test/WebSites/LoggingWebSite/LoggingWebSite.kproj @@ -0,0 +1,25 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 0ad78ab5-d67c-49bc-81b1-0c51bfa82b5e + + + + + + + 2.0 + 40912 + + + + + + + + \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Models/ActivityContextDto.cs b/test/WebSites/LoggingWebSite/Models/ActivityContextDto.cs new file mode 100644 index 0000000000..9f8483bf07 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Models/ActivityContextDto.cs @@ -0,0 +1,15 @@ +using System; + +namespace LoggingWebSite +{ + public class ActivityContextDto + { + public Guid Id { get; set; } + + public RequestInfoDto RequestInfo { get; set; } + + public ScopeNodeDto Root { get; set; } + + public bool RepresentsScope { get; set; } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Models/LogInfoDto.cs b/test/WebSites/LoggingWebSite/Models/LogInfoDto.cs new file mode 100644 index 0000000000..b70fe411bd --- /dev/null +++ b/test/WebSites/LoggingWebSite/Models/LogInfoDto.cs @@ -0,0 +1,24 @@ +using System; +using Microsoft.Framework.Logging; + +namespace LoggingWebSite +{ + public class LogInfoDto + { + public string LoggerName { get; set; } + + /// + /// Type of object representing the State. This is useful for tests + /// to filter the results + /// + public Type StateType { get; set; } + + public LogLevel LogLevel { get; set; } + + public int EventID { get; set; } + + public object State { get; set; } + + public Exception Exception { get; set; } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Models/RequestInfoDto.cs b/test/WebSites/LoggingWebSite/Models/RequestInfoDto.cs new file mode 100644 index 0000000000..a347b23d9a --- /dev/null +++ b/test/WebSites/LoggingWebSite/Models/RequestInfoDto.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Http; + +namespace LoggingWebSite +{ + public class RequestInfoDto + { + public Guid RequestID { get; set; } + + public string Host { get; set; } + + public string Path { get; set; } + + public string ContentType { get; set; } + + public string Scheme { get; set; } + + public int StatusCode { get; set; } + + public string Method { get; set; } + + public string Protocol { get; set; } + + public IEnumerable> Headers { get; set; } + + public string Query { get; set; } + + public IEnumerable> Cookies { get; set; } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Models/ScopeNodeDto.cs b/test/WebSites/LoggingWebSite/Models/ScopeNodeDto.cs new file mode 100644 index 0000000000..5acf5153be --- /dev/null +++ b/test/WebSites/LoggingWebSite/Models/ScopeNodeDto.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; + +namespace LoggingWebSite +{ + public class ScopeNodeDto + { + public List Children { get; private set; } = new List(); + + public List Messages { get; private set; } = new List(); + + public object State { get; set; } + + public Type StateType { get; set; } + + public string LoggerName { get; set; } + } +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Properties/debugSettings.json b/test/WebSites/LoggingWebSite/Properties/debugSettings.json new file mode 100644 index 0000000000..a44fad34a3 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Properties/debugSettings.json @@ -0,0 +1,3 @@ +{ + "Profiles": [] +} \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Startup.cs b/test/WebSites/LoggingWebSite/Startup.cs new file mode 100644 index 0000000000..dbb5560c59 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Startup.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Routing; +using Microsoft.Framework.DependencyInjection; + +namespace LoggingWebSite +{ + public class Startup + { + public void Configure(IApplicationBuilder app) + { + var configuration = app.GetTestConfiguration(); + + app.UseServices(services => + { + services.AddElm(options => + { + // We want to log for all log levels and loggers + options.Filter = (loggerName, logLevel) => true; + }); + + services.AddMvc(configuration); + }); + + app.Map("/logs", (appBuilder) => + { + appBuilder.UseMiddleware(); + }); + + app.UseElmCapture(); + + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller}/{action}/{id?}", + defaults: new { controller = "Home", action = "Index" }); + + routes.MapRoute( + name: "api", + template: "{controller}/{id?}"); + }); + } + } +} diff --git a/test/WebSites/LoggingWebSite/Views/Home/Index.cshtml b/test/WebSites/LoggingWebSite/Views/Home/Index.cshtml new file mode 100644 index 0000000000..98b6900f87 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Views/Home/Index.cshtml @@ -0,0 +1 @@ +Home.Index \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/Views/Shared/_Layout.cshtml b/test/WebSites/LoggingWebSite/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000000..4311c0e375 --- /dev/null +++ b/test/WebSites/LoggingWebSite/Views/Shared/_Layout.cshtml @@ -0,0 +1,30 @@ + + + + + + @ViewBag.Title - My ASP.NET Application + + + +
+
+
+ @Html.ActionLink("LoggingWebApplication", "Index", "Home", new { area = "" }) +
+
+
    +
  • @Html.ActionLink("Home", "Index", "Home")
  • +
+
+
+
+
+ @RenderBody() +
+ +
+ + \ No newline at end of file diff --git a/test/WebSites/LoggingWebSite/project.json b/test/WebSites/LoggingWebSite/project.json new file mode 100644 index 0000000000..5e14c7f6e7 --- /dev/null +++ b/test/WebSites/LoggingWebSite/project.json @@ -0,0 +1,28 @@ +{ + "webroot": "wwwroot", + "version": "1.0.0-*", + "exclude": [ + "wwwroot" + ], + "packExclude": [ + "**.kproj", + "**.user", + "**.vspscc" + ], + "commands": { + "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001", + "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5000" + }, + "dependencies": { + "Kestrel": "1.0.0-*", + "Microsoft.AspNet.Mvc": "6.0.0-*", + "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Server.WebListener": "1.0.0-*", + "Microsoft.AspNet.Mvc.TestConfiguration": "1.0.0", + "Microsoft.AspNet.Diagnostics.Elm": "1.0.0-*" + }, + "frameworks": { + "aspnet50": { }, + "aspnetcore50": { } + } +} diff --git a/test/WebSites/LoggingWebSite/wwwroot/readme.md b/test/WebSites/LoggingWebSite/wwwroot/readme.md new file mode 100644 index 0000000000..a60637ec43 --- /dev/null +++ b/test/WebSites/LoggingWebSite/wwwroot/readme.md @@ -0,0 +1 @@ +This file exists as Git does not allow to add empty directories into the repo \ No newline at end of file