1
0
Форкнуть 0

Add an AddApplicationInsightsTelemetryProcessor extension method

This commit is contained in:
Paul Harrington 2017-07-15 14:10:08 -07:00
Родитель 4e49ac1a77
Коммит 2fb7913974
3 изменённых файлов: 36 добавлений и 13 удалений

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

@ -180,6 +180,19 @@ namespace Microsoft.Extensions.DependencyInjection
return services;
}
/// <summary>
/// Adds an Application Insights Telemetry Processor into a service collection via a <see cref="ITelemetryProcessorFactory"/>.
/// </summary>
/// <typeparam name="T">Type of the telemetry processor to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> instance.</param>
/// <returns>
/// The <see cref="IServiceCollection"/>.
/// </returns>
public static IServiceCollection AddApplicationInsightsTelemetryProcessor<T>(this IServiceCollection services) where T : ITelemetryProcessor
{
return services.AddSingleton<ITelemetryProcessorFactory>(new TelemetryProcessorFactory<T>());
}
/// <summary>
/// Adds Application Insight specific configuration properties to <see cref="IConfigurationBuilder"/>.
/// </summary>

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

@ -0,0 +1,22 @@
namespace Microsoft.ApplicationInsights.AspNetCore
{
using System;
using Microsoft.ApplicationInsights.Extensibility;
/// <summary>
/// A generic factory for telemetry processors of a given type.
/// </summary>
/// <typeparam name="T">The type of telemetry processor created by this factory.</typeparam>
internal class TelemetryProcessorFactory<T> : ITelemetryProcessorFactory where T : ITelemetryProcessor
{
/// <summary>
/// Creates an instance of the telemetry processor, passing the
/// next <see cref="ITelemetryProcessor"/> in the call chain to
/// its constructor.
/// </summary>
public ITelemetryProcessor Create(ITelemetryProcessor next)
{
return (ITelemetryProcessor)Activator.CreateInstance(typeof(T), args: next);
}
}
}

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

@ -348,7 +348,7 @@ namespace Microsoft.Extensions.DependencyInjection.Test
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryProcessorFactoriesFromContainer()
{
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
services.AddSingleton<ITelemetryProcessorFactory>(new MockTelemetryProcessorFactory((next) => new FakeTelemetryProcessor(next)));
services.AddApplicationInsightsTelemetryProcessor<FakeTelemetryProcessor>();
services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());
@ -590,17 +590,5 @@ namespace Microsoft.Extensions.DependencyInjection.Test
{
}
}
private class MockTelemetryProcessorFactory: ITelemetryProcessorFactory
{
public Func<ITelemetryProcessor, ITelemetryProcessor> Factory { get; set; }
public MockTelemetryProcessorFactory(Func<ITelemetryProcessor, ITelemetryProcessor> factory)
{
this.Factory = factory;
}
public ITelemetryProcessor Create(ITelemetryProcessor next) => Factory(next);
}
}
}