Telemetry
Overview
Provide logging, tracing and metrics for ASP.NET Core.
Install
- ASOL.Core.Telemetry.Client.AspNetCore - Tracing and metrics initialization and configuration.
- ASOL.Core.Telemetry.Client.Logger - ILogger initialization and configuration.
Usage of third party libraries (NuGets)
- OpenTelemetry.Extensions.Hosting
- OpenTelemetry.Instrumentation.AspNetCore
- OpenTelemetry.Instrumentation.EntityFrameworkCore
- OpenTelemetry.Instrumentation.Http
- OpenTelemetry.Instrumentation.Runtime
- OpenTelemetry.Exporter.Console
- OpenTelemetry.Exporter.OpenTelemetryProtocol
- Serilog.AspNetCore
- Serilog.Enrichers.Environment
- Serilog.Expressions
- Serilog.Enrichers.Process
- Serilog.Enrichers.Span
- Serilog.Exceptions
- Serilog.Sinks.File
- Elastic.CommonSchema.Serilog
Changelog
- 0.0.1.52330-dev
- fixed incorrect Tenant ID logging
- 0.0.1.48598-dev
- fix application code mapping
- 0.0.1.48471-dev
- Fix filtering unnecessary tracing data while reverse proxy is used
- 0.0.1.45792-dev
- Support for Application Insights has been removed; OpenTelemetry is now used instead. For proper functioning, the minimum required versions are:
ASOL.Core.Messaging*-0.0.1.45790-devASOL.Core.Persistence*-0.0.1.45789-devASOL.Core.Processing-0.0.1.45802-dev
AddTelemetrymethod requiresIConfigurationparameter.
- Support for Application Insights has been removed; OpenTelemetry is now used instead. For proper functioning, the minimum required versions are:
Logger
using ASOL.Core.Telemetry.Client.Logger;
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.AddTelemetryLogger();
or
using ASOL.Core.Telemetry.Client.Logger;
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddTelemetryLogger();
Trace, metrics
using ASOL.Core.Telemetry.Client.AspNetCore;
public class Startup(IConfiguration configuration)
{
public virtual void ConfigureServices(IServiceCollection services)
{
// ..
services.AddTelemetry(Configuration);
// ..
}
}
Configuration
Set Telemetry:ServiceName in appsettings.json.
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"ASOL.Core": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft.AspNetCore.Hosting": "Warning",
"Microsoft.AspNetCore.Mvc.Internal": "Warning",
"Microsoft.AspNetCore.Authentication": "Warning"
}
},
"Telemetry": {
"ServiceName": "ASOL.MyOwnProject"
}
}
ProblemDetails setup
public class ProblemDetailsOptionsCustomSetup(
IWebHostEnvironment environment,
IHttpContextAccessor httpContextAccessor,
IOptions<ApiBehaviorOptions> apiOptions)
: ProblemDetailsOptionsDefaultSetup(environment, httpContextAccessor, apiOptions)
{
public override void Configure(Hellang.Middleware.ProblemDetails.ProblemDetailsOptions options)
{
...
options.Map<ValidationException>(ex =>
{
Activity.Current?.AddException(ex);
...
});
options.Map<ProcessingCoreException>(ex =>
{
Activity.Current?.AddException(ex);
...
});
}
}
Example of custom trace and metrics
public sealed class InstrumentationSource : IDisposable
{
internal const string ActivitySourceName = "MyOwnProject";
internal const string MeterName = "MyOwnProject";
private readonly Meter meter;
public InstrumentationSource()
{
ActivitySource = new ActivitySource(ActivitySourceName);
meter = new Meter(MeterName);
HighPriceCounter = meter.CreateCounter<long>("myownproject.highprice", description: "The number item with high price");
}
public ActivitySource ActivitySource { get; }
public Counter<long> HighPriceCounter { get; }
public void Dispose()
{
ActivitySource.Dispose();
meter.Dispose();
}
}
Register sources in Startup.cs:
using ASOL.Core.Telemetry.Client.AspNetCore;
public class Startup(IConfiguration configuration)
{
public virtual void ConfigureServices(IServiceCollection services)
{
// ..
services.AddSingleton<InstrumentationSource>();
services.AddTelemetry(configuration, builder => builder
.AddTracingSource(InstrumentationSource.ActivitySourceName)
.AddMeterSource(InstrumentationSource.MeterName)
);
// ..
}
}
Usage:
using var activity = instrumentationSource.ActivitySource.StartActivity("process item");
if (command.Price > 1)
{
instrumentationSource.HighPriceCounter.Add(1);
}
DevOps Monitoring
You can view your application logs in the monitoring section.
Authorization
- The user must be assigned the ASOLEU-Product-AP-.Developer role
- For a concrete application and non-production stages the user must be assigned the
{applicationCode}.Userrole or the{applicationCode}.Ownerrole - For a concrete application and production stages the user must be assigned the
{applicationCode}.Ownerrole