Skip to content

Instantly share code, notes, and snippets.

@bitbonk
bitbonk / Program.cs
Created October 26, 2023 22:28
asynclocal culture
// See https://aka.ms/new-console-template for more information
using System.Globalization;
using System.Runtime.CompilerServices;
ShowCulture("start");
await SetFrenchAsync();
ShowCulture("end");
async Task SetFrenchAsync()
@bitbonk
bitbonk / Function1.cs
Created November 17, 2022 16:23
Using Masto.NET in Azure Functions
public class RepostOnMastodon
{
private readonly AuthenticationClient client;
public RepostOnMastodon()
{
this.client = new AuthenticationClient("something.social");
}
[FunctionName("RepostOnMastodon")]
@bitbonk
bitbonk / AzureDevOpsServerAttribute.cs
Last active March 9, 2022 23:19
Nuke pipeline Yaml generation for Azure DevOps Server (on-premise version of Azure DevOps)
// <copyright>
// Copyright (c) Kontron AIS GmbH. All rights reserved.
// </copyright>
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Nuke.Common;
using Nuke.Common.CI;
using Nuke.Common.CI.AzurePipelines;
@bitbonk
bitbonk / Program.cs
Created April 27, 2020 15:28
Task.WhenAll + AggregateExeption
class Program
{
static async Task Main(string[] args)
{
try
{
await Task.WhenAll(
Task.Run(() =>
{
Console.WriteLine("begin one");
@bitbonk
bitbonk / recordsanddiscriminatedunions.cs
Last active January 24, 2020 12:59
Record and discriminated unions in C# 9
public class Person
{
public initonly string Firstname { get; }
public initonly string Lastname { get; }
};
enum class ByteOrBool { byte Y; bool B;}
enum class MixedType
{
@bitbonk
bitbonk / config.md
Last active November 11, 2019 03:43
Example Serilog Config with C# and JSON

C# config:

var logConfig = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .WriteTo.ColoredConsole(LogEventLevel.Error, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
    .WriteTo.Logger(
        c => c.WriteTo.ColoredConsole(LogEventLevel.Information, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
            .Filter.ByIncludingOnly("SourceContext = 'Microsoft.Hosting.Lifetime'"))
public class MyCaliburnBootstrapper : BootstrapperBase
{
private IHost? host;
protected MyCaliburnBootstrapper()
{
this.Initialize();
}
private IHost BuildHost()
@bitbonk
bitbonk / 1. MainWindow.cs
Last active June 28, 2019 13:59
async v3
private Task completion = Task.CompletedTask;
private int counter;
private async void ButtonOnClick(object sender, RoutedEventArgs e)
{
this.completion = this.completion.ContinueWith(async _ =>
{
this.counter = this.counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
private Task completion = Task.CompletedTask;
private int counter;
private void ButtonOnClick(object sender, RoutedEventArgs e)
{
this.completion = this.completion.ContinueWith(async _ =>
{
this.counter = this.counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
@bitbonk
bitbonk / 1. MainWindow.cs
Last active June 28, 2019 13:44
async v1
private int counter = 0;
private async void ButtonOnClick(object sender, RoutedEventArgs e)
{
counter = counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
Debug.WriteLine($"Exit ButtonOnClick {counter}");
}
public async Task DoSubAsync(int number)
{