Skip to content

Commit c3a6c82

Browse files
committed
add YarpDiagnosticSample.csproj
1 parent afd8e76 commit c3a6c82

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Yarp.ReverseProxy.Transforms;
2+
using YarpDiagnosticSample;
3+
4+
var builder = WebApplication.CreateSlimBuilder(args);
5+
builder.Services.AddHttpLogging();
6+
builder.Services.AddSingleton<YarpLoggingTransform>();
7+
builder.Services.AddReverseProxy()
8+
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
9+
.AddTransforms(transformContext =>
10+
transformContext.ResponseTransforms.Add(transformContext.Services.GetRequiredService<YarpLoggingTransform>()));
11+
;
12+
var app = builder.Build();
13+
app.UseHttpLogging();
14+
app.MapReverseProxy();
15+
app.Run();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Yarp.ReverseProxy" Version="2.3.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=yarp/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="YarpDiagnosticSample.csproj" />
3+
</Solution>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Yarp.ReverseProxy.Transforms;
2+
3+
namespace YarpDiagnosticSample;
4+
5+
public class YarpLoggingTransform : ResponseTransform
6+
{
7+
public override ValueTask ApplyAsync(ResponseTransformContext context)
8+
{
9+
var proxyResponse = context.ProxyResponse;
10+
if (proxyResponse is null)
11+
return ValueTask.CompletedTask;
12+
13+
var proxyRequest = proxyResponse.RequestMessage;
14+
15+
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<YarpLoggingTransform>>();
16+
if (proxyRequest is not null)
17+
{
18+
logger.LogInformation("Proxy request: {Url} {Headers}",
19+
proxyRequest.RequestUri?.AbsoluteUri, string.Join(";",
20+
proxyRequest.Headers.Select(h =>
21+
$"{h.Key}:{string.Join(",", h.Value)}")));
22+
}
23+
24+
logger.LogInformation("Proxy response: {ResponseStatusCode} {ResponseHeaders}",
25+
(int)proxyResponse.StatusCode, string.Join(";", proxyResponse.Headers.Select(x=> $"{x.Key}:{string.Join(",", x.Value)}")));
26+
return ValueTask.CompletedTask;
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Microsoft.AspNetCore.HttpLogging": "Information"
7+
}
8+
},
9+
"ReverseProxy": {
10+
"Routes": {
11+
"default" : {
12+
"ClusterId": "default",
13+
"Match": {
14+
"Path": "/"
15+
}
16+
}
17+
},
18+
"Clusters": {
19+
"default": {
20+
"Destinations": {
21+
"default": {
22+
"Address": "https://reservation.weihanli.xyz/api/notice"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)