代码之家  ›  专栏  ›  技术社区  ›  CrazyCoder

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

  •  0
  • CrazyCoder  · 技术社区  · 7 年前

    link
    现在,在我的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();
        } 
    }
    

    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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ivan Glasenberg    7 年前

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

    1.您可以使用app insights在本地测试您的项目,然后使用 应用程序洞察搜索 类型名

    enter image description here

    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中->方法,添加以下代码:

    services.AddApplicationInsightsTelemetry();
    
    services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();
    

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

        2
  •  1
  •   Vishal    6 年前

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

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

        3
  •  0
  •   cijothomas    7 年前

    不确定您是如何启用Ilogger与application insights的集成的,但这里介绍了当前支持的方式。 https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging

    扩展方法的第二个参数控制应用程序洞察获取哪些消息。

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