我已按照link中的说明在Azure中创建了Web应用程序,并在.Net核心框架中创建了Web API。
现在,在我的Web应用程序中,启用了“应用程序见解”。
在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
的文件。如何以及在何处禁用此功能?
答案 0 :(得分:1)
您可以使用ITelemetryProcessor过滤掉不需要的消息(包括跟踪)。
1。您可以使用应用程序洞察力在本地测试您的项目,然后使用应用程序洞察力搜索来检查不需要的跟踪消息,检查其为 CategoryName (或其他属性,可以指定它),例如下面的屏幕截图:
2。创建一个实现ITelemetryProcessor的自定义类。在这里,我使用CategoryName过滤掉不需要的跟踪消息,您可以自己调整代码:
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。经过测试,您可以看到不需要的跟踪消息被过滤掉了。
答案 1 :(得分:0)
不确定如何启用Ilogger与应用程序见解的集成,但是此处介绍了当前受支持的方式。 https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging
扩展方法的第二个参数控制应用程序见解获取哪些消息。 loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Warning);
这应限制将警告或更高级别的日志发送给应用程序见解。当然,您可以使用ITelemetry处理器来过滤日志,但是那样就更昂贵了,因为已经收集了日志,但后来又删除了日志,浪费了计算周期/内存。
答案 2 :(得分:0)
禁用某些类型的日志消息的另一种方法是通过appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore.Hosting.Internal.WebHost": "None",
"Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker": "None",
}
},
这将禁用与@Ivan Young答案相同的遥测,但允许您更改设置而无需更改代码。