Skip to content

Commit c5249c2

Browse files
authored
Update tutorial-logs-ingestion-code.md
1 parent 5d5c478 commit c5249c2

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,54 @@ The following script uses the [Azure Monitor Ingestion client library for .NET](
8787
// Upload logs
8888
try
8989
{
90-
    Response response = client.Upload(ruleId, streamName, RequestContent.Create(data));
90+
var response = await client.UploadAsync(ruleId, streamName, RequestContent.Create(data)).ConfigureAwait(false);
91+
if (response.IsError)
92+
{
93+
throw new Exception(response.ToString());
94+
}
95+
96+
Console.WriteLine("Log upload completed using content upload");
9197
}
9298
catch (Exception ex)
9399
{
94-
    Console.WriteLine("Upload failed with Exception " + ex.Message);
100+
Console.WriteLine("Upload failed with Exception: " + ex.Message);
95101
}
96102
97103
// Logs can also be uploaded in a List
98-
var entries = new List<Object>();
104+
var entries = new List<object>();
99105
for (int i = 0; i < 10; i++)
100106
{
101-
    entries.Add(
102-
        new {
103-
            Time = recordingNow,
104-
            Computer = "Computer" + i.ToString(),
105-
            AdditionalContext = i
106-
        }
107-
    );
107+
entries.Add(
108+
new
109+
{
110+
Time = currentTime,
111+
Computer = "Computer" + i.ToString(),
112+
AdditionalContext = new
113+
{
114+
InstanceName = "user" + i.ToString(),
115+
TimeZone = "Central Time",
116+
Level = Random.Shared.Next(1, 5),
117+
CounterName = "AppMetric1" + i.ToString(),
118+
CounterValue = i
119+
}
120+
}
121+
);
108122
}
109123
110124
// Make the request
111-
LogsUploadOptions options = new LogsUploadOptions();
112-
bool isTriggered = false;
113-
options.UploadFailed += Options_UploadFailed;
114-
await client.UploadAsync(TestEnvironment.DCRImmutableId, TestEnvironment.StreamName, entries, options).ConfigureAwait(false);
125+
try
126+
{
127+
var response = await client.UploadAsync(ruleId, streamName, entries).ConfigureAwait(false);
128+
if (response.IsError)
129+
{
130+
throw new Exception(response.ToString());
131+
}
115132
116-
Task Options_UploadFailed(LogsUploadFailedEventArgs e)
133+
Console.WriteLine("Log upload completed using list of entries");
134+
}
135+
catch (Exception ex)
117136
{
118-
    isTriggered = true;
119-
    Console.WriteLine(e.Exception);
120-
    foreach (var log in e.FailedLogs)
121-
    {
122-
        Console.WriteLine(log);
123-
    }
124-
    return Task.CompletedTask;
137+
Console.WriteLine("Upload failed with Exception: " + ex.Message);
125138
}
126139
```
127140

0 commit comments

Comments
 (0)