![](/img/trans.png)
[英]Trace, Debug and Information messages not reaching App Insights from WorkerService
[英]Disable default trace log messages from Web App in Application insights
我已經按照此鏈接中的說明在 Azure 中創建了一個 Web 應用程序,並在 .Net 核心框架中創建了一個 Web API。
現在在我的 Web App 中,啟用了 Application Insights。
在 WebAPI 中有一些類似的代碼用於日志記錄。
public class Startup
{
public Startup()
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
var logger = loggerFactory.CreateLogger<ConsoleLogger>();
logger.LogInformation("Executing Configure()");
}
}
public class HomeController : Controller
{
ILogger _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Executing Home/Index")
return View();
}
}
默認情況下,它會打印一些類似於 Application Insights 中的跟蹤日志。
2019-01-02T07:22:49 Executing Home/Index
2019-01-02T07:22:49 Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
2019-01-02T07:22:50 Executed action [SomeActionName] (APIName) in 6487.7982ms
2019-01-02T07:22:50 Request finished in 6917.8019ms 200 application/json; charset=utf-8
現在我的要求是它不應該打印 Application Insights 中的所有默認日志。 它必須只打印帶有_logger.LogInformation
那些。 如何以及在何處禁用此功能?
您可以使用 ITelemetryProcessor 過濾掉不需要的消息(包括 Trace)。
1.您可以使用應用洞察在本地測試您的項目,然后使用應用洞察搜索來檢查不需要的跟蹤消息,檢查它的CategoryName (或其他可以指定它的屬性),如下圖所示:
2. 創建一個實現 ITelemetryProcessor 的自定義類。 這里我使用CategoryName來過濾掉不需要的trace消息,你可以自己調整代碼:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace WebApplication1netcore4
{
public class MyTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public MyTelemetryProcessor(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry telemetry)
{
TraceTelemetry trace = telemetry as TraceTelemetry;
if (trace != null && trace.Context.Properties.Keys.Contains("CategoryName"))
{
//Here I just filter out 2 kinds of trace messages, you can adjust your code as per your need.
if (trace.Context.Properties["CategoryName"] == "Microsoft.AspNetCore.Hosting.Internal.WebHost" || trace.Context.Properties["CategoryName"] == "Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker")
{
//return means abandon this trace message which has the specified CategoryName
return;
}
}
if (trace == null)
{
this.Next.Process(telemetry);
}
if (trace != null)
{
this.Next.Process(trace);
}
}
}
}
3.在Startup.cs -> ConfigureServices()方法中,添加如下代碼:
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();
4.經過測試,可以看到不需要的trace消息被過濾掉了。
另一種禁用某些類型的日志消息的方法是通過 appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore.Hosting.Internal.WebHost": "None",
"Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker": "None",
}
},
這會禁用與@Ivan Young 的回答相同的遙測,但允許您在不更改代碼的情況下更改設置。
不確定您如何啟用 Ilogger 與應用程序洞察的集成,但此處描述了當前支持的方式。 https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging
擴展方法的第二個參數控制應用程序洞察選擇哪些消息。 loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
這應該限制發送到應用程序洞察的日志警告或以上。 您當然可以使用 ITelemetryprocessor 來過濾日志,但這會更昂貴,因為日志已經收集但后來被丟棄,浪費計算周期/內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.