Example #1
0
        public void CanSetServiceTimeout_Mocked()
        {
            string   preferHeader    = null;
            TimeSpan?networkOverride = default;

            var mockTransport = MockTransport.FromMessageCallback(message =>
            {
                Assert.True(message.Request.Headers.TryGetValue("prefer", out preferHeader));
                networkOverride = message.NetworkTimeout;

                return(new MockResponse(403));
            });

            var client = new LogsQueryClient(new Uri("https://api.loganalytics.io"), new MockCredential(), new LogsQueryClientOptions()
            {
                Transport = mockTransport
            });

            Assert.ThrowsAsync <RequestFailedException>(() => client.QueryAsync("wid", "tid", TimeSpan.FromDays(1), options: new LogsQueryOptions()
            {
                ServerTimeout = TimeSpan.FromMinutes(10)
            }));

            Assert.AreEqual("wait=600", preferHeader);
            // The network timeout is adjusted with 15 sec buffer
            Assert.AreEqual(TimeSpan.FromMinutes(10).Add(TimeSpan.FromSeconds(15)), networkOverride);
        }
Example #2
0
        public async Task UsesDefaultAuthScope(string scope, string expectedScope)
        {
            var mockTransport = MockTransport.FromMessageCallback(message =>
            {
                var mockResponse = new MockResponse(200);
                mockResponse.SetContent("{\"tables\":[]}");
                return(mockResponse);
            });

            Mock <MockCredential> mock = new() { CallBase = true };

            string[] scopes = null;
            mock.Setup(m => m.GetTokenAsync(It.IsAny <TokenRequestContext>(), It.IsAny <CancellationToken>()))
            .Callback <TokenRequestContext, CancellationToken>((c, _) => scopes = c.Scopes)
            .CallBase();

            var client = new LogsQueryClient(mock.Object, new LogsQueryClientOptions()
            {
                Transport           = mockTransport,
                AuthenticationScope = scope
            });

            await client.QueryAsync("", "", DateTimeRange.All);

            Assert.AreEqual(new[] { expectedScope }, scopes);
        }
    }
Example #3
0
        public async Task QueryLogsAsPrimitive()
        {
            #region Snippet:QueryLogsAsPrimitive

#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            var client = new LogsQueryClient(new DefaultAzureCredential());

            // Query TOP 10 resource groups by event count
            #region Snippet:QueryLogsAsPrimitiveCall
            Response <IReadOnlyList <string> > response = await client.QueryAsync <string>(
                workspaceId,
                "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup",
                new DateTimeRange(TimeSpan.FromDays(1)));

            #endregion

            foreach (var resourceGroup in response.Value)
            {
                Console.WriteLine(resourceGroup);
            }

            #endregion
        }
Example #4
0
        public async Task QueryLogsAsTable()
        {
            #region Snippet:QueryLogsAsTable
#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif
            #region Snippet:CreateLogsClient
            var client = new LogsQueryClient(new DefaultAzureCredential());
            #endregion
            Response <LogsQueryResult> response = await client.QueryAsync(
                workspaceId,
                "AzureActivity | top 10 by TimeGenerated",
                new DateTimeRange(TimeSpan.FromDays(1)));

            LogsQueryResultTable table = response.Value.PrimaryTable;

            foreach (var row in table.Rows)
            {
                Console.WriteLine(row["OperationName"] + " " + row["ResourceGroup"]);
            }

            #endregion
        }
Example #5
0
        public async Task QueryLogsWithTimeout()
        {
            #region Snippet:QueryLogsWithTimeout
#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            var client = new LogsQueryClient(new DefaultAzureCredential());

            // Query TOP 10 resource groups by event count
            Response <IReadOnlyList <int> > response = await client.QueryAsync <int>(
                workspaceId,
                "AzureActivity | summarize count()",
                new DateTimeRange(TimeSpan.FromDays(1)),
                options : new LogsQueryOptions
            {
                ServerTimeout = TimeSpan.FromMinutes(10)
            });

            foreach (var resourceGroup in response.Value)
            {
                Console.WriteLine(resourceGroup);
            }

            #endregion
        }
Example #6
0
        public async Task QueryLogsAsModels()
        {
            #region Snippet:QueryLogsAsModels

            var client = new LogsQueryClient(TestEnvironment.LogsEndpoint, new DefaultAzureCredential());
#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            // Query TOP 10 resource groups by event count
            #region Snippet:QueryLogsAsModelCall
            Response <IReadOnlyList <MyLogEntryModel> > response = await client.QueryAsync <MyLogEntryModel>(
                workspaceId,
                "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
                new DateTimeRange(TimeSpan.FromDays(1)));

            #endregion

            foreach (var logEntryModel in response.Value)
            {
                Console.WriteLine($"{logEntryModel.ResourceGroup} had {logEntryModel.Count} events");
            }

            #endregion
        }
