You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.4 KiB
82 lines
3.4 KiB
using DealerSelection.Common.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace DealerSelection.Common.Logging;
|
|
|
|
public static class AzureLogHelper
|
|
{
|
|
private static string aZureWorkspaceId = ConfigurationHelper.GetSetting<string>("aZureWorkspaceId", true);
|
|
private static string aZureSharedKey = ConfigurationHelper.GetSetting<string>("aZureSharedKey", true);
|
|
private static string aZureLogType = ConfigurationHelper.GetSetting<string>("aZureLogType", true);
|
|
private static string aZureApiVersion = ConfigurationHelper.GetSetting<string>("aZureApiVersion", true);
|
|
|
|
public static void LogMessage(string message)
|
|
{
|
|
try
|
|
{
|
|
string azureLog = GetAzureLogFormatLog(message);
|
|
string requestUriString = $"https://{aZureWorkspaceId}.ods.opinsights.azure.com/api/logs?api-version={aZureApiVersion}";
|
|
DateTime dateTime = DateTime.UtcNow;
|
|
string dateString = dateTime.ToString("r");
|
|
string signature = GetSignature("POST", azureLog.Length, "application/json", dateString, "/api/logs");
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);
|
|
request.ContentType = "application/json";
|
|
request.Method = "POST";
|
|
request.Headers["Log-Type"] = aZureLogType;
|
|
request.Headers["x-ms-date"] = dateString;
|
|
request.Headers["Authorization"] = signature;
|
|
byte[] content = Encoding.UTF8.GetBytes(azureLog);
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// throw new UnexpectedDataException($" Bajaj Booking LogMessage: ", ex);
|
|
}
|
|
}
|
|
|
|
private static string GetAzureLogFormatLog(string inputMessage)
|
|
{
|
|
string message = string.Empty;
|
|
if (!string.IsNullOrWhiteSpace(inputMessage))
|
|
{
|
|
message = JsonConvert.SerializeObject(
|
|
new
|
|
{
|
|
id = Guid.NewGuid().ToString(),
|
|
datetime = DateTime.Now,
|
|
message = inputMessage
|
|
}).ToString();
|
|
}
|
|
return message;
|
|
}
|
|
|
|
private static 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(aZureSharedKey)))
|
|
{
|
|
return $"SharedKey {aZureWorkspaceId}:{Convert.ToBase64String(encryptor.ComputeHash(bytes))}";
|
|
}
|
|
}
|
|
}
|