Skip to content

Instantly share code, notes, and snippets.

@coenm
coenm / MyException.cs
Created February 24, 2023 09:51
Exception according to SonarCloud
namespace A.B.C;
using System;
using System.Net;
using System.Runtime.Serialization;
[Serializable]
public class MyException : Exception
{
public MyException(HttpStatusCode statusCode, string? message)
@coenm
coenm / FreePortLocator.cs
Created October 9, 2022 20:21
FreePortLocator
using System.Net;
using System.Net.Sockets;
public static class FreePortLocator
{
private static readonly IPEndPoint _defaultLoopbackEndpoint = new(IPAddress.Loopback, port: 0);
public static int GetAvailablePort()
{
@coenm
coenm / TaskTimeout.cs
Last active September 30, 2022 06:53
Add a timeout to task
namespace TaskUtils;
using System;
using System.Threading;
using System.Threading.Tasks;
public static class TaskExtensions
{
/// <summary>
/// See for more information:
@coenm
coenm / Windows Defender Exclusions for Developer.ps1
Created April 15, 2021 19:50 — forked from nerzhulart/Windows Defender Exclusions for Developer.ps1
Adds Windows Defender exclusions for developers (Visual Studio, JetBrains Rider, IntellIJ Idea, Git, MsBuild, dotnet, mono etc.)
$userPath = $env:USERPROFILE
$pathExclusions = New-Object System.Collections.ArrayList
$processExclusions = New-Object System.Collections.ArrayList
$pathExclusions.Add('C:\Windows\Microsoft.NET') > $null
$pathExclusions.Add('C:\Windows\assembly') > $null
$pathExclusions.Add($userPath + '\AppData\Local\Microsoft\VisualStudio') > $null
$pathExclusions.Add('C:\ProgramData\Microsoft\VisualStudio\Packages') > $null
$pathExclusions.Add('C:\Program Files (x86)\MSBuild') > $null
$pathExclusions.Add('C:\Program Files (x86)\Microsoft Visual Studio 14.0') > $null
@coenm
coenm / TestNLogMessages.cs
Created January 9, 2015 08:42
Unittest NLog messages
using System.Linq;
using NLog;
using NLog.Targets;
using NUnit.Framework; // Using NUnit but any other unit test framework will do
namespace com.github.gist.coenm.Test
{
public class Xyz
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
@coenm
coenm / LogRequestResponseHelper.cs
Created January 9, 2015 08:03
Owin Middleware Logging
using System;
using Microsoft.Owin;
using NLog; // Using NLog in this example
namespace com.github.gist.coenm
{
public static class LogRequestResponseHelper
{
public static void LogDebugResponse(Logger logger, IOwinResponse response)
{
@coenm
coenm / NonPublicMethodsHelper.cs
Last active August 29, 2015 14:13
Helper methods to execute non public Methods or get/set non public properties.
using System;
using System.Reflection;
namespace com.github.coenm
{
public static class NonPublicMethodsHelper
{
public static MethodInfo GetNonPublicMethod(object instance, string methodName)
{
var instanceType = instance.GetType();