The purpose of this repository is to demonstrate an example of a backend application that implements some modern traceability and observability solutions.
To add Serilog to the solution:
- Add the following packages to the project:
Serilog.AspNetCoreSerilog.Enrichers.EnvironmentSerilog.Enrichers.ProcessSerilog.Enrichers.ThreadSerilog.Settings.ConfigurationSerilog.Sinks.File
Serilog, thanks toSerilog.Settings.Configuration, can be configured viaappsettings.json, e.g:
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "logs/log.json",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
],
"Properties": {
"ApplicationName": "AppName"
}
}- If you want to activate
AspNetCorerequest tracking you just need to activate the middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
...
app.UseSerilogRequestLogging();
...
}- Enable
Serilogconfiguration:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseSerilog((hostingContext, services, configuration) => {
configuration.ReadFrom.Configuration(hostingContext.Configuration);
}, writeToProviders: true);
}- At this point you just need to consume the
ILogger<>interface of dotnet:
public class MyController(ILogger<MyController> logger) {
logger.LogError("Everything is on fire");
}To add AppMetrics to the solution:
- Add the following packages to the project:
App.Metrics.AspNetCore.AllApp.Metrics.Prometheus(or any other reporting library)
- Configure the library via
appsettings.json:
{
"MetricsOptions": {
"DefaultContextLabel": "MyMvcApplication",
"Enabled": true
},
"MetricEndpointsOptions": {
"MetricsEndpointEnabled": true,
"MetricsTextEndpointEnabled": true,
"EnvironmentInfoEndpointEnabled": true
}
}- Enable the library:
// Startup.cs
...
services.AddMvcCore(options => options.EnableEndpointRouting = false).AddMetricsCore();
...
app.UseMvc(); - Create a
MetricsRegistryto centralise the definition of the metrics to be tracked:
//MetricsRegistry.cs
using App.Metrics.ReservoirSampling.Uniform;
using App.Metrics.Timer;
namespace AppMonitoring.POC
{
public static class MetricsRegistry
{
private const string Context = "Weather";
public static readonly TimerOptions TimerWeatherForecastGeneration = new TimerOptions
{
Context = Context,
Name = "weather-forecast-generation",
Reservoir = () => new DefaultAlgorithmRReservoir(),
};
}
}- At this point all you have to do is pass an instance of
IMetricsto your class and consume the metrics you are interested in:
public class MyController
{
public void GetByCountryCity([FromService]IMetrics metrics)
{
using (metrics.Measure.Timer.Time(MetricsRegistry.TimerWeatherForecastGeneration))
{
await MyVeryLongMethod();
}
}
}