过滤掉记录的语句
Filtering out logged statement
我正在寻找一种方法来减少发送到 Application Insights 的记录语句的数量。
虽然 .NET 日志记录允许使用 LogLevels
and categories, there's nothing that would allow removing log entries based on the logged content. Filter function 过滤掉日志,但只能对提供程序名称、类别和 LogLevel
进行操作。
这可能吗?
您可以使用多种方法。首先,Application Insights (AI) 允许您应用sampling。在大多数情况下,它甚至默认打开。但是,它不允许您根据内容应用抽样。它只允许您指定对哪种遥测进行采样。
我认为最适合您的是使用 ITelemetryProcessor 实现。参见 the docs
所有发送到 AI 的遥测,无论是自动收集还是手动发送到 AI,都会通过过滤器,并且根据正在传递的遥测项目的内容,您可以选择完全删除它。
请注意,它在与 .Net 日志记录机制不同的级别上运行。此方法仅适用于过滤发送到 AI 的数据,因此如果您已将记录器设置为将日志发送到不同的接收器,则不会受到影响。
示例实现(来自文档):
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
public class SuccessfulDependencyFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
// next will point to the next TelemetryProcessor in the chain.
public SuccessfulDependencyFilter(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry item)
{
// To filter out an item, return without calling the next processor.
if (!OKtoSend(item)) { return; }
this.Next.Process(item);
}
// Example: replace with your own criteria.
private bool OKtoSend (ITelemetry item)
{
var dependency = item as DependencyTelemetry;
if (dependency == null) return true;
return dependency.Success != true;
}
}
我正在寻找一种方法来减少发送到 Application Insights 的记录语句的数量。
虽然 .NET 日志记录允许使用 LogLevels
and categories, there's nothing that would allow removing log entries based on the logged content. Filter function 过滤掉日志,但只能对提供程序名称、类别和 LogLevel
进行操作。
这可能吗?
您可以使用多种方法。首先,Application Insights (AI) 允许您应用sampling。在大多数情况下,它甚至默认打开。但是,它不允许您根据内容应用抽样。它只允许您指定对哪种遥测进行采样。
我认为最适合您的是使用 ITelemetryProcessor 实现。参见 the docs
所有发送到 AI 的遥测,无论是自动收集还是手动发送到 AI,都会通过过滤器,并且根据正在传递的遥测项目的内容,您可以选择完全删除它。
请注意,它在与 .Net 日志记录机制不同的级别上运行。此方法仅适用于过滤发送到 AI 的数据,因此如果您已将记录器设置为将日志发送到不同的接收器,则不会受到影响。
示例实现(来自文档):
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
public class SuccessfulDependencyFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
// next will point to the next TelemetryProcessor in the chain.
public SuccessfulDependencyFilter(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry item)
{
// To filter out an item, return without calling the next processor.
if (!OKtoSend(item)) { return; }
this.Next.Process(item);
}
// Example: replace with your own criteria.
private bool OKtoSend (ITelemetry item)
{
var dependency = item as DependencyTelemetry;
if (dependency == null) return true;
return dependency.Success != true;
}
}