Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Created June 19, 2025 10:38
Show Gist options
  • Select an option

  • Save vkhorikov/355e964986a2ace23957442d8b40f0ac to your computer and use it in GitHub Desktop.

Select an option

Save vkhorikov/355e964986a2ace23957442d8b40f0ac to your computer and use it in GitHub Desktop.
TelemetryOperation wrapper
public class TelemetryOperation : IDisposable
{
private readonly IOperationHolder<RequestTelemetry> _operation;
private bool _disposed = false;
public TelemetryOperation(TelemetryClient telemetryClient, string operationName, Dictionary<string, string> properties)
{
var reqId = Guid.NewGuid().ToString("N");
_operation = telemetryClient.StartOperation<RequestTelemetry>(operationName, reqId);
// Set initial properties
foreach (var prop in properties)
{
_operation.Telemetry.Properties.Add(prop.Key, prop.Value);
}
_operation.Telemetry.Success = false;
}
public void MarkAsSuccess()
{
if (_disposed)
throw new ObjectDisposedException(nameof(TelemetryOperation));
_operation.Telemetry.Success = true;
}
public void MarkAsFailure(string error = null, Dictionary<string, string> properties = null)
{
if (_disposed)
throw new ObjectDisposedException(nameof(TelemetryOperation));
_operation.Telemetry.Success = false;
if (!string.IsNullOrEmpty(error))
{
_operation.Telemetry.Properties["error"] = error;
}
if (properties != null)
{
foreach (var prop in properties)
{
_operation.Telemetry.Properties[prop.Key] = prop.Value;
}
}
}
public void Dispose()
{
if (!_disposed)
{
_operation?.Dispose();
_disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment