Skip to content

Commit ddf5bd3

Browse files
committed
#546 Extend LoggerExt to support Info, Warn, Error, Fatal log level (#547)
1 parent 5fb5e6c commit ddf5bd3

File tree

4 files changed

+72
-172
lines changed

4 files changed

+72
-172
lines changed

src/NBomber/Extensions/Internal.fs

+1-58
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ namespace NBomber.Extensions
22

33
open System
44
open System.Collections.Generic
5-
open System.Diagnostics
65
open System.Text
7-
open System.Threading.Tasks
86

97
open FSharp.Json
108
open FsToolkit.ErrorHandling
@@ -24,62 +22,7 @@ module internal Internal =
2422
let inline round (digits: int) (value: float) = Math.Round(value, digits)
2523

2624
let inline roundDuration (duration: TimeSpan) =
27-
TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds)
28-
29-
type Operation =
30-
31-
static member retry (retryCount: int, getResult: unit -> Task<Result<'T,'E>>) = backgroundTask {
32-
let mutable counter = 1
33-
let mutable result = Unchecked.defaultof<_>
34-
let! r = getResult()
35-
result <- r
36-
37-
while Result.isError result && counter < retryCount do
38-
counter <- counter + 1
39-
let! r = getResult()
40-
result <- r
41-
42-
return result
43-
}
44-
45-
static member retryDuring (duration: TimeSpan, getResult: unit -> Task<Result<'T,'E>>) =
46-
Operation.retryDuring(duration, None, getResult)
47-
48-
static member retryDuring (duration: TimeSpan,
49-
retryDelay: TimeSpan,
50-
getResult: unit -> Task<Result<'T,'E>>,
51-
?shouldRetry: Result<'T,'E> -> bool) =
52-
53-
Operation.retryDuring(duration, Some retryDelay, getResult, ?shouldRetry = shouldRetry)
54-
55-
static member private retryDuring (duration: TimeSpan,
56-
retryDelay: TimeSpan option,
57-
getResult: unit -> Task<Result<'T,'E>>,
58-
?shouldRetry: Result<'T,'E> -> bool) = backgroundTask {
59-
let shouldContinue =
60-
shouldRetry
61-
|> Option.defaultValue(fun _ -> true)
62-
63-
let stopwatch = Stopwatch()
64-
stopwatch.Start()
65-
66-
let mutable result = Unchecked.defaultof<_>
67-
let! r = getResult()
68-
result <- r
69-
70-
while Result.isError result
71-
&& shouldContinue result
72-
&& stopwatch.Elapsed < duration do
73-
74-
if retryDelay.IsSome then
75-
do! Task.Delay retryDelay.Value
76-
77-
let! r = getResult()
78-
result <- r
79-
80-
stopwatch.Stop()
81-
return result
82-
}
25+
TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds)
8326

8427
module JsonExt =
8528

src/NBomber/Infra/Dependency.fs

+71-12
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,100 @@ type internal IGlobalDependency =
2828

2929
[<Extension>]
3030
type internal LogExt =
31-
31+
3232
[<Extension>]
3333
static member LogInfo(dep: IGlobalDependency, msg) =
3434
dep.ConsoleLogger.Information msg
35-
dep.Logger.Information msg
36-
35+
dep.Logger.Information msg
36+
3737
[<Extension>]
3838
static member LogInfo(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
3939
dep.ConsoleLogger.Information(msg, propertyValues)
4040
dep.Logger.Information(msg, propertyValues)
41-
41+
42+
[<Extension>]
43+
static member LogWarn(dep: IGlobalDependency, msg) =
44+
dep.ConsoleLogger.Warning msg
45+
dep.Logger.Warning msg
46+
4247
[<Extension>]
4348
static member LogWarn(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
4449
dep.ConsoleLogger.Warning(msg, propertyValues)
45-
dep.Logger.Warning(msg, propertyValues)
46-
50+
dep.Logger.Warning(msg, propertyValues)
51+
52+
[<Extension>]
53+
static member LogWarn(dep: IGlobalDependency, ex: exn, msg) =
54+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
55+
dep.ConsoleLogger.Warning(ex, msg)
56+
else
57+
dep.ConsoleLogger.Warning(msg)
58+
59+
dep.Logger.Warning(ex, msg)
60+
4761
[<Extension>]
4862
static member LogWarn(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
49-
dep.ConsoleLogger.Warning(ex, msg, propertyValues)
63+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
64+
dep.ConsoleLogger.Warning(ex, msg, propertyValues)
65+
else
66+
dep.ConsoleLogger.Warning(msg, propertyValues)
67+
5068
dep.Logger.Warning(ex, msg, propertyValues)
51-
69+
5270
[<Extension>]
5371
static member LogError(dep: IGlobalDependency, msg) =
5472
dep.ConsoleLogger.Error msg
5573
dep.Logger.Error msg
56-
74+
75+
[<Extension>]
76+
static member LogError(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
77+
dep.ConsoleLogger.Error(msg, propertyValues)
78+
dep.Logger.Error(msg, propertyValues)
79+
5780
[<Extension>]
5881
static member LogError(dep: IGlobalDependency, ex: exn, msg) =
59-
dep.ConsoleLogger.Error(ex, msg)
82+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
83+
dep.ConsoleLogger.Error(ex, msg)
84+
else
85+
dep.ConsoleLogger.Error msg
86+
6087
dep.Logger.Error(ex, msg)
61-
88+
6289
[<Extension>]
6390
static member LogError(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
64-
dep.ConsoleLogger.Error(ex, msg, propertyValues)
91+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
92+
dep.ConsoleLogger.Error(ex, msg, propertyValues)
93+
else
94+
dep.ConsoleLogger.Error(msg, propertyValues)
95+
6596
dep.Logger.Error(ex, msg, propertyValues)
97+
98+
[<Extension>]
99+
static member LogFatal(dep: IGlobalDependency, msg) =
100+
dep.ConsoleLogger.Fatal msg
101+
dep.Logger.Fatal msg
102+
103+
[<Extension>]
104+
static member LogFatal(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
105+
dep.ConsoleLogger.Fatal(msg, propertyValues)
106+
dep.Logger.Fatal(msg, propertyValues)
107+
108+
[<Extension>]
109+
static member LogFatal(dep: IGlobalDependency, ex: exn, msg) =
110+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
111+
dep.ConsoleLogger.Fatal(ex, msg)
112+
else
113+
dep.ConsoleLogger.Fatal msg
114+
115+
dep.Logger.Fatal(ex, msg)
116+
117+
[<Extension>]
118+
static member LogFatal(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
119+
if dep.Logger.IsEnabled LogEventLevel.Verbose then
120+
dep.ConsoleLogger.Fatal(ex, msg, propertyValues)
121+
else
122+
dep.ConsoleLogger.Fatal(msg, propertyValues)
123+
124+
dep.Logger.Fatal(ex, msg, propertyValues)
66125

67126
module internal Logger =
68127

tests/NBomber.IntegrationTests/ExtensionsTests.fs

-101
This file was deleted.

tests/NBomber.IntegrationTests/NBomber.IntegrationTests.fsproj

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<ItemGroup>
88
<Compile Include="AssemblyInfo.fs" />
99
<Compile Include="TestHelper.fs" />
10-
<Compile Include="ExtensionsTests.fs" />
1110
<Content Include="Configuration\scenario_init_only_config.json">
1211
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1312
</Content>

0 commit comments

Comments
 (0)