C# 在Application insights中禁用来自Web应用程序的默认跟踪日志消息

C# 在Application insights中禁用来自Web应用程序的默认跟踪日志消息,c#,azure,asp.net-core-mvc,azure-application-insights,azure-web-app-service,C#,Azure,Asp.net Core Mvc,Azure Application Insights,Azure Web App Service,我已经按照本文中的说明在Azure中创建了一个Web应用程序,在.Net core framework中创建了一个Web API。 现在,在我的Web应用程序中,已启用应用程序洞察功能。 在WebAPI中,有一些类似的日志代码 public class Startup { public Startup() { } public void Configure(IApplicationBuilder app, IHostingEnvironment env, IL

我已经按照本文中的说明在Azure中创建了一个Web应用程序,在.Net core framework中创建了一个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中的所有默认日志。它必须只打印带有
\u logger.LogInformation
的文件。如何以及在何处禁用此功能?

您可以使用ITelemetryProcessor过滤掉不需要的消息(包括跟踪)

1.您可以使用app insights在本地测试您的项目,然后使用Application insights search检查不需要的跟踪消息,检查它的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>();
services.AddApplicationInsightsTelemetry();
AddApplicationInsightsTelemetryProcessor();

4.测试后,您可以看到不需要的跟踪消息被过滤掉。

不确定您是如何启用Ilogger与application insights的集成的,但此处描述了当前支持的方式。

扩展方法的第二个参数控制应用程序洞察获取哪些消息。 loggerFactory.AddApplicationInsights(app.ApplicationServices,LogLevel.Warning)


这应该限制发送给application insights的警告日志或以上日志。当然,您可以使用ITelemetryprocessor来过滤日志,但这会更加昂贵,因为日志已被收集,但随后会被删除,从而浪费计算周期/内存。

另一种禁用特定类型日志消息的方法是通过appsettings.json:

"Logging": {
 "LogLevel": {
   "Default": "Trace",
   "Microsoft.AspNetCore.Hosting.Internal.WebHost": "None",
   "Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker": "None",
 }
},

这将禁用与@Ivan Young的答案相同的遥测功能,但允许您在不更改代码的情况下更改设置。

这些日志在Azure中还是在您的VS中?桃洲在Azure中。您可以使用ITelemetryProcessor过滤掉不必要的跟踪消息。@IvanYang您太好了,如果您能给我一个示例Yes,就一会儿,这对我不起作用。。。我仍然可以看到所有的内部日志。
"Logging": {
 "LogLevel": {
   "Default": "Trace",
   "Microsoft.AspNetCore.Hosting.Internal.WebHost": "None",
   "Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker": "None",
 }
},