-
Notifications
You must be signed in to change notification settings - Fork 38
/
Logging.fs
61 lines (52 loc) · 2.38 KB
/
Logging.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace CardManagement.Infrastructure
(*
All the logging is defined in here, except for configuration.
The idea is simple: you use functions `logify` and `logifyAsync` to wrap functions you want to log.
It will print start of function execution and execution result upon finishing.
*)
module Logging =
open CardManagement.Common
open Serilog
open Errors
open ErrorMessages
let private funcFinishedWithError funcName = sprintf "%s finished with error: %s" funcName
let logDataError funcName e = dataRelatedErrorMessage e |> funcFinishedWithError funcName |> Log.Warning
let logValidationError funcName e = validationMessage e |> funcFinishedWithError funcName |> Log.Information
let logOperationNotAllowed funcName e = operationNotAllowedMessage e |> funcFinishedWithError funcName |> Log.Warning
let logError funcName e =
match e with
| DataError e -> logDataError funcName e
| ValidationError e -> logValidationError funcName e
| OperationNotAllowed e -> logOperationNotAllowed funcName e
| Bug _ ->
let errorMessage = errorMessage e
Log.Error(errorMessage)
let private logResult funcName result =
match result with
| Ok ok -> sprintf "%s finished with result\n%A" funcName ok |> Log.Information
| Error e ->
match box e with
| :? DataRelatedError as er -> logDataError funcName er
| :? Error as er -> logError funcName er
| :? ValidationError as er -> logValidationError funcName er
| :? OperationNotAllowedError as er -> logOperationNotAllowed funcName er
| e -> sprintf "%A" e |> Log.Error
let logifyResult funcName func x =
sprintf "start %s with arg\n%A" funcName x |> Log.Information
let result = func x
logResult funcName result
result
let logifyResultAsync funcName funcAsync x =
async {
sprintf "start %s with arg\n%A" funcName x |> Log.Information
let! result = funcAsync x
logResult funcName result
return result
}
let logifyPlainAsync funcName funcAsync x =
async {
sprintf "start %s with arg\n%A" funcName x |> Log.Information
let! result = funcAsync x
sprintf "%s finished with result\n%A" funcName result |> Log.Information
return result
}