# Diagnostics\_ConditionalAttribute

***

**1. Logging based on environment variables:**

```csharp
[Conditional("DEBUG")]
public void LogDebug(string message) => Debug.WriteLine(message);
```

**2. Logging based on configuration settings:**

```csharp
[Conditional("LOGGING_ENABLED")]
public void LogInfo(string message) => Console.WriteLine(message);
```

**3. Logging based on build type:**

```csharp
#if DEBUG
[Conditional("DEBUG")]
public void LogTrace(string message) => Console.WriteLine(message);
#endif
```

**4. Logging based on compilation flag:**

```csharp
[Conditional("TRACE")]
public void LogTrace(string message) => Console.WriteLine(message);
```

**5. Logging based on whether a breakpoint is hit:**

```csharp
[Conditional("DEBUG")]
[Conditional("BREAKPOINT")]
public void LogDebug(string message) => Debugger.Log(0, null, message);
```

**6. Logging based on whether a condition is true:**

```csharp
[Conditional("PRODUCTION")]
public void LogProd(string message) => Console.WriteLine(message);
```

**7. Logging based on whether a feature is enabled:**

```csharp
[Conditional("FEATURE_X")]
public void LogFeatureX(string message) => Console.WriteLine(message);
```

**8. Logging based on whether a diagnostics source is enabled:**

```csharp
[Conditional("CUSTOM_DIAGNOSTICS")]
public void LogCustom(string message) => Log.Information(message);
```

**9. Logging based on a custom attribute:**

```csharp
[Conditional("MyConditional")]
public void LogCustom(string message) => Console.WriteLine(message);
```

**10. Logging based on a combination of conditions:**

```csharp
[Conditional("DEBUG")]
[Conditional("PRODUCTION")]
public void LogBoth(string message) => Console.WriteLine(message);
```

**11. Logging based on a dynamic value:**

```csharp
public bool EnableLogging { get; set; }

[Conditional("EnableLogging")]
public void LogDynamic(string message) => Console.WriteLine(message);
```

**12. Logging based on whether a method is called:**

```csharp
[Conditional("MethodExecuted")]
public void LogMethod(string message) => Console.WriteLine(message);
```

**13. Logging based on whether a class is instantiated:**

```csharp
[Conditional("ClassCreated")]
public class MyClass
{
    public MyClass() => Log.Information("Class created.");
}
```

**14. Logging based on whether a property is accessed:**

```csharp
[Conditional("PropertyAccessed")]
public int MyProperty { get; set; }
```

**15. Logging based on whether a field is assigned:**

```csharp
[Conditional("FieldAssigned")]
public int MyField;
```

**16. Logging based on whether an event is raised:**

```csharp
[Conditional("EventRaised")]
public event EventHandler MyEvent;
```

**17. Logging based on whether an exception is thrown:**

```csharp
[Conditional("ExceptionThrown")]
public void ThrowException() => throw new Exception();
```

**18. Logging based on whether a value is null:**

```csharp
[Conditional("ValueIsNull")]
public string? MyString;
```

**19. Logging based on whether a value is zero:**

```csharp
[Conditional("ValueIsZero")]
public int MyInt;
```

**20. Logging based on whether a collection is empty:**

```csharp
[Conditional("CollectionIsEmpty")]
public List<int> MyList;
```

**21. Logging based on whether a dictionary contains a key:**

```csharp
[Conditional("DictionaryContainsValue")]
public Dictionary<int, string> MyDictionary;
```

**22. Logging based on whether a condition is met within a method:**

```csharp
public void MyMethod()
{
    if (condition)
    {
        [Conditional("ConditionMet")]
        public void LogConditionMet() => Console.WriteLine("Condition met.");
    }
}
```

**23. Logging based on whether a variable is assigned within a method:**

```csharp
public void MyMethod()
{
    [Conditional("VariableAssigned")]
    public int MyVariable;
    MyVariable = 10;
}
```

**24. Logging based on whether a property is accessed within a method:**

```csharp
public void MyMethod()
{
    [Conditional("PropertyAccessed")]
    public int MyProperty { get; set; }
    MyProperty++;
}
```

**25. Logging based on whether an event is raised within a method:**

```csharp
public void MyMethod()
{
    [Conditional("EventRaised")]
    public event EventHandler MyEvent;
    MyEvent(this, EventArgs.Empty);
}
```

**26. Logging based on whether an exception is thrown within a method:**

```csharp
public void MyMethod()
{
    [Conditional("ExceptionThrown")]
    public void ThrowException() => throw new Exception();
    ThrowException();
}
```

**27. Logging based on whether a value is null within a method:**

```csharp
public void MyMethod()
{
    [Conditional("ValueIsNull")]
    public string? MyString;
    MyString = null;
}
```

**28. Logging based on whether a value is zero within a method:**

```csharp
public void MyMethod()
{
    [Conditional("ValueIsZero")]
    public int MyInt;
    MyInt = 0;
}
```

**29. Logging based on whether a collection is empty within a method:**

```csharp
public void MyMethod()
{
    [Conditional("CollectionIsEmpty")]
    public List<int> MyList;
    MyList.Clear();
}
```

**30. Logging based on whether a dictionary contains a key within a method:**

```csharp
public void MyMethod()
{
    [Conditional("DictionaryContainsValue")]
    public Dictionary<int, string> MyDictionary;
    MyDictionary.Remove(1);
}
```

**31. Logging based on whether a condition is met within a constructor:**

```csharp
public MyClass()
{
    if (condition)
    {
        [Conditional("ConditionMet")]
        public void LogConditionMet() => Console.WriteLine("Condition met.");
    }
}
```

**32. Logging based on whether a variable is assigned within a constructor:**

```csharp
public MyClass()
{
    [Conditional("VariableAssigned")]
    public int MyVariable;
    MyVariable = 10;
}
```

**33. Logging based on whether a property is accessed within a constructor:**

```csharp
public MyClass()
{
    [Conditional("PropertyAccessed")]
    public int MyProperty { get; set; }
    MyProperty++;
}
```

**34. Logging based on whether an event is raised within a constructor:**

```csharp
public MyClass()
{
    [Conditional("EventRaised")]
    public event EventHandler MyEvent;
    MyEvent(this, EventArgs.Empty);
}
```

**35. Logging based on whether an exception is thrown within a constructor:**

```csharp
public MyClass()
{
    [Conditional("ExceptionThrown")]
    public void ThrowException() => throw new Exception();
    ThrowException();
}
```

**36. Logging based on whether a value is null within a constructor:**

```csharp
public MyClass()
{
    [Conditional("ValueIsNull")]
    public string? MyString;
    MyString = null;
}
```

**37. Logging based on whether a value is zero within a constructor:**

```csharp
public MyClass()
{
    [Conditional("ValueIsZero")]
    public int MyInt;
    MyInt = 0;
}
```

**38. Logging based on whether a collection is empty within a constructor:**

```csharp
public MyClass()
{
    [Conditional("CollectionIsEmpty")]
    public List<int> MyList;
    MyList.Clear();
}
```

**39. Logging based on whether a dictionary contains a key within a constructor:**

```csharp
public MyClass()
{
    [Conditional("DictionaryContainsValue")]
    public Dictionary<int, string> MyDictionary;
    MyDictionary.Remove(1);
}
```

**40. Logging based on whether a static variable is assigned:**

```csharp
[Conditional("StaticVariableAssigned")]
public static int MyStaticVariable;
```

**41. Logging based on whether a static property is accessed:**

```csharp
[Conditional("StaticPropertyAccessed")]
public static int MyStaticProperty { get; set; }
```

**42. Logging based on whether a static event is raised:**

```csharp
[Conditional("StaticEventRaised")]
public static event EventHandler MyStaticEvent;
```

**43. Logging based on whether a static exception is thrown:**

```csharp
[Conditional("StaticExceptionThrown")]
public static void ThrowStaticException() => throw new Exception();
```

**44. Logging based on whether a static value is null:**

```csharp
[Conditional("StaticValueIsNull")]
public static string? MyStaticString;
```

**45. Logging based on whether a static value is zero:**

```csharp
[Conditional("StaticValueIsZero")]
public static int MyStaticInt;
```

**46. Logging based on whether a static collection is empty:**

```csharp
[Conditional("StaticCollectionIsEmpty")]
public static List<int> MyStaticList;
```

**47. Logging based on whether a static dictionary contains a key:**

```csharp
[Conditional("StaticDictionaryContainsValue")]
public static Dictionary<int, string> MyStaticDictionary;
```

**48. Logging based on whether a value is assigned within a property:**

```csharp
public int MyProperty
{
    [Conditional("PropertyAssigned")]
    get { return _myProperty; }
    set { _myProperty = value; }
}
```

**49. Logging based on whether a method is overloaded:**

```csharp
public void MyMethod(int a)
{
    [Conditional("MethodOverloaded")]
    public void MyMethod(int a, int b)
    {
        Console.WriteLine("Method overloaded.");
    }
}
```

**50. Logging based on whether a class implements an interface:**

```csharp
public class MyClass : IMyInterface
{
    [Conditional("InterfaceImplemented")]
    public void MyInterfaceMethod()
    {
        Console.WriteLine("Interface implemented.");
    }
}
```
