Skip to content

Commit

Permalink
#546 Extend LoggerExt to support Info, Warn, Error, Fatal log level (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Feb 28, 2023
1 parent 5fb5e6c commit 128b19b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
59 changes: 1 addition & 58 deletions src/NBomber/Extensions/Internal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ namespace NBomber.Extensions

open System
open System.Collections.Generic
open System.Diagnostics
open System.Text
open System.Threading.Tasks

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

let inline roundDuration (duration: TimeSpan) =
TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds)

type Operation =

static member retry (retryCount: int, getResult: unit -> Task<Result<'T,'E>>) = backgroundTask {
let mutable counter = 1
let mutable result = Unchecked.defaultof<_>
let! r = getResult()
result <- r

while Result.isError result && counter < retryCount do
counter <- counter + 1
let! r = getResult()
result <- r

return result
}

static member retryDuring (duration: TimeSpan, getResult: unit -> Task<Result<'T,'E>>) =
Operation.retryDuring(duration, None, getResult)

static member retryDuring (duration: TimeSpan,
retryDelay: TimeSpan,
getResult: unit -> Task<Result<'T,'E>>,
?shouldRetry: Result<'T,'E> -> bool) =

Operation.retryDuring(duration, Some retryDelay, getResult, ?shouldRetry = shouldRetry)

static member private retryDuring (duration: TimeSpan,
retryDelay: TimeSpan option,
getResult: unit -> Task<Result<'T,'E>>,
?shouldRetry: Result<'T,'E> -> bool) = backgroundTask {
let shouldContinue =
shouldRetry
|> Option.defaultValue(fun _ -> true)

let stopwatch = Stopwatch()
stopwatch.Start()

let mutable result = Unchecked.defaultof<_>
let! r = getResult()
result <- r

while Result.isError result
&& shouldContinue result
&& stopwatch.Elapsed < duration do

if retryDelay.IsSome then
do! Task.Delay retryDelay.Value

let! r = getResult()
result <- r

stopwatch.Stop()
return result
}
TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds)

module JsonExt =

Expand Down
83 changes: 71 additions & 12 deletions src/NBomber/Infra/Dependency.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,100 @@ type internal IGlobalDependency =

[<Extension>]
type internal LogExt =

[<Extension>]
static member LogInfo(dep: IGlobalDependency, msg) =
dep.ConsoleLogger.Information msg
dep.Logger.Information msg

dep.Logger.Information msg
[<Extension>]
static member LogInfo(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Information(msg, propertyValues)
dep.Logger.Information(msg, propertyValues)


[<Extension>]
static member LogWarn(dep: IGlobalDependency, msg) =
dep.ConsoleLogger.Warning msg
dep.Logger.Warning msg

[<Extension>]
static member LogWarn(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Warning(msg, propertyValues)
dep.Logger.Warning(msg, propertyValues)

dep.Logger.Warning(msg, propertyValues)

[<Extension>]
static member LogWarn(dep: IGlobalDependency, ex: exn, msg) =
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Warning(ex, msg)
else
dep.ConsoleLogger.Warning(msg)

dep.Logger.Warning(ex, msg)

[<Extension>]
static member LogWarn(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Warning(ex, msg, propertyValues)
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Warning(ex, msg, propertyValues)
else
dep.ConsoleLogger.Warning(msg, propertyValues)

dep.Logger.Warning(ex, msg, propertyValues)

[<Extension>]
static member LogError(dep: IGlobalDependency, msg) =
dep.ConsoleLogger.Error msg
dep.Logger.Error msg


[<Extension>]
static member LogError(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Error(msg, propertyValues)
dep.Logger.Error(msg, propertyValues)

[<Extension>]
static member LogError(dep: IGlobalDependency, ex: exn, msg) =
dep.ConsoleLogger.Error(ex, msg)
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Error(ex, msg)
else
dep.ConsoleLogger.Error msg

dep.Logger.Error(ex, msg)

[<Extension>]
static member LogError(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Error(ex, msg, propertyValues)
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Error(ex, msg, propertyValues)
else
dep.ConsoleLogger.Error(msg, propertyValues)

dep.Logger.Error(ex, msg, propertyValues)

[<Extension>]
static member LogFatal(dep: IGlobalDependency, msg) =
dep.ConsoleLogger.Fatal msg
dep.Logger.Fatal msg

[<Extension>]
static member LogFatal(dep: IGlobalDependency, msg, [<ParamArray>]propertyValues: obj[]) =
dep.ConsoleLogger.Fatal(msg, propertyValues)
dep.Logger.Fatal(msg, propertyValues)

[<Extension>]
static member LogFatal(dep: IGlobalDependency, ex: exn, msg) =
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Fatal(ex, msg)
else
dep.ConsoleLogger.Fatal msg

dep.Logger.Fatal(ex, msg)

[<Extension>]
static member LogFatal(dep: IGlobalDependency, ex: exn, msg, [<ParamArray>]propertyValues: obj[]) =
if dep.Logger.IsEnabled LogEventLevel.Verbose then
dep.ConsoleLogger.Fatal(ex, msg, propertyValues)
else
dep.ConsoleLogger.Fatal(msg, propertyValues)

dep.Logger.Fatal(ex, msg, propertyValues)

module internal Logger =

Expand Down

0 comments on commit 128b19b

Please sign in to comment.