Add an AddApplicationInsightsTelemetryProcessor extension method
This commit is contained in:
Родитель
4e49ac1a77
Коммит
2fb7913974
|
@ -180,6 +180,19 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
return services;
|
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>
|
/// <summary>
|
||||||
/// Adds Application Insight specific configuration properties to <see cref="IConfigurationBuilder"/>.
|
/// Adds Application Insight specific configuration properties to <see cref="IConfigurationBuilder"/>.
|
||||||
/// </summary>
|
/// </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()
|
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryProcessorFactoriesFromContainer()
|
||||||
{
|
{
|
||||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||||
services.AddSingleton<ITelemetryProcessorFactory>(new MockTelemetryProcessorFactory((next) => new FakeTelemetryProcessor(next)));
|
services.AddApplicationInsightsTelemetryProcessor<FakeTelemetryProcessor>();
|
||||||
|
|
||||||
services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Загрузка…
Ссылка в новой задаче