1
0
Форкнуть 0

add prototype developed by Anastasia

This commit is contained in:
Sergey Kanzhelev 2015-03-17 20:18:12 -07:00
Родитель 0b9ca2cb6e
Коммит d5ab49e1fe
5 изменённых файлов: 236 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22609.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2E6DDE9E-8C75-4F9C-8906-08EBDD6E73EF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{62AD20FD-640F-4F99-94EF-96A7581F1CF9}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.ApplicationInsights.Middleware", "src\AI.MW.P2\Microsoft.ApplicationInsights.Middleware.kproj", "{95EC3635-22E4-4C3A-A066-F5823A0648DA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{95EC3635-22E4-4C3A-A066-F5823A0648DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95EC3635-22E4-4C3A-A066-F5823A0648DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95EC3635-22E4-4C3A-A066-F5823A0648DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95EC3635-22E4-4C3A-A066-F5823A0648DA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{95EC3635-22E4-4C3A-A066-F5823A0648DA} = {2E6DDE9E-8C75-4F9C-8906-08EBDD6E73EF}
EndGlobalSection
EndGlobal

6
aspnet/global.json Normal file
Просмотреть файл

@ -0,0 +1,6 @@
{
"sources": [ "src", "test" ],
"sdk": {
"version": "1.0.0-beta3"
}
}

Просмотреть файл

@ -0,0 +1,155 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.RequestContainer;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using System;
using System.Threading.Tasks;
namespace Microsoft.ApplicationInsights.Middleware
{
public static class ApplicationInsightsExtensions
{
public static void AddTelemetryClient(this IServiceCollection serviceCollection, IConfiguration configuration)
{
serviceCollection.AddScoped(sp => RegisterClient(sp, configuration));
}
public static void UseApplicationInsightsForRequests(this IApplicationBuilder app)
{
app.UseMiddleware<AppInsightsRequestMiddleware>();
}
public static void UseApplicationInsightsForExceptions(this IApplicationBuilder app)
{
app.UseMiddleware<AppInsightsExceptionMiddleware>();
}
private static TelemetryClient RegisterClient(IServiceProvider serviceProvider, IConfiguration configuration)
{
TelemetryClient client = null;
try
{
string key = configuration.Get("InstrumentationKey");
if (string.IsNullOrEmpty(key))
{
// TODO; check logger for null
serviceProvider.GetService<ILogger>().WriteError("InstrumentationKey not registered");
return null;
}
var aiConfig = new TelemetryConfiguration();
aiConfig.InstrumentationKey = key;
var channel = new Channel.InProcessTelemetryChannel();
aiConfig.TelemetryChannel = channel;
var env = serviceProvider.GetService<IHostingEnvironment>();
if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
aiConfig.TelemetryChannel.DeveloperMode = true;
}
client = new TelemetryClient(aiConfig);
channel.Initialize(aiConfig);
}
catch (Exception e)
{
serviceProvider.GetService<ILogger>().WriteError(e.ToString());
}
return client;
}
private class AppInsightsRequestMiddleware
{
private readonly RequestDelegate _next;
private readonly IServiceProvider _services;
public AppInsightsRequestMiddleware(RequestDelegate next, IServiceProvider services)
{
_services = services;
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
using (var container = RequestServicesContainer.EnsureRequestServices(httpContext, _services))
{
var client = httpContext.RequestServices.GetService<TelemetryClient>();
if (client == null)
{
_services.GetService<ILogger>().WriteError("AI TelemetryClient is not registered.");
}
var now = DateTime.UtcNow;
try
{
await _next.Invoke(httpContext);
}
finally
{
if (client != null)
{
var telemetry = new RequestTelemetry(
httpContext.Request.Method + " " + httpContext.Request.Path.Value,
now,
DateTime.UtcNow - now,
httpContext.Response.StatusCode.ToString(),
httpContext.Response.StatusCode < 400);
client.TrackRequest(telemetry);
}
}
}
}
}
private class AppInsightsExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly IServiceProvider _services;
public AppInsightsExceptionMiddleware(RequestDelegate next, IServiceProvider services)
{
_services = services;
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
using (var container = RequestServicesContainer.EnsureRequestServices(httpContext, _services))
{
var client = httpContext.RequestServices.GetService<TelemetryClient>();
if (client == null)
{
_services.GetService<ILogger>().WriteWarning("AI TelemetryClient is not registered.");
}
try
{
await _next.Invoke(httpContext);
}
catch (Exception exp)
{
if (client != null)
{
client.TrackException(exp);
}
throw;
}
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,21 @@
<?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>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>95ec3635-22e4-4c3a-a066-f5823a0648da</ProjectGuid>
<RootNamespace>Microsoft.ApplicationInsights.Middleware</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AssemblyName>Microsoft.ApplicationInsights.Middleware</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

Просмотреть файл

@ -0,0 +1,22 @@
{
"version": "0.30.0.1-beta",
"dependencies": {
"Microsoft.AspNet.Hosting": "1.0.0-beta3",
"Microsoft.Framework.Logging": "1.0.0-beta3",
"Microsoft.AspNet.Http.Extensions": "1.0.0-beta3",
"Microsoft.AspNet.RequestContainer": "1.0.0-beta3",
"Microsoft.ApplicationInsights.Portable": "0.30.0.0-beta"
},
"frameworks" : {
"aspnet50" : {
"dependencies": {
}
},
"aspnetcore50" : {
"dependencies": {
"System.Runtime": "4.0.20-*"
}
}
}
}