有没有办法在ASP.NET Core中的HTTP日志记录中忽略Swagger响应?

huangapple go评论74阅读模式
英文:

Is there a way to ignore swagger response in HTTP Logging in ASP.NET Core?

问题

我有一个使用.NET 7创建的ASP.NET Web API。

我启用了HTTP日志记录,以在应用程序的日志中查看完整的HTTP请求和响应(我知道它可能包含敏感信息,但考虑到应用程序的性质,这不应该是问题)。

HTTP日志记录正常工作,除了一个我忽略的问题。Swagger响应现在也被记录下来。因此,每次我访问Swagger端点时,它会在我的日志中填充Swagger HTML和Swagger JSON等信息。

这不是一个主要问题,因为在生产环境中它很可能不会填充日志,因为它不会一直是应用程序触发的主要请求,但在开发过程中,它经常被使用,这有点烦人,因为它最终会填充我目前不太重要的日志信息。

在阅读有关HttpLogging的链接时,我看到可以过滤MediaTypeOptions。由于我只有一个Web API,除了application/json之外,我不需要其他任何内容,因此我认为我应该过滤掉除此之外的所有内容:

services
.AddHttpLogging(options =>
{
  options.LoggingFields = HttpLoggingFields.All;
  options.RequestBodyLogLimit = 4096;
  options.ResponseBodyLogLimit = 4096;
  options.MediaTypeOptions.Clear();
  options.MediaTypeOptions.AddText("application/json");
})

但这似乎不起作用。我仍然在日志中看到Swagger输出。

明确一下,我不想完全忽略Swagger请求,我只想忽略它们在我的HTTP日志记录中被记录。

有没有一种简单的方法可以从我的HTTP日志记录中过滤掉Swagger?

非常感谢任何帮助。

英文:

I have a asp.net web api that is created with .net 7.

I enabled http logging to see the complete http request and responses in the logs of the application. (I am aware that it could contain sensitive information, but with the nature of the application, this should not be a problem).

The Http Logging is working like expected, with the exception of one oversight on my part. Swagger responses are also being logged now. Thus every time I navigate to my swagger endpoint, it fills my logs with the swagger HTML the swagger json. Things like this:

Connection: keep-alive
Host: localhost:7122
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en
Cookie: [Redacted]
Upgrade-Insecure-Requests: [Redacted]
Sec-GPC: [Redacted]
Sec-Fetch-Site: [Redacted]
Sec-Fetch-Mode: [Redacted]
Sec-Fetch-User: [Redacted]
Sec-Fetch-Dest: [Redacted]
sec-ch-ua: [Redacted]
sec-ch-ua-mobile: [Redacted]
sec-ch-ua-platform: [Redacted]
[14:55:42 INF] Response:
StatusCode: 200
Content-Type: text/html;charset=utf-8
[14:55:42 INF] ResponseBody: <!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css">
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
    <style>

        html {
            box-sizing: border-box;
            overflow: -moz-scrollbars-vertical;
            overflow-y: scroll;
        }

        *,
        *:before,
        *:after {
            box-sizing: inherit;
        }

        body {
            margin: 0;
            background: #fafafa;
        }
    </style>
    
</head>

<body>
    <div id="swagger-ui"></div>

    <!-- Workaround for https://github.com/swagger-api/swagger-editor/issues/1371 -->
    <script>

...
/// Rest left out because I think you get the point...

It is not a major issue as it will most likely not flood the logs when it is in production, as it won't be the main request being triggered by the application all the time, but during development it is something that is being used quite a lot and it is a bit irritating because it does end up filling up the logs with info that is not that crucial to me at the moment.

While reading through the link about the HttpLogging , I did see that I can filter out MediaTypeOptions. Since I only have a web api, I don't need anything other than application/json so I thought I would filter out everything except that:

services
.AddHttpLogging(options =>
{
  options.LoggingFields = HttpLoggingFields.All;
  options.RequestBodyLogLimit = 4096;
  options.ResponseBodyLogLimit = 4096;
  options.MediaTypeOptions.Clear();
  options.MediaTypeOptions.AddText("application/json");
})

But that does not seem to work. I am still stuck with swagger output in my logs.

To be clear, I don't want to ignore swagger requests completely, I just want to ignore them being logged in my httpLogging.

Is there an easy way to filter out the swagger from my HttpLogging?

Any help would be greatly appreciated.

答案1

得分: 5

不知道为什么会发生这种情况(看起来像是文档有点误导或者是一个bug,我会稍后进行调查),但至少有两种解决方法:

  1. 在 Swagger 之后调用 UseHttpLogging()

    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseHttpLogging();
    
  2. 使用条件语句:

    app.UseWhen(ctx => !ctx.Request.Path.StartsWithSegments("/swagger"), // 假设默认的 Swagger 路径,如果不是,请相应地进行调整
     appBuilder => appBuilder.UseHttpLogging());
    
英文:

Do not know why this does happen (seems like a bit misleading documentation or a bug to me, will investigate later) but there are at least 2 workarounds:

  1. Call UseHttpLogging() after the swagger:

    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseHttpLogging();
    
  2. Use conditionals:

    app.UseWhen(ctx => !ctx.Request.Path.StartsWithSegments("/swagger"), // assuming default swagger path, if not - adjust accrodingly
     appBuilder => appBuilder.UseHttpLogging());
    

huangapple
  • 本文由 发表于 2023年2月6日 22:09:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定