Created
March 11, 2020 20:26
-
-
Save jongio/1051c3b053e5ca538e298e686b42b448 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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