C# Asp.Net code snippets
unpublished draft
Asp.Net CoreAsp.NetEF CorePrometheusGrafana
How to create a nuget package ?#
On .csproj
file, make sure <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
presented,
then when building Release
we would have .nupkg
file. Then push it up to dotnet nuget like
push -s http"//localhost:5555/v3/index.json .\bin\Release\Some.Pkg.1.0.0.nupkg
Entity Framework Core#
EF Core CLI#
Install Entity Framework Core tools first.
Visual Studio
// Make sure starting project and targeted on Package Manage Console match
Get-Migration --Context [DbContextName - Optional]
Add-Migration [MigrationName] --Context [DbContextName - Optional]
Update-Database [MigrationName - Optional] --Context [DbContextName - Optional]
Remove-Migration --Context [DbContextName - Optional]
Script-Migration [From_MigrationName] [To_MigrationName] --Context [DbContextName - Optional]
.Net Core CLI
// cd to folder that contain csproj that have `Microsoft.EntityFrameworkCore.Design` or `Microsoft.EntityFrameworkCore.Tools` package installed
dotnet ef migrations list --context [DbContextName - Optional]
dotnet ef migrations add [MigrationName] --context [DbContextName - Optional] --output-dir [Path to desire place to store migration - Optional]
dotnet ef database update [MigrationName - Optional] --context [DbContextName - Optional]
dotnet ef database update 0 --context [DbContextName - Optional] # Revert all the migrations was ever migrate to the Db
dotnet ef migrations remove --context [DbContextName - Optional]
dotnet ef migrations script [From_MigrationName] [To_MigrationName - Optional] --context [DbContextName - Optional]
App monitoring#
Setup prometheus and grafana
# The two need to be on the same net
docker network create -d bridge monitoring-local
# Prometheus
docker run -d -–net=monitoring-local --name=prometheus --hostname=prometheus-local -p 9090:9090 -v I:/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
# Grafana
docker run -d -–net=monitoring-local --name=grafana-local -p 9091:3000 grafana/grafana
Sample prometheus.yml config file
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'barebone-api'
static_configs:
- targets: ['localhost:5000']
metrics_path: '/metrics-text'
Better example of prometheus configuration file
Some basic libs:#
- App.Metrics.AspNetCore
- App.Metrics.AspNetCore.Endpoints
- App.Metrics.AspNetCore.Tracking
- App.Metrics.Formatters.Prometheus
Startup.cs
// Some others register
services.AddMetrics();
// Some others register
Program.cs
Host.CreateDefaultBuilder(args)
.UseMetricsWebTracking()
.UseMetrics(opts =>
{
opts.EndpointOptions = endpointsOptions =>
{
endpointsOptions.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
endpointsOptions.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
endpointsOptions.EnvironmentInfoEndpointEnabled = false;
};
});
Some useful things#
Generate ECDSA key with Prime256v1 and SHA 256 compatible
# Generate private key
openssl ecparam -genkey -name prime256v1 -noout -out ec-prime256v1-priv-dev.pem
# Generate public key
openssl ec -in ec-prime256v1-priv-dev.pem -pubout -out ec-prime256v1-pub-dev.pem