如何从AI日志中排除参数?

时间:2019-06-24 06:55:34

标签: parameters azure-application-insights

我想从Application Insights的跟踪和依赖项日志中排除一些参数(例如,城市,用户ID,应用角色...)。

您有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您可以使用ITelemetryInitializer将userId /应用角色的值更改为空。然后在AI日志中,城市,用户ID,应用角色将被自动删除。

我使用 .net核心网络应用进行了测试:

1。向Web项目添加 MyTelemetryInitializer 类:

    public class MyTelemetryInitializer: ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            //only apply for Trace and Dependency
            if (telemetry is TraceTelemetry || telemetry is DependencyTelemetry)
            {
                telemetry.Context.User.Id = "";
                telemetry.Context.Cloud.RoleInstance = "";
                telemetry.Context.Cloud.RoleName = "";
            }
        }    
    }

2。在Startup.cs-> ConfigureServices方法中,注册自定义遥测初始化程序:

        public void ConfigureServices(IServiceCollection services)
        {
            //other code

            services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

3。在HomeController.cs中,创建一个TelemetryClient以发送遥测数据:

    public class HomeController : Controller
    {

        TelemetryClient client = new TelemetryClient();
        public IActionResult Index()
        {
            client.TrackTrace("0624-2 index: this is a trace message!!!");
            return View();
        }

        public IActionResult About()
        {
            client.TrackTrace("0624-2 about: this is a trace message!!!");

            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            client.TrackTrace("0624-2 contact: this is a trace message!!!");
            ViewData["Message"] = "Your contact page.";

            return View();
        }
   }

4。运行项目后,等待几分钟,然后导航至azure门户->您的应用洞察力->搜索页,检查跟踪消息的属性:

enter image description here