microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Core/Diagnostics/ActivityExtensions.cs
35lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Diagnostics; |
| 5 | |
| 6 | namespace Microsoft.Teams.Core.Diagnostics; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Helpers for setting standardized tags and recording exceptions on <see cref="Activity"/> instances |
| 10 | /// emitted by the Teams SDK's bot pipeline. |
| 11 | /// </summary> |
| 12 | public static class ActivityExtensions |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// Records an exception on the span: sets status to <see cref="ActivityStatusCode.Error"/> and |
| 16 | /// adds an <c>exception</c> event with type/message/stacktrace tags. Mirrors the shape that |
| 17 | /// <see cref="Activity.AddException"/> uses on .NET 9+ but works on net8.0 as well. |
| 18 | /// </summary> |
| 19 | public static void RecordException(this Activity? activity, Exception exception) |
| 20 | { |
| 21 | if (activity is null || exception is null) |
| 22 | { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | ActivityTagsCollection tags = new() |
| 27 | { |
| 28 | { "exception.type", exception.GetType().FullName }, |
| 29 | { "exception.message", exception.Message }, |
| 30 | { "exception.stacktrace", exception.ToString() }, |
| 31 | }; |
| 32 | activity.AddEvent(new ActivityEvent("exception", tags: tags)); |
| 33 | activity.SetStatus(ActivityStatusCode.Error, exception.Message); |
| 34 | } |
| 35 | } |
| 36 | |