Skip to content

[API Proposal]: Add overload to Debugger.Launch() that accepts a conditional expression #76840

@jeffkl

Description

@jeffkl

Background and motivation

I find myself occasionally wanting to make Debugger.Launch() conditional with syntax like:

if (string.Equals(Environment.GetEnvironmentVariable(DebugEnvironmentVariableName), bool.TrueString, StringComparison.OrdinalIgnoreCase))
{
    Debugger.Launch();
}

I think it would be a good addition if an overload to Debugger.Launch() accepted a bool that I could use as my conditional expression. Since Debugger is static, I can't add my own extension method.

API Proposal

namespace System.Diagnostics
{
    public static partial class Debugger
    {
        /// <summary>
        /// Launches and attaches a debugger to the process. If a debugger is already attached, nothing happens.
        /// </summary>
        /// <param name="condition"><c>true</c> to launch the debugger, otherwise <c>false</c>.</param>
        /// <returns><c>true</c> if the debugger was launched or a debugger is already attached, otherwise <c>false</c>.</returns>
        public static bool Launch(bool condition) => condition && Launch();
    }
}

API Usage

Debugger.Launch(condition: string.Equals(Environment.GetEnvironmentVariable(DebugEnvironmentVariableName), bool.TrueString, StringComparison.OrdinalIgnoreCase));

Alternative Designs

I can have my own static class like DebuggerExtensions:

public static class DebuggerExtensions
{
    /// <summary>
    /// Launches and attaches a debugger to the process. If a debugger is already attached, nothing happens.
    /// </summary>
    /// <param name="condition"><c>true</c> to launch the debugger, otherwise <c>false</c>.</param>
    /// <returns><c>true</c> if the debugger was launched or a debugger is already attached, otherwise <c>false</c>.</returns>
    public static bool Launch(bool condition) => condition && System.Diagnostics.Debugger.Launch();
}

But I find myself doing this in multiple projects and don't think its wroth shipping a standalone class library for this.

Risks

Since it would be an overload with a unique set of parameters, there should not be any breaking change in the public API. But if no one is going to use it other than me, it's probably not worth shipping. Also, there's potential frustration where code targeting .NET Framework wouldn't compile with this syntax, since only newer versions of .NET would have it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Diagnostics-coreclrenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions