使用 Microsoft Entra 应用程序的值创建以下环境变量。 这些值在 Azure 标识库中由 DefaultAzureCredential 使用。
AZURE_TENANT_ID
AZURE_CLIENT_ID
AZURE_CLIENT_SECRET
将以下示例代码中的变量替换为 DCR 中的值。 你可能还想要将示例数据替换为自己的数据。
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.Monitor.Ingestion;
// Initialize variables
var endpoint = new Uri("https://my-url.monitor.azure.cn");
var ruleId = "dcr-00000000000000000000000000000000";
var streamName = "Custom-MyTableRawData";
// Create credential and client
var credential = new DefaultAzureCredential();
LogsIngestionClient client = new(endpoint, credential);
DateTimeOffset currentTime = DateTimeOffset.UtcNow;
// Use BinaryData to serialize instances of an anonymous type into JSON
BinaryData data = BinaryData.FromObjectAsJson(
new[] {
new
{
Time = currentTime,
Computer = "Computer1",
AdditionalContext = new
{
InstanceName = "user1",
TimeZone = "Pacific Time",
Level = 4,
CounterName = "AppMetric1",
CounterValue = 15.3
}
},
new
{
Time = currentTime,
Computer = "Computer2",
AdditionalContext = new
{
InstanceName = "user2",
TimeZone = "Central Time",
Level = 3,
CounterName = "AppMetric1",
CounterValue = 23.5
}
},
});
// Upload logs
try
{
var response = await client.UploadAsync(ruleId, streamName, RequestContent.Create(data)).ConfigureAwait(false);
if (response.IsError)
{
throw new Exception(response.ToString());
}
Console.WriteLine("Log upload completed using content upload");
}
catch (Exception ex)
{
Console.WriteLine("Upload failed with Exception: " + ex.Message);
}
// Logs can also be uploaded in a List
var entries = new List<object>();
for (int i = 0; i < 10; i++)
{
entries.Add(
new
{
Time = currentTime,
Computer = "Computer" + i.ToString(),
AdditionalContext = new
{
InstanceName = "user" + i.ToString(),
TimeZone = "Central Time",
Level = 3,
CounterName = "AppMetric1" + i.ToString(),
CounterValue = i
}
}
);
}
// Make the request
try
{
var response = await client.UploadAsync(ruleId, streamName, entries).ConfigureAwait(false);
if (response.IsError)
{
throw new Exception(response.ToString());
}
Console.WriteLine("Log upload completed using list of entries");
}
catch (Exception ex)
{
Console.WriteLine("Upload failed with Exception: " + ex.Message);
}
### Step 0: Set variables required for the rest of the script.
# information needed to authenticate to AAD and obtain a bearer token
$tenantId = "00000000-0000-0000-00000000000000000" #Tenant ID the data collection endpoint resides in
$appId = " 000000000-0000-0000-00000000000000000" #Application ID created and granted permissions
$appSecret = "0000000000000000000000000000000000000000" #Secret created for the application
# information needed to send data to the DCR endpoint
$endpoint_uri = "https://my-url.monitor.azure.cn" #Logs ingestion URI for the DCR
$dcrImmutableId = "dcr-00000000000000000000000000000000" #the immutableId property of the DCR object
$streamName = "Custom-MyTableRawData" #name of the stream in the DCR that represents the destination table
### Step 1: Obtain a bearer token used later to authenticate against the DCR.
$scope= [System.Web.HttpUtility]::UrlEncode("https://monitor.azure.cn//.default")
$body = "client_id=$appId&scope=$scope&client_secret=$appSecret&grant_type=client_credentials";
$headers = @{"Content-Type"="application/x-www-form-urlencoded"};
$uri = "https://login.partner.microsoftonline.cn/$tenantId/oauth2/v2.0/token"
$bearerToken = (Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers).access_token
### Step 2: Create some sample data.
$currentTime = Get-Date ([datetime]::UtcNow) -Format O
$staticData = @"
[
{
"Time": "$currentTime",
"Computer": "Computer1",
"AdditionalContext": {
"InstanceName": "user1",
"TimeZone": "Pacific Time",
"Level": 4,
"CounterName": "AppMetric1",
"CounterValue": 15.3
}
},
{
"Time": "$currentTime",
"Computer": "Computer2",
"AdditionalContext": {
"InstanceName": "user2",
"TimeZone": "Central Time",
"Level": 3,
"CounterName": "AppMetric1",
"CounterValue": 23.5
}
}
]
"@;
### Step 3: Send the data to the Log Analytics workspace.
$body = $staticData;
$headers = @{"Authorization"="Bearer $bearerToken";"Content-Type"="application/json"};
$uri = "$endpoint_uri/dataCollectionRules/$dcrImmutableId/streams/$($streamName)?api-version=2023-01-01"
$uploadResponse = Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers
注意
如果收到 Unable to find type [System.Web.HttpUtility]. 错误,请运行脚本第 1 部分中用于修复的最后一行并执行它。 在取消注释的情况下将其作为脚本的一部分执行不能解决此问题。 必须单独执行该命令。