Skip to content

Commit

Permalink
Delete NetEventSource.IsEnabled (#44901)
Browse files Browse the repository at this point in the history
* Delete NetEventSource.IsEnabled

These were all meant to be changed to NetEventSource.Log.IsEnabled(), and there are almost 900 occurrences of that pattern.  But there are still 9 stragglers, and the method itself.  Deleting the method and fixing the call sites.

* Fix uses in NetEventSource itself
  • Loading branch information
stephentoub authored Nov 19, 2020
1 parent 75c0b99 commit 8bbbf6d
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static unsafe SafeFreeCredentials AcquireCredentialsHandle(ISSPIInterface

if (errorCode != 0)
{
if (NetEventSource.IsEnabled) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_with_error, nameof(AcquireCredentialsHandle), $"0x{errorCode:X}"));
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, SR.Format(SR.net_log_operation_failed_with_error, nameof(AcquireCredentialsHandle), $"0x{errorCode:X}"));
throw new Win32Exception(errorCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public static unsafe int AcquireCredentialsHandle(
ref outCredential._handle,
out timeStamp);

if (NetEventSource.IsEnabled) NetEventSource.Verbose(null, $"{nameof(Interop.SspiCli.AcquireCredentialsHandleW)} returns 0x{errorCode:x}, handle = {outCredential}");
if (NetEventSource.Log.IsEnabled()) NetEventSource.Verbose(null, $"{nameof(Interop.SspiCli.AcquireCredentialsHandleW)} returns 0x{errorCode:x}, handle = {outCredential}");

if (errorCode != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void Enter(object? thisOrContextObject, FormattableString? formatt
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(formattableString);
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
}

/// <summary>Logs entrance to a method.</summary>
Expand All @@ -107,7 +107,7 @@ public static void Enter(object? thisOrContextObject, object arg0, [CallerMember
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(arg0);
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
}

/// <summary>Logs entrance to a method.</summary>
Expand All @@ -121,7 +121,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
DebugValidateArg(thisOrContextObject);
DebugValidateArg(arg0);
DebugValidateArg(arg1);
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)}, {Format(arg1)})");
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)}, {Format(arg1)})");
}

/// <summary>Logs entrance to a method.</summary>
Expand All @@ -137,7 +137,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
DebugValidateArg(arg0);
DebugValidateArg(arg1);
DebugValidateArg(arg2);
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)}, {Format(arg1)}, {Format(arg2)})");
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)}, {Format(arg1)}, {Format(arg2)})");
}

