Skip to content

Instantly share code, notes, and snippets.

@kadukf
kadukf / Base64Encoding.UsingSpanBase64BinaryGuid.cs
Created April 23, 2020 20:52
Base64Encoding.UsingSpanBase64BinaryGuid
public static string UsingSpanBase64BinaryGuid(Guid oid, params string[] input)
{
var totalUtf8Bytes = _guidSize;
for (int i = 0; i < input.Length; i++)
{
totalUtf8Bytes += Encoding.UTF8.GetByteCount(input[i]);
}
Span<byte> resultSpan = stackalloc byte[Base64.GetMaxEncodedToUtf8Length(totalUtf8Bytes)];
if (!MemoryMarshal.TryWrite(resultSpan, ref oid))
@kadukf
kadukf / Base64Encoding.UsingSpanBase64.cs
Created April 23, 2020 20:29
Base64Encoding.UsingSpanBase64
public static string UsingSpanBase64(Guid oid, params string[] input)
{
var totalUtf8Bytes = 36; // GUID.Format(D) = 36 bytes
for (int i = 0; i < input.Length; i++)
{
totalUtf8Bytes += Encoding.UTF8.GetByteCount(input[i]);
}
Span<byte> resultSpan = stackalloc byte[Base64.GetMaxEncodedToUtf8Length(totalUtf8Bytes)];
if (!Utf8Formatter.TryFormat(oid, resultSpan, out int writtenBytes) || writtenBytes != 36)
public static string UsingWebEncoders(string input)
{
byte[] inputBytes = null;
char[] outputChars = null;
try
{
var minimumLength = Encoding.UTF8.GetByteCount(input);
inputBytes = ArrayPool<byte>.Shared.Rent(minimumLength);
int inputBytesCount = Encoding.UTF8.GetBytes(input, 0, input.Length, inputBytes, 0);
@kadukf
kadukf / Base64Encoding.Setup.cs
Created April 23, 2020 07:52
Base64Encoding.Setup
[GlobalSetup]
public void GlobalSetup()
{
var rnd = new Random();
_guid = Guid.NewGuid();
var buffer = new byte[DataLen];
rnd.NextBytes(buffer);
@kadukf
kadukf / Program.cs
Created January 8, 2020 09:44
Setup of scope filtering
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost
.CreateDefaultBuilder(args)
@kadukf
kadukf / ValuesController.cs
Created January 8, 2020 09:43
Sample controller with logging and using scopes
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
@kadukf
kadukf / appsettings.json
Last active January 8, 2020 09:34
Logging scope levels settings
{
"Logging": {
"LogLevel": {
"Default": "Verbose"
},
"ScopeLevel": {
"ScopeFilteringWebApplication": "PlainText",
"Default": "None"
}
},
@kadukf
kadukf / appsettings.json
Last active January 8, 2020 09:22
App settings to log all messages
{
"Logging": {
"LogLevel": {
"Default": "Verbose"
}
},
"AllowedHosts": "*"
}
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Starting, press a key to continue ...");
Console.ReadKey();
var ips = await Dns.GetHostAddressesAsync("www.google.com");
foreach (var ipAddress in ips)
Microsoft.WindowsAzure.ServiceRuntime Information: 200 : Role entrypoint . CALLING OnStart(): Microsoft.WindowsAzure.ServiceRuntime.ProgramEntryPoint
Microsoft.WindowsAzure.ServiceRuntime Information: 202 : Role entrypoint . COMPLETED OnStart(): Microsoft.WindowsAzure.ServiceRuntime.ProgramEntryPoint
The thread 0x7bbc has exited with code 0 (0x0).
Microsoft.WindowsAzure.ServiceRuntime Information: 203 : Role entrypoint . CALLING Run(): Microsoft.WindowsAzure.ServiceRuntime.ProgramEntryPoint
Microsoft.WindowsAzure.ServiceRuntime Verbose: 500 : Role instance status check starting
Microsoft.WindowsAzure.ServiceRuntime Verbose: 502 : Role instance status check succeeded: Ready
Microsoft.WindowsAzure.ServiceRuntime Verbose: 500 : Role instance status check starting
Microsoft.WindowsAzure.ServiceRuntime Verbose: 502 : Role instance status check succeeded: Ready
WaWorkerHost.exe Information: 0 : NetCoreConsoleApp:Initiliaze:0
WaWorkerHost.exe Information: 0 : NetCoreConsoleApp:GetCscfgSetting('foo'):bar