我正在尝试使用 Web API 和 .net Framework 4.7.2 将日志上传到 Azure Log Analytics。我按照本教程在控制台应用程序中进行了测试,只是为了检查它是否有效:
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/tutorial-logs-ingestion-portal
当我从控制台应用程序运行它时,一切正常,我可以看到 azure 上的日志和所有内容,但是当我将相同的代码复制并粘贴到 Web API 端点时,我总是收到此错误:
Azure.Identity.AuthenticationFailedExceptionHResult=0x80131500Message=ClientSecretCredential authentication failed: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy.Source=Azure.Identity
我以为我在 azure 上拥有错误的权限,但到目前为止我无法弄清楚问题出在哪里。
此外,我正在使用免费试用天蓝色帐户进行此测试,这可能与此处相关。
我正在使用的代码:
using Azure.Core;
using Azure.Identity;
using Azure.Monitor.Ingestion;
var endpoint = new Uri("XXXXXXXXXXXXX");
var ruleId = "XXXXXXXXXX";
var streamName = "Custom-SimpleLog_CL";
// Create credential and client
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var credential = new ClientSecretCredential("XXXXXXXXXXXX", "XXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX", options);
LogsIngestionClient client = new LogsIngestionClient(endpoint, credential);
// Use BinaryData to serialize instances of an anonymous type into JSON
BinaryData data = BinaryData.FromObjectAsJson(
new[] {
new
{
RawData = "Some test JSON",
TimeGenerated = DateTime.UtcNow.ToString()
}
});
//Upload logs
try
{
var response = client.Upload(ruleId, streamName, RequestContent.Create(data));
}
catch (Exception ex)
{
Console.WriteLine("Upload failed with Exception " + ex.Message);
}
因此,在不同的环境中进行了相当多的测试之后,我意识到要检查安全协议:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
在上传调用之前添加此解决了问题。