Last active
July 30, 2025 21:58
-
-
Save bent-rasmussen/00c8a7032a73e520a2836fa4ba5b8837 to your computer and use it in GitHub Desktop.
F# Trace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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