From c68b006574c7132469bda1672bcb2f73a9337871 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:46:07 -0700 Subject: [PATCH 1/6] Add more helpful log messages --- .../CaptureParametersOperation.cs | 33 ++++++++++++++----- src/Tools/dotnet-monitor/Strings.Designer.cs | 29 +++++++++++++++- src/Tools/dotnet-monitor/Strings.resx | 14 +++++++- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs index 2bf58687e65..b89ec6785e9 100644 --- a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs +++ b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Globalization; using System.Threading; using System.Threading.Tasks; @@ -49,19 +50,38 @@ public static bool IsEndpointRuntimeSupported(IEndpointInfo endpointInfo) return endpointInfo.RuntimeVersion != null && endpointInfo.RuntimeVersion.Major >= 7; } - private async Task CanEndpointProcessRequestsAsync(CancellationToken token) + private async Task EnsureEndpointProcessRequestsAsync(CancellationToken token) { DiagnosticsClient client = new(_endpointInfo.Endpoint); IDictionary env = await client.GetProcessEnvironmentAsync(token); + static Exception getNotAvailableException(string reason) + { + return new MonitoringException(string.Format( + CultureInfo.InvariantCulture, + Strings.ErrorMessage_ParameterCapturingNotAvailable, + reason)); + } + + const string PreventHostingStartupEnvName = "ASPNETCORE_PREVENTHOSTINGSTARTUP"; + if (env.TryGetValue(PreventHostingStartupEnvName, out string preventHostingStartupEnvValue) && + ToolIdentifiers.IsEnvVarValueEnabled(preventHostingStartupEnvValue)) + { + throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_PreventedHostingStartup); + } + if (!env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.ManagedMessaging, out string isManagedMessagingAvailable) || - !env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.HostingStartup, out string isHostingStartupAvailable)) + !ToolIdentifiers.IsEnvVarValueEnabled(isManagedMessagingAvailable)) { - return false; + throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad); } - return ToolIdentifiers.IsEnvVarValueEnabled(isManagedMessagingAvailable) && ToolIdentifiers.IsEnvVarValueEnabled(isHostingStartupAvailable); + if (!env.TryGetValue(InProcessFeaturesIdentifiers.EnvironmentVariables.AvailableInfrastructure.HostingStartup, out string isHostingStartupAvailable) || + !ToolIdentifiers.IsEnvVarValueEnabled(isHostingStartupAvailable)) + { + throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad); + } } public async Task ExecuteAsync(CancellationToken token) @@ -74,10 +94,7 @@ public async Task ExecuteAsync(CancellationToken token) } // Check if the endpoint is capable of responding to our requests - if (!await CanEndpointProcessRequestsAsync(token)) - { - throw new MonitoringException(Strings.ErrorMessage_ParameterCapturingNotAvailable); - } + await EnsureEndpointProcessRequestsAsync(token); EventParameterCapturingPipelineSettings settings = new() { diff --git a/src/Tools/dotnet-monitor/Strings.Designer.cs b/src/Tools/dotnet-monitor/Strings.Designer.cs index 0d54b6b1517..4c3cdaf6d51 100644 --- a/src/Tools/dotnet-monitor/Strings.Designer.cs +++ b/src/Tools/dotnet-monitor/Strings.Designer.cs @@ -421,7 +421,7 @@ internal static string ErrorMessage_OperationIsNotStoppable { } /// - /// Looks up a localized string similar to Parameter capturing is not available in the process.. + /// Looks up a localized string similar to Parameter capturing is not available in the process. {0}. /// internal static string ErrorMessage_ParameterCapturingNotAvailable { get { @@ -1699,6 +1699,33 @@ internal static string Message_ShowSources { } } + /// + /// Looks up a localized string similar to This feature is only available on ASP.NET Core. If the process is ASP.NET Core and has started, ensure that it has not been configured to prevent hosting startup assemblies from loading. See https://learn.microsoft.com/aspnet/core/fundamentals/host/platform-specific-configuration#disable-automatic-loading-of-hosting-startup-assemblies for more information.. + /// + internal static string ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad { + get { + return ResourceManager.GetString("ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An internal error occured that prevented communicated between the process and dotnet-monitor.. + /// + internal static string ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad { + get { + return ResourceManager.GetString("ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable.. + /// + internal static string ParameterCapturingNotAvailable_Reason_PreventedHostingStartup { + get { + return ResourceManager.GetString("ParameterCapturingNotAvailable_Reason_PreventedHostingStartup", resourceCulture); + } + } + /// /// Looks up a localized string similar to :NOT PRESENT:. /// diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index dffb2e9da28..751679a25b1 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -942,9 +942,21 @@ The shared file '{0}' is different from source file '{1}'. - Parameter capturing is not available in the process. + Parameter capturing is not available in the process. {0} + Gets the format string for parameter capturing not being available +1 Format Parameters: +0. reason: The reason why the feature is unavailable. Parameter capturing is not available in the process, the process needs to be using .NET 7 or newer. + + The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable. + + + This feature is only available on ASP.NET Core. If the process is ASP.NET Core and has started, ensure that it has not been configured to prevent hosting startup assemblies from loading. See https://learn.microsoft.com/aspnet/core/fundamentals/host/platform-specific-configuration#disable-automatic-loading-of-hosting-startup-assemblies for more information. + + + An internal error occured that prevented communicated between the process and dotnet-monitor. + \ No newline at end of file From b2fcb23ffdd31ca5116ae87176c36939a94f7544 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:47:18 -0700 Subject: [PATCH 2/6] remove msdn link --- src/Tools/dotnet-monitor/Strings.Designer.cs | 2 +- src/Tools/dotnet-monitor/Strings.resx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-monitor/Strings.Designer.cs b/src/Tools/dotnet-monitor/Strings.Designer.cs index 4c3cdaf6d51..b99de7661e6 100644 --- a/src/Tools/dotnet-monitor/Strings.Designer.cs +++ b/src/Tools/dotnet-monitor/Strings.Designer.cs @@ -1700,7 +1700,7 @@ internal static string Message_ShowSources { } /// - /// Looks up a localized string similar to This feature is only available on ASP.NET Core. If the process is ASP.NET Core and has started, ensure that it has not been configured to prevent hosting startup assemblies from loading. See https://learn.microsoft.com/aspnet/core/fundamentals/host/platform-specific-configuration#disable-automatic-loading-of-hosting-startup-assemblies for more information.. + /// Looks up a localized string similar to This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has succesfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading.. /// internal static string ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad { get { diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index 751679a25b1..02641588b7a 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -954,7 +954,7 @@ The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable. - This feature is only available on ASP.NET Core. If the process is ASP.NET Core and has started, ensure that it has not been configured to prevent hosting startup assemblies from loading. See https://learn.microsoft.com/aspnet/core/fundamentals/host/platform-specific-configuration#disable-automatic-loading-of-hosting-startup-assemblies for more information. + This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has succesfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading. An internal error occured that prevented communicated between the process and dotnet-monitor. From 729f36ee5f57b8d24081e3d6a89964791df7dce7 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:50:02 -0700 Subject: [PATCH 3/6] Standardize net 7 or newer --- .../CaptureParametersOperation.cs | 18 +++++++++--------- src/Tools/dotnet-monitor/Strings.Designer.cs | 18 +++++++++--------- src/Tools/dotnet-monitor/Strings.resx | 6 +++--- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs index b89ec6785e9..73b0ae81040 100644 --- a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs +++ b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs @@ -52,10 +52,6 @@ public static bool IsEndpointRuntimeSupported(IEndpointInfo endpointInfo) private async Task EnsureEndpointProcessRequestsAsync(CancellationToken token) { - DiagnosticsClient client = new(_endpointInfo.Endpoint); - - IDictionary env = await client.GetProcessEnvironmentAsync(token); - static Exception getNotAvailableException(string reason) { return new MonitoringException(string.Format( @@ -64,6 +60,15 @@ static Exception getNotAvailableException(string reason) reason)); } + if (!IsEndpointRuntimeSupported(_endpointInfo)) + { + throw getNotAvailableException(Strings.ParameterCapturingNotAvailable_Reason_UnsupportedRuntime); + } + + DiagnosticsClient client = new(_endpointInfo.Endpoint); + + IDictionary env = await client.GetProcessEnvironmentAsync(token); + const string PreventHostingStartupEnvName = "ASPNETCORE_PREVENTHOSTINGSTARTUP"; if (env.TryGetValue(PreventHostingStartupEnvName, out string preventHostingStartupEnvValue) && ToolIdentifiers.IsEnvVarValueEnabled(preventHostingStartupEnvValue)) @@ -88,11 +93,6 @@ public async Task ExecuteAsync(CancellationToken token) { try { - if (!IsEndpointRuntimeSupported(_endpointInfo)) - { - throw new MonitoringException(Strings.ErrorMessage_ParameterCapturingRequiresAtLeastNet7); - } - // Check if the endpoint is capable of responding to our requests await EnsureEndpointProcessRequestsAsync(token); diff --git a/src/Tools/dotnet-monitor/Strings.Designer.cs b/src/Tools/dotnet-monitor/Strings.Designer.cs index b99de7661e6..dcd6c5a4320 100644 --- a/src/Tools/dotnet-monitor/Strings.Designer.cs +++ b/src/Tools/dotnet-monitor/Strings.Designer.cs @@ -429,15 +429,6 @@ internal static string ErrorMessage_ParameterCapturingNotAvailable { } } - /// - /// Looks up a localized string similar to Parameter capturing is not available in the process, the process needs to be using .NET 7 or newer.. - /// - internal static string ErrorMessage_ParameterCapturingRequiresAtLeastNet7 { - get { - return ResourceManager.GetString("ErrorMessage_ParameterCapturingRequiresAtLeastNet7", resourceCulture); - } - } - /// /// Looks up a localized string similar to The {0} parameter value '{1}' is not allowed.. /// @@ -1726,6 +1717,15 @@ internal static string ParameterCapturingNotAvailable_Reason_PreventedHostingSta } } + /// + /// Looks up a localized string similar to The process needs to be using .NET 7 or newer.. + /// + internal static string ParameterCapturingNotAvailable_Reason_UnsupportedRuntime { + get { + return ResourceManager.GetString("ParameterCapturingNotAvailable_Reason_UnsupportedRuntime", resourceCulture); + } + } + /// /// Looks up a localized string similar to :NOT PRESENT:. /// diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index 02641588b7a..e4af11b0ea9 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -947,9 +947,6 @@ 1 Format Parameters: 0. reason: The reason why the feature is unavailable. - - Parameter capturing is not available in the process, the process needs to be using .NET 7 or newer. - The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable. @@ -959,4 +956,7 @@ An internal error occured that prevented communicated between the process and dotnet-monitor. + + The process needs to be using .NET 7 or newer. + \ No newline at end of file From 3b16a58295e30d846bb85698d622f3d107a5909d Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:55:52 -0700 Subject: [PATCH 4/6] spelling --- src/Tools/dotnet-monitor/Strings.Designer.cs | 4 ++-- src/Tools/dotnet-monitor/Strings.resx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Tools/dotnet-monitor/Strings.Designer.cs b/src/Tools/dotnet-monitor/Strings.Designer.cs index dcd6c5a4320..abf79f46a03 100644 --- a/src/Tools/dotnet-monitor/Strings.Designer.cs +++ b/src/Tools/dotnet-monitor/Strings.Designer.cs @@ -1691,7 +1691,7 @@ internal static string Message_ShowSources { } /// - /// Looks up a localized string similar to This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has succesfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading.. + /// Looks up a localized string similar to This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has successfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading.. /// internal static string ParameterCapturingNotAvailable_Reason_HostingStartupDidNotLoad { get { @@ -1700,7 +1700,7 @@ internal static string ParameterCapturingNotAvailable_Reason_HostingStartupDidNo } /// - /// Looks up a localized string similar to An internal error occured that prevented communicated between the process and dotnet-monitor.. + /// Looks up a localized string similar to An internal error occurred that has prevented communicated between the process and dotnet-monitor.. /// internal static string ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad { get { diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index e4af11b0ea9..accdd521857 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -951,10 +951,10 @@ The process has prevented hosting startup assemblies from loading using the ASPNETCORE_PREVENTHOSTINGSTARTUP environment variable. - This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has succesfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading. + This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has successfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading. - An internal error occured that prevented communicated between the process and dotnet-monitor. + An internal error occurred that has prevented communicated between the process and dotnet-monitor. The process needs to be using .NET 7 or newer. From 6a253ffadf26ec583f10e432d0f69ba182c317ae Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:58:24 -0700 Subject: [PATCH 5/6] Cleanup message --- src/Tools/dotnet-monitor/Strings.Designer.cs | 2 +- src/Tools/dotnet-monitor/Strings.resx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-monitor/Strings.Designer.cs b/src/Tools/dotnet-monitor/Strings.Designer.cs index abf79f46a03..f51a5f966c4 100644 --- a/src/Tools/dotnet-monitor/Strings.Designer.cs +++ b/src/Tools/dotnet-monitor/Strings.Designer.cs @@ -1700,7 +1700,7 @@ internal static string ParameterCapturingNotAvailable_Reason_HostingStartupDidNo } /// - /// Looks up a localized string similar to An internal error occurred that has prevented communicated between the process and dotnet-monitor.. + /// Looks up a localized string similar to An internal error occurred that has prevented communication with the process.. /// internal static string ParameterCapturingNotAvailable_Reason_ManagedMessagingDidNotLoad { get { diff --git a/src/Tools/dotnet-monitor/Strings.resx b/src/Tools/dotnet-monitor/Strings.resx index accdd521857..10eb2d719a5 100644 --- a/src/Tools/dotnet-monitor/Strings.resx +++ b/src/Tools/dotnet-monitor/Strings.resx @@ -954,7 +954,7 @@ This feature is only available on processes using ASP.NET Core. If the process is using ASP.NET Core and has successfully started, ensure that it has not been configured to prevent hosting startup assemblies from loading. - An internal error occurred that has prevented communicated between the process and dotnet-monitor. + An internal error occurred that has prevented communication with the process. The process needs to be using .NET 7 or newer. From 376721380774ac5001a4e14a6b74ed707ccdec52 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 25 Aug 2023 15:59:10 -0700 Subject: [PATCH 6/6] Rename method --- .../ParameterCapturing/CaptureParametersOperation.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs index 73b0ae81040..6f4b79c74b3 100644 --- a/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs +++ b/src/Tools/dotnet-monitor/ParameterCapturing/CaptureParametersOperation.cs @@ -50,7 +50,7 @@ public static bool IsEndpointRuntimeSupported(IEndpointInfo endpointInfo) return endpointInfo.RuntimeVersion != null && endpointInfo.RuntimeVersion.Major >= 7; } - private async Task EnsureEndpointProcessRequestsAsync(CancellationToken token) + private async Task EnsureEndpointCanProcessRequestsAsync(CancellationToken token) { static Exception getNotAvailableException(string reason) { @@ -94,7 +94,7 @@ public async Task ExecuteAsync(CancellationToken token) try { // Check if the endpoint is capable of responding to our requests - await EnsureEndpointProcessRequestsAsync(token); + await EnsureEndpointCanProcessRequestsAsync(token); EventParameterCapturingPipelineSettings settings = new() {