Logging functional tests using ElmLogger
This commit is contained in:
parent
0a473b0600
commit
227f564098
15
Mvc.sln
15
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
|
||||
|
|
|
|||
|
|
@ -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<IApplicationBuilder> _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<MvcRouteHandlerRouteAsyncValues>()
|
||||
.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<MvcRouteHandlerRouteAsyncValues>()
|
||||
.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<IEnumerable<ActivityContextDto>> GetLogsAsync(HttpClient client,
|
||||
string requestTraceId)
|
||||
{
|
||||
var responseData = await client.GetStringAsync("http://localhost/logs");
|
||||
var logActivities = JsonConvert.DeserializeObject<List<ActivityContextDto>>(responseData);
|
||||
return logActivities.FilterByRequestTraceId(requestTraceId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares two trees and verifies if the scope nodes are equal
|
||||
/// </summary>
|
||||
/// <param name="expected"></param>
|
||||
/// <param name="actual"></param>
|
||||
/// <returns></returns>
|
||||
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<string>();
|
||||
var actualScopes = new List<string>();
|
||||
|
||||
TraverseScopeTree(expected, expectedScopes);
|
||||
TraverseScopeTree(actual, actualScopes);
|
||||
|
||||
throw new EqualException(expected: string.Join(", ", expectedScopes),
|
||||
actual: string.Join(", ", actualScopes));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two trees and verifies if the scope nodes are equal
|
||||
/// </summary>
|
||||
/// <param name="root1"></param>
|
||||
/// <param name="root2"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traverses the scope node sub-tree and collects the list scopes
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/// <param name="scopes"></param>
|
||||
private static void TraverseScopeTree(ScopeNodeDto root, List<string> scopes)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
scopes.Add(root.State?.ToString());
|
||||
|
||||
foreach (var childScope in root.Children)
|
||||
{
|
||||
TraverseScopeTree(childScope, scopes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
|
||||
/// <summary>
|
||||
/// Gets a scope node with the given name
|
||||
/// </summary>
|
||||
/// <param name="activities"></param>
|
||||
/// <param name="scopeName"></param>
|
||||
/// <returns>A scope node if found, else null</returns>
|
||||
public static ScopeNodeDto FindScope(this IEnumerable<ActivityContextDto> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all the logs messages matching the given data type
|
||||
/// </summary>
|
||||
/// <param name="activities"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<LogInfoDto> GetLogsByDataType<T>(this IEnumerable<ActivityContextDto> activities)
|
||||
{
|
||||
var logInfos = new List<LogInfoDto>();
|
||||
foreach (var activity in activities)
|
||||
{
|
||||
if (!activity.RepresentsScope)
|
||||
{
|
||||
var logInfo = activity.Root.Messages.OfDataType<T>()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (logInfo != null)
|
||||
{
|
||||
logInfos.Add(logInfo);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetLogsByDataType<T>(activity.Root, logInfos);
|
||||
}
|
||||
}
|
||||
|
||||
return logInfos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters for logs activties created during application startup
|
||||
/// </summary>
|
||||
/// <param name="activities"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<ActivityContextDto> FilterByStartup(this IEnumerable<ActivityContextDto> activities)
|
||||
{
|
||||
return activities.Where(activity => activity.RequestInfo == null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters log activities based on the given request.
|
||||
/// </summary>
|
||||
/// <param name="activities"></param>
|
||||
/// <param name="requestTraceId">The "RequestTraceId" query parameter value</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<ActivityContextDto> FilterByRequestTraceId(this IEnumerable<ActivityContextDto> activities,
|
||||
string requestTraceId)
|
||||
{
|
||||
return activities.Where(activity => activity.RequestInfo != null
|
||||
&& string.Equals(GetQueryValue(activity.RequestInfo.Query, RequestTraceIdQueryKey),
|
||||
requestTraceId,
|
||||
StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters the log messages based on the given data type
|
||||
/// </summary>
|
||||
/// <param name="logInfos"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<LogInfoDto> OfDataType<T>(this IEnumerable<LogInfoDto> logInfos)
|
||||
{
|
||||
return logInfos.Where(logInfo => logInfo.StateType != null
|
||||
&& logInfo.StateType.Equals(typeof(T)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traverses through the log node tree and gets the log messages whose StateType
|
||||
/// matches the supplied data type.
|
||||
/// </summary>
|
||||
/// <param name="node"></param>
|
||||
/// <param name="logInfoDtos"></param>
|
||||
private static void GetLogsByDataType<T>(ScopeNodeDto node, IList<LogInfoDto> logInfoDtos)
|
||||
{
|
||||
foreach (var logInfo in node.Messages.OfDataType<T>())
|
||||
{
|
||||
logInfoDtos.Add(logInfo);
|
||||
}
|
||||
|
||||
foreach (var scopeNode in node.Children)
|
||||
{
|
||||
GetLogsByDataType<T>(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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IApplicationBuilder> _app = new LoggingWebSite.Startup().Configure;
|
||||
|
||||
[Fact]
|
||||
public async Task AssemblyValues_LoggedAtStartup()
|
||||
{
|
||||
// Arrange and Act
|
||||
var logs = await GetLogsByDataTypeAsync<AssemblyValues>();
|
||||
|
||||
// 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<IsControllerValues>();
|
||||
|
||||
// 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<ControllerModelValues>();
|
||||
|
||||
// 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<ActionDescriptorValues>();
|
||||
|
||||
// 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<IEnumerable<LogInfoDto>> GetLogsByDataTypeAsync<T>()
|
||||
{
|
||||
var server = TestServer.Create(_services, _app);
|
||||
var client = server.CreateClient();
|
||||
|
||||
var response = await client.GetStringAsync("http://localhost/logs");
|
||||
|
||||
var activityDtos = JsonConvert.DeserializeObject<List<ActivityContextDto>>(response);
|
||||
|
||||
var logs = activityDtos.FilterByStartup().GetLogsByDataType<T>();
|
||||
|
||||
return logs;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
|
|
@ -14,4 +14,9 @@
|
|||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
||||
|
|
@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
|
||||
services.AddInstance(
|
||||
typeof(ILoggerFactory),
|
||||
NullLoggerFactory.Instance);
|
||||
new LoggerFactory());
|
||||
|
||||
if (newServices != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ActivityContextDto> GetLogDetails(ElmStore elmStore)
|
||||
{
|
||||
var activities = new List<ActivityContextDto>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>0ad78ab5-d67c-49bc-81b1-0c51bfa82b5e</ProjectGuid>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<DevelopmentServerPort>40912</DevelopmentServerPort>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
||||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using Microsoft.Framework.Logging;
|
||||
|
||||
namespace LoggingWebSite
|
||||
{
|
||||
public class LogInfoDto
|
||||
{
|
||||
public string LoggerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Type of object representing the State. This is useful for tests
|
||||
/// to filter the results
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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<KeyValuePair<string, string[]>> Headers { get; set; }
|
||||
|
||||
public string Query { get; set; }
|
||||
|
||||
public IEnumerable<KeyValuePair<string, string[]>> Cookies { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LoggingWebSite
|
||||
{
|
||||
public class ScopeNodeDto
|
||||
{
|
||||
public List<ScopeNodeDto> Children { get; private set; } = new List<ScopeNodeDto>();
|
||||
|
||||
public List<LogInfoDto> Messages { get; private set; } = new List<LogInfoDto>();
|
||||
|
||||
public object State { get; set; }
|
||||
|
||||
public Type StateType { get; set; }
|
||||
|
||||
public string LoggerName { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"Profiles": []
|
||||
}
|
||||
|
|
@ -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<ElmLogSerializerMiddleware>();
|
||||
});
|
||||
|
||||
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?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Home.Index
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>@ViewBag.Title - My ASP.NET Application</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
@Html.ActionLink("LoggingWebApplication", "Index", "Home", new { area = "" })
|
||||
</div>
|
||||
<div>
|
||||
<ul>
|
||||
<li>@Html.ActionLink("Home", "Index", "Home")</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
@RenderBody()
|
||||
<hr />
|
||||
<footer>
|
||||
<p>© 2014 - My ASP.NET Application</p>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -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": { }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
This file exists as Git does not allow to add empty directories into the repo
|
||||
Loading…
Reference in New Issue