Class ResultDebugExtensions
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Debug extension methods for inspecting Result values during development. These methods execute only in DEBUG builds and become no-ops in RELEASE builds (with zero overhead).
public static class ResultDebugExtensions
- Inheritance
-
ResultDebugExtensions
- Inherited Members
Remarks
The methods are always available to prevent compilation errors in RELEASE builds, but their implementation is conditionally compiled and becomes empty in RELEASE mode. The compiler will inline and optimize away these empty methods, resulting in zero runtime overhead.
These methods create dedicated Activities for debug operations, making them compatible with OpenTelemetry and modern observability systems like .NET Aspire, Application Insights, and Jaeger. Debug activities appear as child spans in distributed traces, making it easy to filter them out in production.
Methods
DebugDetailed<TValue>(Result<TValue>, string)
Writes detailed debug information about the Result to a new Activity, including error properties. This method only executes in DEBUG builds.
public static Result<TValue> DebugDetailed<TValue>(this Result<TValue> result, string message = "")
Parameters
resultResult<TValue>The Result to debug.
messagestringOptional message to prefix the debug output.
Returns
- Result<TValue>
The same Result that was passed in.
Type Parameters
TValueThe type of the value in the Result.
Examples
var result = EmailAddress.TryCreate(email)
.Combine(FirstName.TryCreate(firstName))
.DebugDetailed("After validation");
DebugOnFailure<TValue>(Result<TValue>, Action<Error>)
Executes a custom debug action if the result is a failure. This method only executes in DEBUG builds.
public static Result<TValue> DebugOnFailure<TValue>(this Result<TValue> result, Action<Error> action)
Parameters
resultResult<TValue>The Result to debug.
actionAction<Error>The action to execute with the error.
Returns
- Result<TValue>
The same Result that was passed in.
Type Parameters
TValueThe type of the value in the Result.
Examples
var result = GetUser(id)
.DebugOnFailure(error =>
{
var activity = Activity.Current;
activity?.SetTag("error.type", error.GetType().Name);
activity?.SetTag("error.code", error.Code);
activity?.SetTag("error.detail", error.Detail);
});
DebugOnSuccess<TValue>(Result<TValue>, Action<TValue>)
Executes a custom debug action if the result is a success. This method only executes in DEBUG builds.
public static Result<TValue> DebugOnSuccess<TValue>(this Result<TValue> result, Action<TValue> action)
Parameters
resultResult<TValue>The Result to debug.
actionAction<TValue>The action to execute with the success value.
Returns
- Result<TValue>
The same Result that was passed in.
Type Parameters
TValueThe type of the value in the Result.
Examples
var result = GetUser(id)
.DebugOnSuccess(user =>
{
var activity = Activity.Current;
activity?.SetTag("user.id", user.Id);
activity?.SetTag("user.email", user.Email);
activity?.SetTag("user.is_active", user.IsActive);
});
DebugWithStack<TValue>(Result<TValue>, string, bool)
Writes debug information with a stack trace for detailed debugging. Useful for understanding the call chain when debugging complex ROP operations. This method only executes in DEBUG builds.
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Debug-only code not included in Release builds")]
public static Result<TValue> DebugWithStack<TValue>(this Result<TValue> result, string message = "", bool includeStackTrace = true)
Parameters
resultResult<TValue>The Result to debug.
messagestringOptional message to prefix the debug output.
includeStackTraceboolWhether to include the stack trace. Default is true.
Returns
- Result<TValue>
The same Result that was passed in.
Type Parameters
TValueThe type of the value in the Result.
Debug<TValue>(Result<TValue>, string)
Writes debug information about the Result to a new Activity and returns the same Result. This method only executes in DEBUG builds.
public static Result<TValue> Debug<TValue>(this Result<TValue> result, string message = "")
Parameters
resultResult<TValue>The Result to debug.
messagestringOptional message to prefix the debug output.
Returns
- Result<TValue>
The same Result that was passed in.
Type Parameters
TValueThe type of the value in the Result.
Examples
var result = GetUser(id)
.Debug("After GetUser")
.Ensure(u => u.IsActive, Error.Validation("Inactive"))
.Debug("After Ensure")
.Bind(ProcessUser)
.Debug("After ProcessUser");
Remarks
Creates a new Activity span for the debug operation, making it visible as a child span in:
- .NET Aspire dashboard
- Application Insights
- Jaeger, Zipkin, and other OpenTelemetry-compatible tools