Skip to content

Instantly share code, notes, and snippets.

public class MetricReporter
{
private readonly ILogger<MetricReporter> _logger;
private readonly Counter _requestCounter;
private readonly Histogram _responseTimeHistogram;
public MetricReporter(ILogger<MetricReporter> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
# HELP process_private_memory_bytes Process private memory size
# TYPE process_private_memory_bytes gauge
process_private_memory_bytes 74604544
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 2223070081024
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1576244939.1073897
# HELP dotnet_total_memory_bytes Total known allocated memory
public static class Day6
{
public static void Solve()
{
var input = File.ReadAllText("Day6.txt").Split(Environment.NewLine).Select(i => i.Split(')'));
var directOrbits = new Dictionary<string, HashSet<string>> { { "COM", new HashSet<string>() } };
// Grab the orbits and arrange them outwards in - i.e. the key is the "orbiter", and the values are the masses we orbit.
// That way, when we calculate indirect orbits later on, we can simply iterate the values instead of having to do reverse lookups.
public static class Day3
{
public static void Solve()
{
var input = File.ReadAllLines("Day3.txt");
var visits = new Dictionary<(int, int), Dictionary<int, int>>();
for (var wire = 0; wire < input.Length; wire++)
{
var path = input[wire].Split(',');
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// We ultimately resolve the actual services we use from the scope we create below.
// This ensures that all services that were registered with services.AddScoped<T>()
// will be disposed at the end of the service scope (the current iteration).
using var scope = _serviceScopeFactory.CreateScope();
var sendGridClient = scope.ServiceProvider.GetRequiredService<ISendGridClient>();
"Email": {
"From": "noreply@domain.com",
"Recipient": "me@domain.com"
}
var message = new SendGridMessage
{
Subject = "Greetings",
PlainTextContent = "Hello from an ASP.NET Core worker service!",
From = new EmailAddress(configuration["Email:From"]),
};
message.AddTo(configuration["Email:Recipient"]);
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// We ultimately resolve the actual services we use from the scope we create below.
// This ensures that all services that were registered with services.AddScoped<T>()
// will be disposed at the end of the service scope (the current iteration).
using var scope = _serviceScopeFactory.CreateScope();
var sendGridClient = scope.ServiceProvider.GetRequiredService<ISendGridClient>();
private readonly ILogger<Worker> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
public Worker(ILogger<Worker> logger, IServiceScopeFactory serviceScopeFactory)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config => config.AddUserSecrets(Assembly.GetExecutingAssembly()))
.ConfigureServices((hostContext, services) =>
{
var sendGridApiKey = hostContext.Configuration.GetSection("SendGrid:ApiKey").Value;
if (string.IsNullOrWhiteSpace(sendGridApiKey))
throw new InvalidOperationException("Can not start the service without a valid SendGrid API key!");