Skip to content

Instantly share code, notes, and snippets.

/// <summary>
/// <see cref="DelegatingHandler"/> that can be used to capture the actual outbound request and the returned
/// response. These are logged by default to the console but can also be routed to an action. This can be useful
/// for tests that use a <see cref="HttpClient"/> to call a fake service.
/// </summary>
/// <example>
/// new HttpClient(new LoggingDelegatingHandler(new HttpClientHandler()));
/// </example>
public class LoggingDelegatingHandler : DelegatingHandler
{
@bronumski
bronumski / BufferedTextWriterSink.cs
Created November 3, 2021 20:06
BufferedTextWriterSink
class BufferedTextWriterSink : ILogEventSink, IDisposable
{
private readonly LogEventLevel? standardErrorFromLevel;
private readonly ITextFormatter formatter;
private readonly object _syncRoot = new();
private readonly TextWriter standardWriter;
private readonly TextWriter errorWriter;
private readonly ConcurrentBag<LogEvent> logEvents = new();
@bronumski
bronumski / InterpolatedStringHandler.cs
Last active May 4, 2022 17:25
Using `InterpolatedStringHandler` to support writing safe structured log messages
logger.LogInformation($"We can now do this {someCode}");
logger.LogInformation($"And this {1 + 1:Calculated}");
logger.LogInformation($"And also this {() => ExpensiveMethodCall(TimeSpan.FromSeconds(1)):Expensive}");
logger.LogInformation($"And not worry about {UnsafeMethodCall:Unsafe}");
logger.LogTrace($"And not have to check if the log level is enabled {() => ExpensiveMethodCall(TimeSpan.FromSeconds(10)):Expensive}");
@bronumski
bronumski / TypeExtensions.cs
Created November 3, 2023 16:13
Type extensions
static class TypeExtensions
{
public static IEnumerable<Type> GetInstanceTypesImplementing<T>(
this Assembly assembly, params Assembly[] assemblies)
{
var serviceType = typeof(T);
return assemblies.Union(new [] { assembly }).SelectMany(x => x.GetTypes())
.Where( x => x.IsClass && !x.IsAbstract && serviceType.IsAssignableFrom( x ) );
}
board = [
[None, None, None],
[None, None, None],
[None, None, None]
]
def available_goes():
return len([item for item in [x for sublist in [board[0], board[1], board[2]] for x in sublist] if item is None]) > 0
def set_position(player, x, y):