1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Diagnostics; 11 12 namespace System.Data.SqlClient.Tests 13 { 14 public sealed class FakeDiagnosticListenerObserver : IObserver<DiagnosticListener> 15 { 16 private class FakeDiagnosticSourceWriteObserver : IObserver<KeyValuePair<string, object>> 17 { 18 private readonly Action<KeyValuePair<string, object>> _writeCallback; 19 FakeDiagnosticSourceWriteObserver(Action<KeyValuePair<string, object>> writeCallback)20 public FakeDiagnosticSourceWriteObserver(Action<KeyValuePair<string, object>> writeCallback) 21 { 22 _writeCallback = writeCallback; 23 } 24 OnCompleted()25 public void OnCompleted() 26 { 27 } 28 OnError(Exception error)29 public void OnError(Exception error) 30 { 31 } 32 OnNext(KeyValuePair<string, object> value)33 public void OnNext(KeyValuePair<string, object> value) 34 { 35 _writeCallback(value); 36 } 37 } 38 39 private readonly Action<KeyValuePair<string, object>> _writeCallback; 40 private bool _writeObserverEnabled; 41 FakeDiagnosticListenerObserver(Action<KeyValuePair<string, object>> writeCallback)42 public FakeDiagnosticListenerObserver(Action<KeyValuePair<string, object>> writeCallback) 43 { 44 _writeCallback = writeCallback; 45 } 46 OnCompleted()47 public void OnCompleted() 48 { 49 } 50 OnError(Exception error)51 public void OnError(Exception error) 52 { 53 } 54 OnNext(DiagnosticListener value)55 public void OnNext(DiagnosticListener value) 56 { 57 if (value.Name.Equals("SqlClientDiagnosticListener")) 58 { 59 value.Subscribe(new FakeDiagnosticSourceWriteObserver(_writeCallback), IsEnabled); 60 } 61 } 62 Enable()63 public void Enable() 64 { 65 _writeObserverEnabled = true; 66 } Disable()67 public void Disable() 68 { 69 _writeObserverEnabled = false; 70 } IsEnabled(string s)71 private bool IsEnabled(string s) 72 { 73 return _writeObserverEnabled; 74 } 75 } 76 }