如何将自定义日志文件从 Azure 应用服务导入到 Azure Monitor

How to import custom log file from Azure App Service into Azure Monitor

我有一个作为 Web 应用程序托管在 Azure 中的 .Net 应用程序。此应用程序使用 Serilog 将自定义日志写入存储在站点根目录中的文件。我希望找到一种方法将此日志文件引入 Azure Monitor 并设置电子邮件警报,以便在检测到文件的特定行(错误事件)时发送电子邮件。我已尝试在 this guide 之后在 Azure Monitor 中设置自定义日志,但是当尝试 运行 查询时,未找到任何结果。任何人有任何提示或可以指出正确的方向将不胜感激。

谢谢!

实现您的要求的最佳方法之一是利用 Azure Log Analytics HTTP Data Collector API,这有助于将自定义日志数据发送到 Azure 监视器 Log Analytics 工作区存储库。为了便于说明,您还可以在文章中查看示例代码。

// Send a request to the POST API endpoint
public static void PostData(string signature, string date, string json) {
try {
string url = "https://" + customerId + ".ods.opinsights.azure.com/api/logs?api-version=2016-04-01";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); 
client.DefaultRequestHeaders.Add("Accept", "application/json"); 
client.DefaultRequestHeaders.Add("Log-Type", LogName); 
client.DefaultRequestHeaders.Add("Authorization", signature); 
client.DefaultRequestHeaders.Add("x-ms-date", date); 
client.DefaultRequestHeaders.Add("time-generated-field", TimeStampField);  

System.Net.Http.HttpContent httpContent = new StringContent(json, Encoding.UTF8); 
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
Task<System.Net.Http.HttpResponseMessage> response = client.PostAsync(new Uri(url), httpContent); 
System.Net.Http.HttpContent responseContent = response.Result.Content; string result = responseContent.ReadAsStringAsync().Result; 
Console.WriteLine("Return Result: " + result); 
}
 catch (Exception excep) { 
 Console.WriteLine("API Post Exception: " + excep.Message); 
 } }