Example #7
0
        public async Task QueryLogsAsPrimitive()
        {
            #region Snippet:QueryLogsAsPrimitive

#if SNIPPET
            Uri    endpoint    = new Uri("https://api.loganalytics.io");
            string workspaceId = "<workspace_id>";
#else
            Uri    endpoint    = TestEnvironment.LogsEndpoint;
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            LogsQueryClient client = new LogsQueryClient(endpoint, new DefaultAzureCredential());

            // Query TOP 10 resource groups by event count
            Response <IReadOnlyList <string> > response = await client.QueryAsync <string>(workspaceId,
                                                                                           "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup",
                                                                                           TimeSpan.FromDays(1));

            foreach (var resourceGroup in response.Value)
            {
                Console.WriteLine(resourceGroup);
            }

            #endregion
        }
        public async Task UsesDefaultEndpoint()
        {
            var mockTransport = MockTransport.FromMessageCallback(_ =>
            {
                var mockResponse = new MockResponse(200);
                mockResponse.SetContent("{\"tables\":[]}");
                return(mockResponse);
            });

            var client = new LogsQueryClient(new MockCredential(), new LogsQueryClientOptions()
            {
                Transport = mockTransport
            });

            await client.QueryAsync("", "", QueryTimeRange.All);

            StringAssert.StartsWith("https://api.monitor.azure.com", mockTransport.SingleRequest.Uri.ToString());
        }
Example #9
0
        public async Task UsesDefaultEndpoint()
        {
            string uri           = null;
            var    mockTransport = MockTransport.FromMessageCallback(message =>
            {
                uri = message.Request.Uri.ToString();
                var mockResponse = new MockResponse(200);
                mockResponse.SetContent("{\"tables\":[]}");
                return(mockResponse);
            });

            var client = new LogsQueryClient(new MockCredential(), new LogsQueryClientOptions()
            {
                Transport = mockTransport
            });

            await client.QueryAsync("", "", DateTimeRange.All);

            StringAssert.StartsWith("https://api.loganalytics.io", uri);
        }
Example #10
0
        public async Task BadRequest()
        {
            #region Snippet:BadRequest
#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            var client = new LogsQueryClient(new DefaultAzureCredential());

            try
            {
                await client.QueryAsync(
                    workspaceId, "My Not So Valid Query", new DateTimeRange(TimeSpan.FromDays(1)));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            #endregion
        }
Example #11
0
        public async Task QueryLogsAsTablePrintAll()
        {
            #region Snippet:QueryLogsPrintTable

#if SNIPPET
            string workspaceId = "<workspace_id>";
#else
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            var client = new LogsQueryClient(new DefaultAzureCredential());
            Response <LogsQueryResult> response = await client.QueryAsync(
                workspaceId,
                "AzureActivity | top 10 by TimeGenerated",
                new DateTimeRange(TimeSpan.FromDays(1)));

            LogsQueryResultTable table = response.Value.PrimaryTable;

            foreach (var column in table.Columns)
            {
                Console.Write(column.Name + ";");
            }

            Console.WriteLine();

            var columnCount = table.Columns.Count;
            foreach (var row in table.Rows)
            {
                for (int i = 0; i < columnCount; i++)
                {
                    Console.Write(row[i] + ";");
                }

                Console.WriteLine();
            }

            #endregion
        }
Example #12
0
        public async Task BadRequest()
        {
            #region Snippet:BadRequest
#if SNIPPET
            Uri    endpoint    = new Uri("https://api.loganalytics.io");
            string workspaceId = "<workspace_id>";
#else
            Uri    endpoint    = TestEnvironment.LogsEndpoint;
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            LogsQueryClient client = new LogsQueryClient(endpoint, new DefaultAzureCredential());

            try
            {
                await client.QueryAsync(workspaceId, "My Not So Valid Query", TimeSpan.FromDays(1));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            #endregion
        }
Example #13
0
        public async Task QueryLogsAsTable()
        {
            #region Snippet:QueryLogsAsTable
#if SNIPPET
            Uri    endpoint    = new Uri("https://api.loganalytics.io");
            string workspaceId = "<workspace_id>";
#else
            Uri    endpoint    = TestEnvironment.LogsEndpoint;
            string workspaceId = TestEnvironment.WorkspaceId;
#endif

            LogsQueryClient            client   = new LogsQueryClient(endpoint, new DefaultAzureCredential());
            Response <LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

            LogsQueryResultTable table = response.Value.PrimaryTable;

            foreach (var row in table.Rows)
            {
                Console.WriteLine(row["OperationName"] + " " + row["ResourceGroup"]);
            }

            #endregion
        }
 public override async Task RunAsync(CancellationToken cancellationToken)
 {
     await LogsQueryClient.QueryAsync <TestModelForTypes>(TestEnvironment.WorkspaceId, LogsQuery, DateTimeRange.All, cancellationToken : cancellationToken).ConfigureAwait(false);
 }