[Event(EnterEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
Expand All @@ -155,7 +155,7 @@ public static void Exit(object? thisOrContextObject, FormattableString? formatta
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(formattableString);
if (IsEnabled) Log.Exit(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
if (Log.IsEnabled()) Log.Exit(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
}

/// <summary>Logs exit from a method.</summary>
Expand All @@ -167,7 +167,7 @@ public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberN
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(arg0);
if (IsEnabled) Log.Exit(IdOf(thisOrContextObject), memberName, Format(arg0).ToString());
if (Log.IsEnabled()) Log.Exit(IdOf(thisOrContextObject), memberName, Format(arg0).ToString());
}

/// <summary>Logs exit from a method.</summary>
Expand All @@ -181,7 +181,7 @@ public static void Exit(object? thisOrContextObject, object arg0, object arg1, [
DebugValidateArg(thisOrContextObject);
DebugValidateArg(arg0);
DebugValidateArg(arg1);
if (IsEnabled) Log.Exit(IdOf(thisOrContextObject), memberName, $"{Format(arg0)}, {Format(arg1)}");
if (Log.IsEnabled()) Log.Exit(IdOf(thisOrContextObject), memberName, $"{Format(arg0)}, {Format(arg1)}");
}

[Event(ExitEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
Expand All @@ -199,7 +199,7 @@ public static void Info(object? thisOrContextObject, FormattableString? formatta
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(formattableString);
if (IsEnabled) Log.Info(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
if (Log.IsEnabled()) Log.Info(IdOf(thisOrContextObject), memberName, formattableString != null ? Format(formattableString) : NoParameters);
}

/// <summary>Logs an information message.</summary>
Expand All @@ -211,7 +211,7 @@ public static void Info(object? thisOrContextObject, object? message, [CallerMem
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(message);
if (IsEnabled) Log.Info(IdOf(thisOrContextObject), memberName, Format(message).ToString());
if (Log.IsEnabled()) Log.Info(IdOf(thisOrContextObject), memberName, Format(message).ToString());
}

[Event(InfoEventId, Level = EventLevel.Informational, Keywords = Keywords.Default)]
Expand All @@ -229,7 +229,7 @@ public static void Error(object? thisOrContextObject, FormattableString formatta
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(formattableString);
if (IsEnabled) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
if (Log.IsEnabled()) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
}

/// <summary>Logs an error message.</summary>
Expand All @@ -241,7 +241,7 @@ public static void Error(object? thisOrContextObject, object message, [CallerMem
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(message);
if (IsEnabled) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(message).ToString());
if (Log.IsEnabled()) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(message).ToString());
}

[Event(ErrorEventId, Level = EventLevel.Error, Keywords = Keywords.Default)]
Expand Down Expand Up @@ -269,7 +269,7 @@ public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [Calle
[NonEvent]
public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string? memberName = null)
{
if (IsEnabled && offset >= 0 && offset <= buffer.Length - count)
if (Log.IsEnabled() && offset >= 0 && offset <= buffer.Length - count)
{
count = Math.Min(count, MaxDumpSize);

Expand All @@ -295,7 +295,7 @@ public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferP
Debug.Assert(bufferPtr != IntPtr.Zero);
Debug.Assert(count >= 0);

if (IsEnabled)
if (Log.IsEnabled())
{
var buffer = new byte[Math.Min(count, MaxDumpSize)];
fixed (byte* targetPtr = buffer)
Expand All @@ -321,7 +321,7 @@ public static void Associate(object first, object second, [CallerMemberName] str
{
DebugValidateArg(first);
DebugValidateArg(second);
if (IsEnabled) Log.Associate(IdOf(first), memberName, IdOf(first), IdOf(second));
if (Log.IsEnabled()) Log.Associate(IdOf(first), memberName, IdOf(first), IdOf(second));
}

/// <summary>Logs a relationship between two objects.</summary>
Expand All @@ -335,7 +335,7 @@ public static void Associate(object? thisOrContextObject, object first, object s
DebugValidateArg(thisOrContextObject);
DebugValidateArg(first);
DebugValidateArg(second);
if (IsEnabled) Log.Associate(IdOf(thisOrContextObject), memberName, IdOf(first), IdOf(second));
if (Log.IsEnabled()) Log.Associate(IdOf(thisOrContextObject), memberName, IdOf(first), IdOf(second));
}

[Event(AssociateEventId, Level = EventLevel.Informational, Keywords = Keywords.Default, Message = "[{2}]<-->[{3}]")]
Expand All @@ -348,7 +348,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
private static void DebugValidateArg(object? arg)
{
if (!IsEnabled)
if (!Log.IsEnabled())
{
Debug.Assert(!(arg is ValueType), $"Should not be passing value type {arg?.GetType()} to logging without IsEnabled check");
Debug.Assert(!(arg is FormattableString), $"Should not be formatting FormattableString \"{arg}\" if tracing isn't enabled");
Expand All @@ -358,12 +358,9 @@ private static void DebugValidateArg(object? arg)
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
private static void DebugValidateArg(FormattableString? arg)
{
Debug.Assert(IsEnabled || arg == null, $"Should not be formatting FormattableString \"{arg}\" if tracing isn't enabled");
Debug.Assert(Log.IsEnabled() || arg == null, $"Should not be formatting FormattableString \"{arg}\" if tracing isn't enabled");
}

public static new bool IsEnabled =>
Log.IsEnabled();

[NonEvent]
public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : NullInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ private void BufferBytes(ReadOnlySpan<byte> span)

_recvBuffer.Discard(bytesRead);

if (NetEventSource.IsEnabled)
if (NetEventSource.Log.IsEnabled())
{
Trace($"Received frame {frameType} of length {payloadLength}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public void RegisterForCancellation(CancellationToken cancellationToken)
}
}

if (cancelResult != 0 && cancelResult != Interop.Winsock.WSA_INVALID_HANDLE && NetEventSource.IsEnabled)
if (cancelResult != 0 && cancelResult != Interop.Winsock.WSA_INVALID_HANDLE && NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(@this, $"GetAddrInfoExCancel returned error {cancelResult}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ internal override async ValueTask<int> ReadAsync(Memory<byte> destination, Cance
throw new InvalidOperationException("Reading is not allowed on stream.");
}

if (NetEventSource.IsEnabled)
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(this, $"[{GetHashCode()}] reading into Memory of '{destination.Length}' bytes.");
}
Expand Down Expand Up @@ -479,7 +479,7 @@ private static uint NativeCallbackHandler(

private uint HandleEvent(ref StreamEvent evt)
{
if (NetEventSource.IsEnabled)
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(this, $"[{GetHashCode()}] handling event '{evt.Type}'.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private SslStreamCertificateContext(X509Certificate2 target, X509Certificate2[]
{
store.Dispose();
store = null;
if (NetEventSource.IsEnabled)
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Error(this, $"Failed to open certificate store for intermediates.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static SslStreamCertificateContext Create(X509Certificate2 target, X509Ce
chain.ChainPolicy.DisableCertificateDownloads = offline;
bool chainStatus = chain.Build(target);

if (!chainStatus && NetEventSource.IsEnabled)
if (!chainStatus && NetEventSource.Log.IsEnabled())
{
NetEventSource.Error(null, $"Failed to build chain for {target.Subject}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static unsafe SafeFreeCredentials AcquireCredentialsHandleSchCredentials(
credential.paCred = &certificateHandle;
}

if (NetEventSource.IsEnabled) NetEventSource.Info($"flags=({flags}), ProtocolFlags=({protocolFlags}), EncryptionPolicy={policy}");
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info($"flags=({flags}), ProtocolFlags=({protocolFlags}), EncryptionPolicy={policy}");

if (protocolFlags != 0)
{
Expand Down

0 comments on commit 8bbbf6d

Please sign in to comment.