Skip to content

Commit ce34b1c

Browse files
committed
new .net code
1 parent bfb35af commit ce34b1c

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

articles/azure-monitor/logs/tutorial-logs-ingestion-code.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,11 @@ The following script uses the [Azure Monitor Ingestion client library for .NET](
349349
350350
2. Replace the variables in the following sample code with values from your DCE and DCR. You may also want to replace the sample data with your own.
351351
352-
353-
354352
```csharp
355353
// Initialize variables
356-
var endpoint = new Uri("https://logs-ingestion-rzmk.eastus2-1.ingest.monitor.azure.com";);
357-
var ruleId = "dcr-00000000000000000000000000000000";
358-
var streamName = "Custom-MyTableRawData";
354+
var endpoint = new Uri("<data_collection_endpoint_uri>");
355+
var ruleId = "<data_collection_rule_id>";
356+
var streamName = "<stream_name>";
359357
360358
// Create credential and client
361359
var credential = new DefaultAzureCredential();
@@ -395,7 +393,44 @@ The following script uses the [Azure Monitor Ingestion client library for .NET](
395393
    });
396394
397395
// Upload logs
398-
Response response = client.Upload(ruleId, streamName, RequestContent.Create(data));
396+
try
397+
{
398+
    Response response = client.Upload(ruleId, streamName, RequestContent.Create(data));
399+
}
400+
catch (Exception ex)
401+
{
402+
    Console.WriteLine("Upload failed with Exception " + ex.Message);
403+
}
404+
405+
// Logs can also be uploaded in a List
406+
var entries = new List<Object>();
407+
for (int i = 0; i < 10; i++)
408+
{
409+
    entries.Add(
410+
        new {
411+
            Time = recordingNow,
412+
            Computer = "Computer" + i.ToString(),
413+
            AdditionalContext = i
414+
        }
415+
    );
416+
}
417+
418+
// Make the request
419+
LogsUploadOptions options = new LogsUploadOptions();
420+
bool isTriggered = false;
421+
options.UploadFailed += Options_UploadFailed;
422+
await client.UploadAsync(TestEnvironment.DCRImmutableId, TestEnvironment.StreamName, entries, options).ConfigureAwait(false);
423+
424+
Task Options_UploadFailed(LogsUploadFailedEventArgs e)
425+
{
426+
    isTriggered = true;
427+
    Console.WriteLine(e.Exception);
428+
    foreach (var log in e.FailedLogs)
429+
    {
430+
        Console.WriteLine(log);
431+
    }
432+
    return Task.CompletedTask;
433+
}
399434
```
400435
401436
3. Execute the code, and the data should arrive in your Log Analytics workspace within a few minutes.

0 commit comments

Comments
 (0)