Skip to content

Commit d0a695e

Browse files
committed
Update debug code to not use Startup /2
1 parent 550ffac commit d0a695e

File tree

5 files changed

+111
-155
lines changed

5 files changed

+111
-155
lines changed

aspnetcore/test/troubleshoot.md

Lines changed: 6 additions & 80 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
@@ -79,85 +79,11 @@ Place the following [middleware](xref:fundamentals/middleware/index#create-a-mid
7979

8080
To obtain the environment, use either of the following approaches:
8181

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-
```
82+
* Inject `IHostingEnvironment` and check the environment with the local variable. The following sample code demonstrates this approach.
83+
84+
* Assign the environment to a property in `Program.cs`. Check the environment using the property, for example, `if (app.Environment.IsDevelopment())`.
85+
86+
:::code language="csharp" source="~/test/troubleshoot/code/9.x/Program.cs" id="snippet1" highlight="13-85":::
16187

16288
## Debug ASP.NET Core apps
16389

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

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.GetRequiredService<IAuthenticationSchemeProvider>();
23+
24+
sb.Append($"Request{rule}");
25+
sb.Append($"{DateTimeOffset.Now}{nl}");
26+
sb.Append($"{context.Request.Method} {context.Request.Path}{nl}");
27+
sb.Append($"Scheme: {context.Request.Scheme}{nl}");
28+
sb.Append($"Host: {context.Request.Headers["Host"]}{nl}");
29+
sb.Append($"PathBase: {context.Request.PathBase.Value}{nl}");
30+
sb.Append($"Path: {context.Request.Path.Value}{nl}");
31+
sb.Append($"Query: {context.Request.QueryString.Value}{nl}{nl}");
32+
33+
sb.Append($"Connection{rule}");
34+
sb.Append($"RemoteIp: {context.Connection.RemoteIpAddress}{nl}");
35+
sb.Append($"RemotePort: {context.Connection.RemotePort}{nl}");
36+
sb.Append($"LocalIp: {context.Connection.LocalIpAddress}{nl}");
37+
sb.Append($"LocalPort: {context.Connection.LocalPort}{nl}");
38+
sb.Append($"ClientCert: {context.Connection.ClientCertificate}{nl}{nl}");
39+
40+
sb.Append($"Identity{rule}");
41+
sb.Append($"User: {context.User.Identity.Name}{nl}");
42+
var scheme = await authSchemeProvider
43+
.GetSchemeAsync(IISDefaults.AuthenticationScheme);
44+
sb.Append($"DisplayName: {scheme?.DisplayName}{nl}{nl}");
45+
46+
sb.Append($"Headers{rule}");
47+
foreach (var header in context.Request.Headers)
48+
{
49+
sb.Append($"{header.Key}: {header.Value}{nl}");
50+
}
51+
sb.Append(nl);
52+
53+
sb.Append($"WebSockets{rule}");
54+
if (context.Features.Get<IHttpUpgradeFeature>() != null)
55+
{
56+
sb.Append($"Status: Enabled{nl}{nl}");
57+
}
58+
else
59+
{
60+
sb.Append($"Status: Disabled{nl}{nl}");
61+
}
62+
63+
sb.Append($"Configuration{rule}");
64+
var config = builder.Configuration;
65+
66+
foreach (var pair in config.AsEnumerable())
67+
{
68+
sb.Append($"{pair.Key}: {pair.Value}{nl}");
69+
}
70+
sb.Append(nl);
71+
sb.Append(nl);
72+
73+
sb.Append($"Environment Variables{rule}");
74+
var vars = System.Environment.GetEnvironmentVariables();
75+
foreach (var key in vars.Keys.Cast<string>().OrderBy(key => key,
76+
StringComparer.OrdinalIgnoreCase))
77+
{
78+
var value = vars[key];
79+
sb.Append($"{key}: {value}{nl}");
80+
}
81+
82+
context.Response.ContentType = "text/plain";
83+
await context.Response.WriteAsync(sb.ToString());
84+
});
85+
}
86+
87+
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)