Skip to content

Commit 87c457a

Browse files
authored
Fixes getLoggerByFunc for computation expressions (#26)
1 parent ab10c32 commit 87c457a

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/FsLibLog/FsLibLog.fs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ module Providers =
10141014

10151015
#endif
10161016

1017+
10171018
module LogProvider =
10181019
open System
10191020
open Types
@@ -1184,22 +1185,6 @@ module LogProvider =
11841185
let getLoggerByQuotation (quotation: Quotations.Expr) =
11851186
getModuleType quotation |> getLoggerByType
11861187

1187-
/// **Description**
1188-
///
1189-
/// Creates a logger based on `Reflection.MethodBase.GetCurrentMethod` call. This is only useful for calls within functions. This does not protect against inlined functions.
1190-
///
1191-
/// **Output Type**
1192-
/// * `ILog`
1193-
///
1194-
/// **Exceptions**
1195-
///
1196-
let inline getLoggerByFunc () =
1197-
let mi = Reflection.MethodBase.GetCurrentMethod()
1198-
1199-
sprintf "%s.%s" mi.DeclaringType.FullName mi.Name
1200-
|> getLoggerByName
1201-
1202-
12031188
/// **Description**
12041189
///
12051190
/// Creates a logger. It's name is based on the current StackFrame. This will attempt to retrieve any loggers set with `setLoggerProvider`. It will fallback to a known list of providers.
@@ -1211,4 +1196,21 @@ module LogProvider =
12111196
let getCurrentLogger () =
12121197
let stackFrame = StackFrame(2, false)
12131198
getLoggerByType (stackFrame.GetMethod().DeclaringType)
1199+
1200+
1201+
type LogProvider =
1202+
/// <summary>
1203+
/// Creates a logger based on `Reflection.MethodBase.GetCurrentMethod().FullName` and `CallerMemberName`. This is only useful for calls within functions. Results may vary on lambda and inlined functions.
1204+
/// </summary>
1205+
/// <param name="memberName">Do not pass anything to this parameter to get `CallerMemberName` to work.</param>
1206+
/// <returns></returns>
1207+
static member inline getLoggerByFunc([<System.Runtime.CompilerServices.CallerMemberName>] ?memberName: string) =
1208+
let mi = System.Reflection.MethodBase.GetCurrentMethod()
1209+
// When we're in a CE we get something like `WebBackend.App+thingsToCall2@130`.
1210+
// CallerMemberName gets us the function that actually called it
1211+
// Splitting off + seems like the best option to get the Fully Qualified Path
1212+
let location = mi.DeclaringType.FullName.Split('+') |> Seq.tryHead |> Option.defaultValue ""
1213+
sprintf "%s.%s" location memberName.Value
1214+
|> LogProvider.getLoggerByName
1215+
12141216
#endif

tests/FsLibLog.Tests/Tests.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ let private getNewProvider () =
7070
let someFunction () =
7171
let logger = LogProvider.getLoggerByFunc()
7272
()
73+
let someAsyncFunction () = async {
74+
let logger = LogProvider.getLoggerByFunc()
75+
()
76+
}
7377
#endif
7478

7579
type Dog = {
@@ -88,6 +92,11 @@ let tests =
8892
let provider = getNewProvider()
8993
someFunction()
9094
Expect.equal provider.LoggerName "Tests.someFunction" ""
95+
testCaseAsync "LogProvider.getLoggerByFunc async CE" <| async {
96+
let provider = getNewProvider()
97+
do! someAsyncFunction()
98+
Expect.equal provider.LoggerName "Tests.someAsyncFunction" ""
99+
}
91100

92101
testCase "LogProvider.getLoggerByQuotation" <| fun _ ->
93102
Expect.equal SomeOtherModule.provider.LoggerName "Tests+SomeOtherModule" ""

0 commit comments

Comments
 (0)