Skip to content

Instantly share code, notes, and snippets.

@bent-rasmussen
Last active July 30, 2025 21:58
Show Gist options
  • Select an option

  • Save bent-rasmussen/00c8a7032a73e520a2836fa4ba5b8837 to your computer and use it in GitHub Desktop.

Select an option

Save bent-rasmussen/00c8a7032a73e520a2836fa4ba5b8837 to your computer and use it in GitHub Desktop.
F# Trace
open System
open System.Diagnostics
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
let activitySource = new ActivitySource("Test")
[<IsReadOnly; Struct>]
type Trace<'T>(activitySource: ActivitySource) =
static let typeName = typeof<'T>.Name
member _.Test([<CallerMemberName; Optional; DefaultParameterValue("_")>] callerName: string) =
printfn $"{typeName}.{callerName}"
member _.Span([<CallerMemberName; Optional; DefaultParameterValue("_")>] callerName: string) =
if activitySource.HasListeners() then
activitySource.StartActivity($"{typeName}.{callerName}")
else
null
[<AutoOpen>]
module Tracing =
let trace<'T>() = Trace<'T>(activitySource)
type Foo() =
static let trace = trace<Foo>()
member this.Bar() =
use _ = trace.Span()
trace.Test()
Foo().Bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment