Skip to content

Instantly share code, notes, and snippets.

@SlyNet
SlyNet / ObserbableCollection.cs
Created May 7, 2021 12:22
Helper method to make 2 collections be the same without replacing the whole collection
public static class ObservableCollectionExtensions
{
/// <summary>
/// Synchronizes source collection with the target to get identical lists.
/// </summary>
/// <param name="source">Collection to modify during synchronization.</param>
/// <param name="target">Target collection to receive.</param>
/// <param name="comparer">Equality comparer for the items.</param>
/// <returns>Returns items that were removed from the <paramref name="source"/> collection to properly dispose them.</returns>
public static IEnumerable<T> SynchronizeWith<T>(this ObservableCollection<T> source,
public class LoggerAdapter
{
private const string NullString = "<NULL>";
private readonly Type _type;
public LoggerAdapter(Type type)
{
_type = type;
}
@SlyNet
SlyNet / FodyWeavers.xml
Last active June 11, 2020 14:03
Fody configuration
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Tracer adapterAssembly="Tests"
logManager="Tests.LogManagerAdapter"
logger="Tests.Tracer.LoggerAdapter"
staticLogger="Tests.Tracer.Log"
traceConstructors="false" traceProperties="false">
</Tracer>
<MethodDecorator />
</Weavers>
@SlyNet
SlyNet / FileHelper.cs
Last active June 11, 2020 08:49
filehelper
var testName = CoerceValidFileName(TestContext.CurrentContext.Test.FullName);
tempLocalFolder = Path.Combine(Root, "temp", testName);
/// <remarks>
/// http://stackoverflow.com/questions/309485/c-sharp-sanitize-file-name
/// </remarks>
public static string CoerceValidFileName(string filename)
{
var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
var invalidReStr = string.Format(@"[{0}]+", invalidChars);
@SlyNet
SlyNet / WebSelementExtnsions.cs
Last active May 20, 2020 13:47
Type text in input selenium chrome
public static void SendKeysGently(this IWebElement typeTo, string text, int retryCount = 10)
{
bool introduceDelay = false;
Policy.Handle<Exception>()
.WaitAndRetry(retryCount, i => TimeSpan.FromMilliseconds(500),
(exception, timeSpan, retry, context) =>
{
introduceDelay = retry > 5;
})
.Execute(() =>
@SlyNet
SlyNet / ClearExtensions.cs
Created May 20, 2020 13:42
selenium clear input for sure
public static void ClearForChrome(this IWebElement webElement)
{
webElement.Click();
if (!string.IsNullOrEmpty(webElement.GetAttribute("value")))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
webElement.SendKeys(Keys.Control + 'a');
}
@SlyNet
SlyNet / HttpMetricsMiddleware.cs
Created March 24, 2020 09:18
metrics collecting middleware
public class HttpMetricsMiddleware
{
private readonly RequestDelegate next;
private static readonly Gauge httpInProgress = Prometheus.Metrics.CreateGauge("http_requests_in_progress", "Number or requests in progress", "system");
private static readonly Histogram httpRequestsDuration = Prometheus.Metrics.CreateHistogram("http_requests_duration_seconds", "Duration of http requests per tracking system", "system");
public HttpMetricsMiddleware(RequestDelegate next)
{
this.next = next;
}
@SlyNet
SlyNet / test.js
Created March 16, 2020 10:51
Loadtest.js
import http from 'k6/http'
import { check, group, sleep } from 'k6'
import { Trend, Rate, Counter } from 'k6/metrics'
// var host = "http://localhost/headquarters"
//var tag = "6KCM4SN5"
// var host = "https://hqrc.mysurvey.solutions"
// var host = "http://192.168.88.24:57627"
@SlyNet
SlyNet / 1 NpgsqlMetricsCollectionService.cs
Last active December 20, 2022 07:25
Npgsql connections counter publish to Prometheus
internal sealed class NpgsqlMetricsCollectionService : EventListener, IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventName != "EventCounters"
|| eventData.Payload.Count <= 0
|| !(eventData.Payload[0] is IDictionary<string, object> data)
@SlyNet
SlyNet / ProgressiveDownload.cs
Created November 9, 2018 10:24
Progressive download response message for asp.net webapi that supports video streaming and partial download and not broken
public class ProgressiveDownload
{
private readonly HttpRequestMessage request;
public ProgressiveDownload(HttpRequestMessage request)
{
this.request = request;
}
public HttpResponseMessage HeaderInfoMessage(long contentLength, string mediaType)