-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd7c7ee
commit 4cbaa7f
Showing
6 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace FsLibLog.Providers.Expecto | ||
|
||
open System | ||
open Ionide.ProjInfo.Logging | ||
|
||
module EMsg = Expecto.Logging.Message | ||
type ELL = Expecto.Logging.LogLevel | ||
|
||
module internal Helpers = | ||
let addExnOpt exOpt msg = | ||
match exOpt with | ||
| None -> msg | ||
| Some ex -> | ||
msg | ||
|> EMsg.addExn ex | ||
|
||
let addValues (items: obj[]) msg = | ||
(msg, | ||
items | ||
|> Seq.mapi (fun i item -> i, item)) | ||
||> Seq.fold (fun msg (i, item) -> | ||
msg | ||
|> EMsg.setField (string i) item | ||
) | ||
|
||
let getLogLevel: LogLevel -> Expecto.Logging.LogLevel = | ||
function | ||
| LogLevel.Debug -> ELL.Debug | ||
| LogLevel.Error -> ELL.Error | ||
| LogLevel.Fatal -> ELL.Fatal | ||
| LogLevel.Info -> ELL.Info | ||
| LogLevel.Trace -> ELL.Verbose | ||
| LogLevel.Warn -> ELL.Warn | ||
| _ -> ELL.Warn | ||
|
||
open Helpers | ||
|
||
// Naive implementation, not that important, just need logging to actually work | ||
type ExpectoLogProvider() = | ||
let propertyStack = System.Collections.Generic.Stack<string * obj>() | ||
|
||
|
||
let addProp key value = | ||
propertyStack.Push(key, value) | ||
|
||
{ new IDisposable with | ||
member __.Dispose() = | ||
propertyStack.Pop() | ||
|> ignore | ||
} | ||
|
||
interface ILogProvider with | ||
override __.GetLogger(name: string) : Logger = | ||
let logger = Expecto.Logging.Log.create name | ||
|
||
fun ll mt exnOpt values -> | ||
match mt with | ||
| Some f -> | ||
let ll = getLogLevel ll | ||
|
||
logger.log | ||
ll | ||
(fun ll -> | ||
let message = f () | ||
let mutable msg = Expecto.Logging.Message.eventX message ll | ||
|
||
for (propertyName, propertyValue) in (Seq.rev propertyStack) do | ||
msg <- Expecto.Logging.Message.setField propertyName propertyValue msg | ||
|
||
match exnOpt with | ||
| None -> msg | ||
| Some ex -> | ||
msg | ||
|> Expecto.Logging.Message.addExn ex | ||
|> addValues values | ||
) | ||
|> Async.RunSynchronously | ||
|
||
true | ||
| None -> false | ||
|
||
override __.OpenMappedContext (key: string) (value: obj) (b: bool) = addProp key value | ||
override __.OpenNestedContext name = addProp "NDC" name | ||
|
||
module ExpectoLogProvider = | ||
let create () = ExpectoLogProvider() :> ILogProvider |
This file contains 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
This file contains 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
This file contains 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