Skip to content

Instantly share code, notes, and snippets.

@jongio
Created March 11, 2020 20:26
Show Gist options
  • Save jongio/1051c3b053e5ca538e298e686b42b448 to your computer and use it in GitHub Desktop.
Save jongio/1051c3b053e5ca538e298e686b42b448 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using Azure.Core;
using Azure.Core.Diagnostics;
using Azure.Identity;
using DotNetEnv;
namespace azid
{
class Program
{
static void Main(string[] args)
{
try
{
Env.Load();
using var listener = new AzureEventSourceListener((eventArgs, text) => AzureEventSourceHandler(eventArgs, text), EventLevel.Informational);
new DefaultAzureCredential().GetToken(new TokenRequestContext(new string[] { "https://management.azure.com/.default" }));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void AzureEventSourceHandler(EventWrittenEventArgs eventArgs, string text)
{
if (text.Contains("Credential"))
{
if (text.Contains("succeeded"))
{
Console.ForegroundColor = ConsoleColor.Green;
}
text = text.TrimStart("Azure.Identity.".ToCharArray());
var scopesIndex = text.IndexOf("Scopes:");
var exceptionIndex = text.IndexOf("Exception:");
var exceptionText = exceptionIndex >= 0 ? text.Substring(exceptionIndex) : "";
text = text.Substring(0, scopesIndex) + exceptionText;
Console.WriteLine(text);
}
Console.ResetColor();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment