50 lines
2 KiB
C#
50 lines
2 KiB
C#
|
|
using Autofac;
|
|||
|
|
using Microsoft.ApplicationInsights.Channel;
|
|||
|
|
using Microsoft.ApplicationInsights.Extensibility;
|
|||
|
|
using SWP.Core.Telemetry;
|
|||
|
|
|
|||
|
|
namespace SWP.Core.ModuleRegistry
|
|||
|
|
{
|
|||
|
|
public class TelemetryModule : Module
|
|||
|
|
{
|
|||
|
|
public required TelemetryConfig TelemetryConfig { get; set; }
|
|||
|
|
|
|||
|
|
protected override void Load(ContainerBuilder builder)
|
|||
|
|
{
|
|||
|
|
var configuration = TelemetryConfiguration.CreateDefault();
|
|||
|
|
configuration.ConnectionString = TelemetryConfig.ConnectionString;
|
|||
|
|
configuration.TelemetryChannel.DeveloperMode = true;
|
|||
|
|
|
|||
|
|
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
|
|||
|
|
client.Context.GlobalProperties["Application"] = GetType().Namespace?.Split('.')[0];
|
|||
|
|
client.Context.GlobalProperties["MachineName"] = Environment.MachineName;
|
|||
|
|
client.Context.GlobalProperties["CLRVersion"] = Environment.Version.ToString();
|
|||
|
|
client.Context.GlobalProperties["ProcessorCount"] = Environment.ProcessorCount.ToString();
|
|||
|
|
|
|||
|
|
builder.Register(c => client).InstancePerLifetimeScope();
|
|||
|
|
|
|||
|
|
if (TelemetryConfig.UseSeqLoggingTelemetryChannel)
|
|||
|
|
{
|
|||
|
|
var messageChannel = new MessageChannel();
|
|||
|
|
|
|||
|
|
builder.RegisterInstance(messageChannel)
|
|||
|
|
.As<IMessageChannel<ITelemetry>>()
|
|||
|
|
.SingleInstance();
|
|||
|
|
|
|||
|
|
configuration.TelemetryChannel = new SeqTelemetryChannel(messageChannel, client);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var telemetryProcessorChain =
|
|||
|
|
new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(
|
|||
|
|
configuration);
|
|||
|
|
telemetryProcessorChain.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
|
|||
|
|
telemetryProcessorChain.Build();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class TelemetryConfig
|
|||
|
|
{
|
|||
|
|
public string ConnectionString { get; set; }
|
|||
|
|
public bool UseSeqLoggingTelemetryChannel { get; set; }
|
|||
|
|
}
|
|||
|
|
}
|