Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序洞察-全局最低严重性级别筛选器?_C#_.net_Azure_Azure Application Insights - Fatal编程技术网

C# 应用程序洞察-全局最低严重性级别筛选器?

C# 应用程序洞察-全局最低严重性级别筛选器?,c#,.net,azure,azure-application-insights,C#,.net,Azure,Azure Application Insights,是否有一个地方可以设置日志写入的最低严重性?它还应适用于通过TelemetryClient.TraceTelemetry写入的记录道!有几个问题只涉及ILogger。没有直接的方法。但您可以使用来确定可以记录哪个严重级别 例如,创建一个实现ITelemetryProcessor的类,如下所示: public class MyCustomFilter : ITelemetryProcessor { private ITelemetryProcessor Next { get; set; }

是否有一个地方可以设置日志写入的最低严重性?它还应适用于通过TelemetryClient.TraceTelemetry写入的记录道!有几个问题只涉及ILogger。

没有直接的方法。但您可以使用来确定可以记录哪个
严重级别

例如,创建一个实现ITelemetryProcessor的类,如下所示:

public class MyCustomFilter : ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; set; }

    // next will point to the next TelemetryProcessor in the chain.
    public MyCustomFilter(ITelemetryProcessor next)
    {
        this.Next = next;
    }

    public void Process(ITelemetry item)
    {
        TraceTelemetry traceTelemetry = item as TraceTelemetry;

        if (traceTelemetry != null)
        {
            //use this line of code to determine which SeverityLevel should be logged. In this example, only SeverityLevel is warning and up can be logged
            if (traceTelemetry.SeverityLevel < SeverityLevel.Warning) { return; }
        }

        this.Next.Process(item);
    }
}
公共类MyCustomFilter:ITelemetryProcessor
{
私有ITelemetryProcessor Next{get;set;}
//next将指向链中的下一个遥测处理器。
公共MyCustomFilter(ITelemetryProcessor下一步)
{
this.Next=Next;
}
公共作废流程(ITelemetry项)
{
TraceTelemetry TraceTelemetry=作为TraceTelemetry的项目;
如果(traceTelemetry!=null)
{
//使用这行代码确定应该记录哪个SeverityLevel。在本例中,只有SeverityLevel为warning,并且可以记录up
if(tracetelmetry.SeverityLevel
然后在Startup.cs(用于.net核心web应用)中注册它:

public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry();
//注册您的自定义遥测处理器
AddApplicationInsightsTelemetryProcessor();
}

你的项目是什么。net还是.net内核?web还是控制台项目?@IvanYang.net core,3.1,最新SDK,控制台和web都有。请确认,您实际上是指日志级别吗?比如只捕获日志级别,比如信息、跟踪或异常?我的意思是
SeverityLevel
。假设我不想再注册详细日志,只有警告和upI实际认为
services.AddApplicationInsightsTelemetryProcessor
无法正常工作,您必须使用
aiConfig.TelemetryProcessorChainBuilder.use(next=>new SeverityLevel FilterTelemetryProcessor(next,config))
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddApplicationInsightsTelemetry();

        //register your custom TelemetryProcessor
        services.AddApplicationInsightsTelemetryProcessor<MyCustomFilter>();
    }