Adds a DebugChannel for TelemetryClient
This commit is contained in:
parent
cdd645bb3b
commit
b2c0919a8c
8 changed files with 203 additions and 47 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using Autofac;
|
||||
using Core.Configurations.JsonConfigProvider;
|
||||
using Core.Configurations;
|
||||
|
||||
namespace PlanTempus
|
||||
{
|
||||
public class Startup
|
||||
|
|
|
|||
|
|
@ -12,7 +12,12 @@
|
|||
<PackageReference Include="Insight.Database" Version="8.0.1" />
|
||||
<PackageReference Include="Insight.Database.Providers.PostgreSQL" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.22.0" />
|
||||
<PackageReference Include="npgsql" Version="9.0.2" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
using Autofac;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
|
||||
|
||||
namespace Core.ModuleRegistry
|
||||
{
|
||||
|
|
@ -10,16 +14,53 @@ namespace Core.ModuleRegistry
|
|||
if (TelemetryConfig == null)
|
||||
throw new Exceptions.ConfigurationException("TelemetryConfig is missing");
|
||||
|
||||
//builder.Register(c =>
|
||||
//{
|
||||
// var channel = new Telemetry.DualTelemetryChannel("C:\\logs\\telemetry.log");
|
||||
// channel.DeveloperMode = true;
|
||||
// var config = new TelemetryConfiguration
|
||||
// {
|
||||
|
||||
// ConnectionString = TelemetryConfig.ConnectionString,
|
||||
// TelemetryChannel = channel
|
||||
// };
|
||||
// return new TelemetryClient(config);
|
||||
//}).InstancePerLifetimeScope();
|
||||
|
||||
var telemetryChannel = new InMemoryChannel
|
||||
{
|
||||
DeveloperMode = true
|
||||
};
|
||||
|
||||
//var configuration = new TelemetryConfiguration
|
||||
//{
|
||||
// ConnectionString = TelemetryConfig.ConnectionString,
|
||||
// TelemetryChannel = telemetryChannel
|
||||
//};
|
||||
|
||||
//telemetryChannel.Initialize(configuration);
|
||||
|
||||
|
||||
var tmc = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault();
|
||||
tmc.ConnectionString = TelemetryConfig.ConnectionString;
|
||||
tmc.TelemetryChannel.DeveloperMode = true;
|
||||
var channel = new Telemetry.DebugTelemetryChannel("C:\\logs\\telemetry.log");
|
||||
|
||||
//var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(tmc);
|
||||
//r.Use(next => new Domain.EventTelemetryEnrichers.EnrichWithMetaTelemetry(next));
|
||||
//r.Build();
|
||||
tmc.TelemetryChannel = channel;
|
||||
|
||||
builder.RegisterInstance(tmc);
|
||||
builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>().InstancePerLifetimeScope();
|
||||
////var r = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(tmc);
|
||||
////r.Use(next => new Domain.EventTelemetryEnrichers.EnrichWithMetaTelemetry(next));
|
||||
////r.Build();
|
||||
|
||||
//builder.RegisterInstance(configuration);
|
||||
builder.Register(c => new TelemetryClient(tmc)).InstancePerLifetimeScope();
|
||||
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
|
||||
//builder.RegisterType<Microsoft.ApplicationInsights.TelemetryClient>()
|
||||
// .As<Telemetry.ITelemetryClient>()
|
||||
// .InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
21
Core/Telemetry/DebugTelemetryChannel.cs
Normal file
21
Core/Telemetry/DebugTelemetryChannel.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Microsoft.ApplicationInsights.Channel;
|
||||
|
||||
namespace Core.Telemetry
|
||||
{
|
||||
public class DebugTelemetryChannel : InMemoryChannel, ITelemetryChannel
|
||||
{
|
||||
private readonly string _filePath;
|
||||
public ITelemetryChannel _defaultChannel;
|
||||
|
||||
public DebugTelemetryChannel(string filePath)
|
||||
{
|
||||
_filePath = filePath;
|
||||
}
|
||||
public new void Send(ITelemetry item)
|
||||
{
|
||||
base.Send(item);
|
||||
var logEntry = $"{DateTime.UtcNow:u}|{item.Context.Operation.Name}|{item.Context.Operation.Id}";
|
||||
//File.AppendAllText(_filePath, logEntry + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
namespace SetupInfrastructure
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
|
||||
|
||||
namespace SetupInfrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// SETUP APPLICATION USER NAMED sathumper
|
||||
|
|
@ -14,9 +19,30 @@
|
|||
/// </summary>
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
|
||||
var telemetryChannel = new ServerTelemetryChannel();
|
||||
var configuration = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault();
|
||||
configuration.ConnectionString = "InstrumentationKey=2d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/";
|
||||
configuration.TelemetryChannel = telemetryChannel;
|
||||
|
||||
|
||||
|
||||
telemetryChannel.Initialize(configuration);
|
||||
|
||||
telemetryChannel.Send(item);
|
||||
|
||||
|
||||
var log = new TelemetryClient(configuration);
|
||||
log.TrackTrace("Console log med kanal 2");
|
||||
|
||||
log.Flush();
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
await Task.Delay(5000);
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
using Autofac;
|
||||
using System.Data;
|
||||
using Insight.Database;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Core.Telemetry;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
|
|
@ -18,7 +21,18 @@ namespace Tests
|
|||
conn.ExecuteSql("SELECT 1 as p");
|
||||
|
||||
}
|
||||
[TestMethod]
|
||||
public void MyTestMethod()
|
||||
{
|
||||
var logger = Container.Resolve<Microsoft.ApplicationInsights.TelemetryClient>();
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
logger.TrackTrace("Hello 23");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TryTenantSetupService()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ using Core.ModuleRegistry;
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Core.Configurations.JsonConfigProvider;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Logging;
|
||||
namespace Tests
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -43,14 +45,32 @@ namespace Tests
|
|||
{
|
||||
IConfigurationRoot configuration = Configuration();
|
||||
|
||||
//var logger = new LoggerConfiguration()
|
||||
// .MinimumLevel.Verbose()
|
||||
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||
// .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
|
||||
// .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW")
|
||||
// .WriteTo.ApplicationInsights(configuration.Get<string>("ApplicationInsights:ConnectionString"),
|
||||
// TelemetryConverter.Traces)
|
||||
// .Enrich.FromLogContext()
|
||||
// .Enrich.WithMachineName()
|
||||
// .CreateLogger();
|
||||
|
||||
//Log.Logger = logger;
|
||||
|
||||
//Log.Logger.Verbose("Is thos work");
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterInstance(new LoggerFactory())
|
||||
.As<ILoggerFactory>();
|
||||
|
||||
|
||||
//builder.Register(c => new SerilogLoggerFactory(logger))
|
||||
// .As<ILoggerFactory>()
|
||||
// .SingleInstance();
|
||||
|
||||
builder.RegisterGeneric(typeof(Logger<>))
|
||||
.As(typeof(ILogger<>))
|
||||
.SingleInstance();
|
||||
|
||||
|
||||
builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
|
|
@ -62,13 +82,14 @@ namespace Tests
|
|||
});
|
||||
|
||||
|
||||
|
||||
ContainerBuilder = builder;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
//Serilog.Log.CloseAndFlush();
|
||||
|
||||
Trace.Flush();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
|
|
|
|||
|
|
@ -28,5 +28,32 @@
|
|||
"Min": { "Limit": 9 }
|
||||
}
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"System": "Warning"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Seq",
|
||||
"Args": {
|
||||
"serverUrl": "http://localhost:5341",
|
||||
"apiKey": ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [
|
||||
"WithMachineName",
|
||||
"WithThreadId",
|
||||
"WithProcessId",
|
||||
"WithEnvironmentName"
|
||||
],
|
||||
"Properties": {
|
||||
"Application": "PlanTempus"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue