-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
LogsUploadOptions.cs
84 lines (75 loc) · 3.17 KB
/
LogsUploadOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Core.Serialization;
namespace Azure.Monitor.Ingestion
{
/// <summary>
/// The options model to configure the request to upload logs to Azure Monitor.
/// </summary>
public class LogsUploadOptions
{
/// <summary>
/// The serializer to use to convert the log objects to JSON.
/// <remarks> Default Serializer is System.Text.Json. </remarks>
/// </summary>
public ObjectSerializer Serializer { get; set; }
/// <summary>
/// The max concurrent requests to send to the Azure Monitor service when uploading logs.
/// <remarks> In the Upload method, this parameter is not used as the batches are uploaded in sequence. For parallel uploads, if this value is not set the default concurrency will be 5. </remarks>
/// </summary>
public int MaxConcurrency
{
get { return _maxConcurrency; }
set { _maxConcurrency = AssertNotNegative(value, "MaxConcurrency"); }
}
private int _maxConcurrency = 5;
/// <summary>
/// An optional EventHandler that provides the list of failed logs and the corresponding exception.
/// </summary>
public event SyncAsyncEventHandler<LogsUploadFailedEventArgs> UploadFailed;
internal virtual async Task InvokeEvent(LogsUploadFailedEventArgs uploadFailedArgs)
{
await UploadFailed.RaiseAsync(uploadFailedArgs, nameof(LogsIngestionClient), "Upload", uploadFailedArgs.ClientDiagnostics).ConfigureAwait(false);
}
internal virtual async Task<Exception> OnUploadFailedAsync(LogsUploadFailedEventArgs eventArgs)
{
try
{
if (eventArgs.IsRunningSynchronously)
{
#pragma warning disable AZC0103 // Do not wait synchronously in asynchronous scope.
// for customer code so async not ran over sync
InvokeEvent(eventArgs).GetAwaiter().GetResult();
#pragma warning restore AZC0103 // Do not wait synchronously in asynchronous scope.
}
else
{
await InvokeEvent(eventArgs).ConfigureAwait(false);
}
return null;
}
catch (Exception ex)
{
// return exception to caller and caller should check exception to abort processing and rethrow this exception
return ex;
}
}
internal static int AssertNotNegative(int argumentValue, string argumentName)
{
if (argumentValue <= 0)
{
throw new ArgumentOutOfRangeException(argumentName, $"Argument {argumentName} must be a non-negative concurrency (integer) value. The provided value was {argumentValue}.");
}
else
return argumentValue;
}
internal bool HasHandler => UploadFailed != null;
}
}