Skip to content

Commit

Permalink
启用缓存简略输出
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente.Yu <^@^>
  • Loading branch information
Vicente.Yu committed Dec 6, 2023
1 parent e2d002c commit 60f7298
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Attributes/EnableLoggingFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class EnableLoggingFilterAttribute : ApiFilterAttribute
{
private static readonly LoggingFilterAttribute _loggingFilter = new();

/// <summary>
/// 启用时,不再输出缓存内容。
/// </summary>
public bool EnableCacheLogging { get; set; } = true;

/// <summary>
/// 请求之前
/// </summary>
Expand All @@ -28,6 +33,16 @@ public async override Task OnRequestAsync(ApiRequestContext context)
public async override Task OnResponseAsync(ApiResponseContext context)
{
if (!IsEnable(context.HttpContext)) return;

if (EnableCacheLogging
&& context.HttpContext.ResponseMessage?.Headers?
.TryGetValues("Response-Cache-Provider", out var values) == true
&& values?.Any() == true)
{
context.HttpContext.ResponseMessage ??= new HttpResponseMessage();
context.HttpContext.ResponseMessage.Content = new StringContent("...来自缓存...");
}

await _loggingFilter.OnResponseAsync(context);
}

Expand Down

0 comments on commit 60f7298

Please sign in to comment.