Table of Contents

Class VhInvoker

Namespace
VpnHood.Net.Toolkit.Utils
Assembly
VpnHood.Net.Toolkit.dll

Provides the means to run what may fail without letting the failure out: it is logged as Could not {actionName}. at the level the caller gives.

public static class VhInvoker
Inheritance
VhInvoker
Inherited Members

Remarks

Give Debug for best-effort work, Warning for what a failure leaves behind, and Error for a bug, such as a failed handler. Every exception is caught; a cancellation is logged at Debug with its own wording. A failure of the log itself is not caught.

Methods

TryInvoke(string, LogLevel, Action)

Runs an action, and logs a failure instead of throwing it.

public static void TryInvoke(string actionName, LogLevel failureLevel, Action action)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

action Action

The action.

TryInvokeAsync(string, LogLevel, Func<Task>)

Runs an asynchronous action, and logs a failure instead of throwing it.

public static Task TryInvokeAsync(string actionName, LogLevel failureLevel, Func<Task> task)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

task Func<Task>

Starts the action.

Returns

Task

A task that completes when the action does, failed or not.

TryInvokeAsync(string, LogLevel, Func<ValueTask>)

Runs an asynchronous action, and logs a failure instead of throwing it.

public static Task TryInvokeAsync(string actionName, LogLevel failureLevel, Func<ValueTask> task)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

task Func<ValueTask>

Starts the action.

Returns

Task

A task that completes when the action does, failed or not.

TryInvokeAsync<T>(string, LogLevel, Func<Task<T>>, T?)

Runs an asynchronous function, and logs a failure instead of throwing it.

public static Task<T?> TryInvokeAsync<T>(string actionName, LogLevel failureLevel, Func<Task<T>> task, T? defaultValue = default)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

task Func<Task<T>>

Starts the function.

defaultValue T

The result after a failure.

Returns

Task<T>

A task whose result is the function's, or defaultValue after a failure.

Type Parameters

T

The type of the result.

TryInvokeAsync<T>(string, LogLevel, Func<ValueTask<T>>, T?)

Runs an asynchronous function, and logs a failure instead of throwing it.

public static ValueTask<T?> TryInvokeAsync<T>(string actionName, LogLevel failureLevel, Func<ValueTask<T>> task, T? defaultValue = default)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

task Func<ValueTask<T>>

Starts the function.

defaultValue T

The result after a failure.

Returns

ValueTask<T>

A task whose result is the function's, or defaultValue after a failure.

Type Parameters

T

The type of the result.

TryInvoke<T>(string, LogLevel, Func<T>, T?)

Runs a function, and logs a failure instead of throwing it.

public static T? TryInvoke<T>(string actionName, LogLevel failureLevel, Func<T> func, T? defaultValue = default)

Parameters

actionName string

What is being done, for the log line Could not {actionName}.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

func Func<T>

The function.

defaultValue T

The result after a failure.

Returns

T

The function's result, or defaultValue after a failure.

Type Parameters

T

The type of the result.