在应用程序见解中禁用来自Web App的默认跟踪日志消息 [英] Disable default trace log messages from Web App in Application insights

查看:76
本文介绍了在应用程序见解中禁用来自Web App的默认跟踪日志消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照 在WebAPI中,有一些类似的日志记录代码.

I have created a Web App in Azure and a Web API in .Net core framework following the instructions in this link.
Now in my Web App , Application Insights is enabled.
In WebAPI has some similar code for logging.

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中打印一些类似的跟踪日志.

By default, it is printing some trace logs something like similar in 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的内容.如何以及在何处禁用此功能?

Now my requirement is it should not print all the default logs in Application Insights. It has to print only those with _logger.LogInformation. How and where do I disable this feature?

推荐答案

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

You can use ITelemetryProcessor to filter out the unwanted messages(including the Trace).

1.您可以使用应用程序洞察力在本地测试项目,然后使用应用程序洞察力搜索来检查不需要的跟踪消息,并检查其 CategoryName (或其他属性,可以指定它),如下面的屏幕截图:

1.You can test your project locally with app insights, and then use Application insights search to check the unwanted trace messages, check it's CategoryName(or others property which can specify it), like screenshot below:

2.创建一个实现ITelemetryProcessor的自定义类.在这里,我使用CategoryName过滤掉不需要的跟踪消息,您可以自己调整代码:

2.Create a custom class which implements the ITelemetryProcessor. Here I use the CategoryName to filter out the unwanted trace messages, you can adjust the code yourself:

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

3.In Startup.cs -> ConfigureServices() method, add the following code:

services.AddApplicationInsightsTelemetry();

services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();

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

4.After test, you can see the unwanted trace messages are filtered out.

这篇关于在应用程序见解中禁用来自Web App的默认跟踪日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