Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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#_Azure Application Insights - Fatal编程技术网

C# 具有无效检测键的应用程序洞察-如何在不引发错误时进行处理

C# 具有无效检测键的应用程序洞察-如何在不引发错误时进行处理,c#,azure-application-insights,C#,Azure Application Insights,当跟踪消息到应用程序洞察时使用无效的检测密钥时,是否有方法捕获错误 我正在以编程方式指定一个如下所示的插入键,但没有引发异常。我正在尝试构建一个日志WebApi,它将根据消息是否成功记录到Application Insights来返回成功或失败? TelemetryConfiguration config = TelemetryConfiguration.CreateDefault(); config.InstrumentationKey = "ABC"; client.TrackTrace("

当跟踪消息到应用程序洞察时使用无效的检测密钥时,是否有方法捕获错误

我正在以编程方式指定一个如下所示的插入键,但没有引发异常。
我正在尝试构建一个日志WebApi,它将根据消息是否成功记录到Application Insights来返回成功或失败?

TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
config.InstrumentationKey = "ABC";
client.TrackTrace("Test"),SeverityLevel.Information);

您应该实现自己的通道来实现,并根据需要处理异常

下面是一个天真的例子:

public class SynchronousTelemetryChannel : ITelemetryChannel
{
    private const string ContentType = "application/x-json-stream";
    private readonly List<ITelemetry> _items;
    private object _lock = new object();
    public bool? DeveloperMode { get; set; }
    public string EndpointAddress { get; set; }

    public SynchronousTelemetryChannel()
    {
        _items = new List<ITelemetry>();
        EndpointAddress = "https://dc.services.visualstudio.com/v2/track";
    }

    public void Send(ITelemetry item)
    {
        lock (_lock)
        {
            _items.Add(item);
        }
    }

    public void Flush()
    {
        lock (_lock)
        {
            try
            {
                byte[] data = JsonSerializer.Serialize(_items);
                new Transmission(new Uri(EndpointAddress), data, ContentType, JsonSerializer.CompressionType).SendAsync().Wait();
                _items.Clear();
            }
            catch (Exception e)
            {
                // Do whatever you want.
            }
        }
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}
        TelemetryConfiguration.Active.TelemetryChannel = new SynchronousTelemetryChannel();