Skip to content

Commit 7cdd284

Browse files
Update debug code to not use Startup /2 (#35339)
* Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Update debug code to not use Startup /2 * Apply suggestions from code review Co-authored-by: Tom Dykstra <[email protected]> --------- Co-authored-by: Tom Dykstra <[email protected]>
1 parent 554eea0 commit 7cdd284

File tree

5 files changed

+110
-157
lines changed

5 files changed

+110
-157
lines changed

aspnetcore/test/troubleshoot.md

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: tdykstra
44
description: Understand and troubleshoot warnings and errors with ASP.NET Core projects.
55
ms.author: tdykstra
66
ms.custom: mvc
7-
ms.date: 07/10/2019
7+
ms.date: 5/2/2025
88
uid: test/troubleshoot
99
---
1010
# Troubleshoot and debug ASP.NET Core projects
@@ -77,87 +77,9 @@ If an app is capable of responding to requests, you can obtain the following dat
7777

7878
Place the following [middleware](xref:fundamentals/middleware/index#create-a-middleware-pipeline-with-iapplicationbuilder) code at the beginning of the `Startup.Configure` method's request processing pipeline. The environment is checked before the middleware is run to ensure that the code is only executed in the Development environment.
7979

80-
To obtain the environment, use either of the following approaches:
81-
82-
* Inject the `IHostingEnvironment` into the `Startup.Configure` method and check the environment with the local variable. The following sample code demonstrates this approach.
83-
84-
* Assign the environment to a property in the `Startup` class. Check the environment using the property (for example, `if (Environment.IsDevelopment())`).
85-
86-
```csharp
87-
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
88-
IConfiguration config)
89-
{
90-
if (env.IsDevelopment())
91-
{
92-
app.Run(async (context) =>
93-
{
94-
var sb = new StringBuilder();
95-
var nl = System.Environment.NewLine;
96-
var rule = string.Concat(nl, new string('-', 40), nl);
97-
var authSchemeProvider = app.ApplicationServices
98-
.GetRequiredService<IAuthenticationSchemeProvider>();
99-
100-
sb.Append($"Request{rule}");
101-
sb.Append($"{DateTimeOffset.Now}{nl}");
102-
sb.Append($"{context.Request.Method} {context.Request.Path}{nl}");
103-
sb.Append($"Scheme: {context.Request.Scheme}{nl}");
104-
sb.Append($"Host: {context.Request.Headers["Host"]}{nl}");
105-
sb.Append($"PathBase: {context.Request.PathBase.Value}{nl}");
106-
sb.Append($"Path: {context.Request.Path.Value}{nl}");
107-
sb.Append($"Query: {context.Request.QueryString.Value}{nl}{nl}");
108-
109-
sb.Append($"Connection{rule}");
110-
sb.Append($"RemoteIp: {context.Connection.RemoteIpAddress}{nl}");
111-
sb.Append($"RemotePort: {context.Connection.RemotePort}{nl}");
112-
sb.Append($"LocalIp: {context.Connection.LocalIpAddress}{nl}");
113-
sb.Append($"LocalPort: {context.Connection.LocalPort}{nl}");
114-
sb.Append($"ClientCert: {context.Connection.ClientCertificate}{nl}{nl}");
115-
116-
sb.Append($"Identity{rule}");
117-
sb.Append($"User: {context.User.Identity.Name}{nl}");
118-
var scheme = await authSchemeProvider
119-
.GetSchemeAsync(IISDefaults.AuthenticationScheme);
120-
sb.Append($"DisplayName: {scheme?.DisplayName}{nl}{nl}");
121-
122-
sb.Append($"Headers{rule}");
123-
foreach (var header in context.Request.Headers)
124-
{
125-
sb.Append($"{header.Key}: {header.Value}{nl}");
126-
}
127-
sb.Append(nl);
128-
129-
sb.Append($"WebSockets{rule}");
130-
if (context.Features.Get<IHttpUpgradeFeature>() != null)
131-
{
132-
sb.Append($"Status: Enabled{nl}{nl}");
133-
}
134-
else
135-
{
136-
sb.Append($"Status: Disabled{nl}{nl}");
137-
}
138-
139-
sb.Append($"Configuration{rule}");
140-
foreach (var pair in config.AsEnumerable())
141-
{
142-
sb.Append($"{pair.Path}: {pair.Value}{nl}");
143-
}
144-
sb.Append(nl);
145-
146-
sb.Append($"Environment Variables{rule}");
147-
var vars = System.Environment.GetEnvironmentVariables();
148-
foreach (var key in vars.Keys.Cast<string>().OrderBy(key => key,
149-
StringComparer.OrdinalIgnoreCase))
150-
{
151-
var value = vars[key];
152-
sb.Append($"{key}: {value}{nl}");
153-
}
154-
155-
context.Response.ContentType = "text/plain";
156-
await context.Response.WriteAsync(sb.ToString());
157-
});
158-
}
159-
}
160-
```
80+
Get the environment from the `Environment` property of `WebApplication`. For example, `if (app.Environment.IsDevelopment())` as in the following sample code.
81+
82+
:::code language="csharp" source="~/test/troubleshoot/code/9.x/Program.cs" highlight="13-85":::
16183

16284
## Debug ASP.NET Core apps
16385

aspnetcore/test/troubleshoot/code/5.x/Program.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Http.Features;
3+
using Microsoft.AspNetCore.Server.IISIntegration;
4+
using System.Text;
5+
6+
var builder = WebApplication.CreateBuilder(args);
7+
builder.Services.AddAuthentication();
8+
9+
var app = builder.Build();
10+
11+
app.MapGet("/", () => "Hello World!");
12+
13+
if (app.Environment.IsDevelopment())
14+
{
15+
app.Run(async (context) =>
16+
{
17+
var sb = new StringBuilder();
18+
var nl = System.Environment.NewLine;
19+
var rule = string.Concat(nl, new string('-', 40), nl);
20+
21+
22+
var authSchemeProvider = app.Services.
23+
GetRequiredService<IAuthenticationSchemeProvider>();
24+
25+
sb.Append($"Request{rule}");
26+
sb.Append($"{DateTimeOffset.Now}{nl}");
27+
sb.Append($"{context.Request.Method} {context.Request.Path}{nl}");
28+
sb.Append($"Scheme: {context.Request.Scheme}{nl}");
29+
sb.Append($"Host: {context.Request.Headers["Host"]}{nl}");
30+
sb.Append($"PathBase: {context.Request.PathBase.Value}{nl}");
31+
sb.Append($"Path: {context.Request.Path.Value}{nl}");
32+
sb.Append($"Query: {context.Request.QueryString.Value}{nl}{nl}");
33+
34+
sb.Append($"Connection{rule}");
35+
sb.Append($"RemoteIp: {context.Connection.RemoteIpAddress}{nl}");
36+
sb.Append($"RemotePort: {context.Connection.RemotePort}{nl}");
37+
sb.Append($"LocalIp: {context.Connection.LocalIpAddress}{nl}");
38+
sb.Append($"LocalPort: {context.Connection.LocalPort}{nl}");
39+
sb.Append($"ClientCert: {context.Connection.ClientCertificate}{nl}{nl}");
40+
41+
sb.Append($"Identity{rule}");
42+
sb.Append($"User: {context.User.Identity.Name}{nl}");
43+
var scheme = await authSchemeProvider
44+
.GetSchemeAsync(IISDefaults.AuthenticationScheme);
45+
sb.Append($"DisplayName: {scheme?.DisplayName}{nl}{nl}");
46+
47+
sb.Append($"Headers{rule}");
48+
foreach (var header in context.Request.Headers)
49+
{
50+
sb.Append($"{header.Key}: {header.Value}{nl}");
51+
}
52+
sb.Append(nl);
53+
54+
sb.Append($"WebSockets{rule}");
55+
if (context.Features.Get<IHttpUpgradeFeature>() != null)
56+
{
57+
sb.Append($"Status: Enabled{nl}{nl}");
58+
}
59+
else
60+
{
61+
sb.Append($"Status: Disabled{nl}{nl}");
62+
}
63+
64+
sb.Append($"Configuration{rule}");
65+
var config = builder.Configuration;
66+
67+
foreach (var pair in config.AsEnumerable())
68+
{
69+
sb.Append($"{pair.Key}: {pair.Value}{nl}");
70+
}
71+
sb.Append(nl);
72+
sb.Append(nl);
73+
74+
sb.Append($"Environment Variables{rule}");
75+
var vars = System.Environment.GetEnvironmentVariables();
76+
foreach (var key in vars.Keys.Cast<string>().OrderBy(key => key,
77+
StringComparer.OrdinalIgnoreCase))
78+
{
79+
var value = vars[key];
80+
sb.Append($"{key}: {value}{nl}");
81+
}
82+
83+
context.Response.ContentType = "text/plain";
84+
await context.Response.WriteAsync(sb.ToString());
85+
});
86+
}
87+
88+
app.Run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)