gpt4 book ai didi

c# - 更改遥测处理器中的遥测类型

转载 作者:行者123 更新时间:2023-12-02 06:28:22 31 4
gpt4 key购买 nike

我希望将某些异常作为警告而不是异常记录到应用程序洞察中。不幸的是,我无法更改引发异常的代码,因此我创建了一个 TelemetryProcessor ( based on this answer )。

但是,此代码创建的 TraceTelemetry 永远不会到达 Application Insights。我检查了这两个实时指标,并等待了几分钟,条目才显示在搜索中。

我的猜测是我缺少一些东西,比如正确的上下文,但我不确定要复制什么。 Context 属性是只读的,因此我不能简单地重用它。

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

public ExceptionTelemetryProcessor(ITelemetryProcessor next)
{
Next = next;
}

public void Process(ITelemetry item)
{
if (item is ExceptionTelemetry exceptionTelemetry
&& exceptionTelemetry.Exception is Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException bhre
&& bhre.StatusCode == 408)
{
// Track exception as warning instead
var traceTelemetry = new TraceTelemetry(exceptionTelemetry.Exception.Message, SeverityLevel.Warning);
Next.Process(traceTelemetry);
}
else
{
Next.Process(item);
}
}
}

最佳答案

如果您将原始异常遥测中的所有数据复制到跟踪遥测中并进一步传递 - 它将被记录(至关重要的是Context.InstrumentationKey时间戳):

    private ITelemetry ToTraceTelemetry(ExceptionTelemetry exceptionTelemetry)
{
var traceTelemetry = new TraceTelemetry(exceptionTelemetry.Message, SeverityLevel.Warning);
foreach (var telemetryProperty in exceptionTelemetry.Properties)
{
traceTelemetry.Properties.Add(telemetryProperty);
}

traceTelemetry.Timestamp = exceptionTelemetry.Timestamp;
traceTelemetry.Extension = exceptionTelemetry.Extension;
traceTelemetry.ProactiveSamplingDecision = exceptionTelemetry.ProactiveSamplingDecision;
traceTelemetry.Sequence = traceTelemetry.Sequence;

traceTelemetry.Context.InstrumentationKey = exceptionTelemetry.Context.InstrumentationKey;
traceTelemetry.Context.Flags = exceptionTelemetry.Context.Flags;

traceTelemetry.Context.Location.Ip = exceptionTelemetry.Context.Location.Ip;
traceTelemetry.Context.Cloud.RoleInstance = exceptionTelemetry.Context.Cloud.RoleInstance;
traceTelemetry.Context.Cloud.RoleName = exceptionTelemetry.Context.Cloud.RoleName;
traceTelemetry.Context.Component.Version = exceptionTelemetry.Context.Component.Version;
traceTelemetry.Context.Device.Id = exceptionTelemetry.Context.Device.Id;
traceTelemetry.Context.Device.Model = exceptionTelemetry.Context.Device.Model;
traceTelemetry.Context.Device.OemName = exceptionTelemetry.Context.Device.OemName;
traceTelemetry.Context.Device.OperatingSystem = exceptionTelemetry.Context.Device.OperatingSystem;
traceTelemetry.Context.Device.Type = exceptionTelemetry.Context.Device.Type;
traceTelemetry.Context.Operation.Id = exceptionTelemetry.Context.Operation.Id;
traceTelemetry.Context.Operation.CorrelationVector = exceptionTelemetry.Context.Operation.CorrelationVector;
traceTelemetry.Context.Operation.Name = exceptionTelemetry.Context.Operation.Name;
traceTelemetry.Context.Operation.ParentId = exceptionTelemetry.Context.Operation.ParentId;
traceTelemetry.Context.Operation.SyntheticSource = exceptionTelemetry.Context.Operation.SyntheticSource;
traceTelemetry.Context.Session.Id = exceptionTelemetry.Context.Session.Id;
traceTelemetry.Context.Session.IsFirst = exceptionTelemetry.Context.Session.IsFirst;
traceTelemetry.Context.User.Id = exceptionTelemetry.Context.User.Id;
traceTelemetry.Context.User.AccountId = exceptionTelemetry.Context.User.AccountId;
traceTelemetry.Context.User.AuthenticatedUserId = exceptionTelemetry.Context.User.AuthenticatedUserId;
traceTelemetry.Context.User.UserAgent = exceptionTelemetry.Context.User.UserAgent;

return traceTelemetry;
}

然后调用它:

public void Process(ITelemetry item)
{
if (item is ExceptionTelemetry exceptionTelemetry
&& other_condition)
{
item = ToTraceTelemetry(exceptionTelemetry);
}

Next.Process(item);
}

关于c# - 更改遥测处理器中的遥测类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709519/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com