I am trying to send data from my .net core API 2.1 version to Azure Log Analytics.
Below is my code:
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace xyz.Common
{
public class AzureLogAnalytics
{
public String WorkspaceId { get; set; }
public String SharedKey { get; set; }
public String ApiVersion { get; set; }
public String LogType { get; set; }
public AzureLogAnalytics(String workspaceId, String sharedKey, String logType, String apiVersion = "2015-03-20")
{
this.WorkspaceId = workspaceId;
this.SharedKey = sharedKey;
this.LogType = logType;
this.ApiVersion = apiVersion;
}
public void Post(string json)
{
string requestUriString = $"https://{WorkspaceId}.ods.opinsights.azure.com/api/logs?api-version={ApiVersion}";
DateTime dateTime = DateTime.UtcNow;
string dateString = dateTime.ToString("r");
string signature = GetSignature("POST", json.Length, "application/json", dateString, "/api/logs");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);
request.ContentType = "application/json";
request.Method = "POST";
request.Headers["Log-Type"] = LogType;
request.Headers["x-ms-date"] = dateString;
request.Headers["Authorization"] = signature;
byte[] content = Encoding.UTF8.GetBytes(json);
using (Stream requestStreamAsync = request.GetRequestStream())
{
requestStreamAsync.Write(content, 0, content.Length);
}
using (HttpWebResponse responseAsync = (HttpWebResponse)request.GetResponse())
{
if (responseAsync.StatusCode != HttpStatusCode.OK && responseAsync.StatusCode != HttpStatusCode.Accepted)
{
Stream responseStream = responseAsync.GetResponseStream();
if (responseStream != null)
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
throw new Exception(streamReader.ReadToEnd());
}
}
}
}
}
private string GetSignature(string method, int contentLength, string contentType, string date, string resource)
{
string message = $"{method}\n{contentLength}\n{contentType}\nx-ms-date:{date}\n{resource}";
byte[] bytes = Encoding.UTF8.GetBytes(message);
using (HMACSHA256 encryptor = new HMACSHA256(Convert.FromBase64String(SharedKey)))
{
return $"SharedKey {WorkspaceId}:{Convert.ToBase64String(encryptor.ComputeHash(bytes))}";
}
}
}
}
In the above code while accessing the
URL "$"https://{WorkspaceId}.ods.opinsights.azure.com/api/logs?api-version={ApiVersion}";
I am getting this error:
And because of this, I am not able to get a response in line
using (HttpWebResponse responseAsync = (HttpWebResponse)request.GetResponse())
There was an issue with the authorization and API version. I was using API Version 2015-03-20, I have changed it to 2016-04-01. I have found out this link which works - https://dejanstojanovic.net/aspnet/2018/february/send-data-to-azure-log-analytics-from-c-code/
Also, there are multiple links that I have gone through.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.