1
0
Форкнуть 0

WebUserAgentTelemetryInitializer without unit tests

This commit is contained in:
Sergey Kanzhelev 2015-03-27 10:39:45 -07:00
Родитель 06918a0c14
Коммит 2503d0e245
3 изменённых файлов: 52 добавлений и 3 удалений

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

@ -5,8 +5,6 @@
using Microsoft.AspNet.Builder;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using System;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.ApplicationInsights.AspNet.DataCollection;
using Microsoft.ApplicationInsights.AspNet.Implementation;
@ -37,7 +35,8 @@
services.AddSingleton<TelemetryClient>((svcs) => {
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs));
return new TelemetryClient();
TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebUserAgentTelemetryInitializer(svcs));
return new TelemetryClient();
});
services.AddScoped<RequestTelemetry>((svcs) => {

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

@ -0,0 +1,35 @@
namespace Microsoft.ApplicationInsights.AspNet.DataCollection
{
using System;
using Microsoft.ApplicationInsights.AspNet.Implementation;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Framework.DependencyInjection;
/// <summary>
/// Telemetry initializer populates user agent (telemetry.Context.User.UserAgent) for
/// all telemetry data items.
/// </summary>
public class WebUserAgentTelemetryInitializer : ITelemetryInitializer
{
private IServiceProvider serviceProvider;
public WebUserAgentTelemetryInitializer(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public void Initialize(ITelemetry telemetry)
{
var request = this.serviceProvider.GetService<RequestTelemetry>();
if (string.IsNullOrEmpty(request.Context.User.UserAgent))
{
var context = this.serviceProvider.GetService<HttpContextHolder>().Context;
var userAgent = context.Request.Headers["User-Agent"];
request.Context.User.UserAgent = userAgent;
}
telemetry.Context.User.UserAgent = request.Context.User.UserAgent;
}
}
}

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

@ -0,0 +1,15 @@
namespace Microsoft.ApplicationInsights.AspNet.Tests
{
using System;
using Xunit;
public class WebUserAgentTelemetryInitializerTests
{
[Fact]
public void WebUserAgentTelemetryInitializerReadsProperHeader()
{
//TODO: need to implement after Anastasia will create an example on how it should be implemented
Assert.True(true);
}
}
}