static void Main(string[] args)
 {
     if (args.Length == 0)
     {
         Utility.Trace("duration missing for CollectNetworkTrace.exe");
         Utility.FlushTrace();
         return;
     }
     try
     {
         if (int.TryParse(args[0], out int duration))
         {
             duration = duration > 900 ? 60 : duration;
             var config     = NetworkTraceConfiguation.GetConfiguration();
             var authToken  = AuthService.GetAcccessToken(config);
             var armService = new ArmService(config, authToken);
             Utility.Trace($"Chosen duration for Network Trace = {duration}");
             armService.StartNetworkTrace(duration);
         }
         else
         {
             Utility.Trace($"Invalid argument value for duration = {args[0]}");
         }
     }
     catch (Exception ex)
     {
         Utility.Trace($"Exception while running the tool {ex.GetType()}:{ex.Message} {Environment.NewLine} {ex.StackTrace}");
     }
     finally
     {
         Utility.FlushTrace();
     }
 }
        public static NetworkTraceConfiguation GetConfiguration()
        {
            var secret = new NetworkTraceConfiguation()
            {
                TenantId       = GetConfigurationValue("NETWORKTRACE_TENANTID"),
                ClientId       = GetConfigurationValue("NETWORKTRACE_CLIENTID"),
                Secret         = GetConfigurationValue("NETWORKTRACE_CLIENTSECRET"),
                SiteName       = GetEnvironmentVariable("WEBSITE_SITE_NAME"),
                ResourceGroup  = GetEnvironmentVariable("WEBSITE_RESOURCE_GROUP"),
                SubscriptionId = GetSubscriptionId(),
                SlotName       = ConfigurationManager.AppSettings["NETWORKTRACE_SLOTNAME"]
            };

            return(secret);
        }
        public static string GetAcccessToken(NetworkTraceConfiguation config)
        {
            IConfidentialClientApplication app;

            app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
                  .WithClientSecret(config.Secret)
                  .WithTenantId(config.TenantId)
                  .Build();

            string[] scopes = new string[] { ScopeEndpoint };

            var result = app.AcquireTokenForClient(scopes)
                         .ExecuteAsync()
                         .ConfigureAwait(false).GetAwaiter().GetResult();

            Utility.Trace("Token acquired");
            return(result.AccessToken);
        }
 internal ArmService(NetworkTraceConfiguation config, string authToken)
 {
     _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
     _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     _config = config;
 }