title: Is there any way to get request body in .NET Core FilterAttribute?
body: <p>Sample of my request</p>
  
  <pre><code>http://localhost:8065/api/note
  POST
  content-type:application/json
  request body: { "id" : "1234", "title" : "test", "status" : "draft"}
  </code></pre>
  
  <p>and the response should be</p>
  
  <pre><code>{ "msg" : "ok", "code" : 1}
  </code></pre>
  
  <p>The action</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Post([FromBody]NoteModel model)
  </code></pre>
  
  <hr>
  
  <p>In order to have every request logged automatically, I create an attribute to do this job. The attribute looks like: (from <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#Dependency%20injection" rel="noreferrer">Microsoft Docs</a>)</p>
  
  <pre><code>public class SampleActionFilterAttribute : TypeFilterAttribute
  {
      public SampleActionFilterAttribute():base(typeof(SampleActionFilterImpl))
      {
      }
  
      private class SampleActionFilterImpl : IActionFilter
      {
          private readonly ILogger _logger;
          public SampleActionFilterImpl(ILoggerFactory loggerFactory)
          {
              _logger = loggerFactory.CreateLogger&lt;SampleActionFilterAttribute&gt;();
          }
  
          public void OnActionExecuting(ActionExecutingContext context)
          {
  
          }
  
          public void OnActionExecuted(ActionExecutedContext context)
          {
              _logger.LogDebug("[path]" + context.HttpContext.Request.Path);
              _logger.LogDebug("[method]" + context.HttpContext.Request.Method);
              _logger.LogDebug("[body]"); //log request body, expectation: { "id" : "1234", "title" : "test", "status" : "draft"}
              _logger.LogDebug("[statuscode]" + context.HttpContext.Response.StatusCode);
              _logger.LogDebug("[response]"); //log response
          }
      }
  }
  </code></pre>
  
  <p>I try to use streamReader to get request body only get empty string.</p>
  
  <pre><code>StreamReader reader = new StreamReader(context.HttpContext.Request.Body);
  string text = reader.ReadToEnd();
  </code></pre>
  
  <p>Is that because the body was read by [fromBody] from controller so the stream can not be read twice? If so, how am I supposed to get request body and response in <code>OnActionExecuted</code> method?</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>I've just copied Set's code into my project, not working. Here is the debug gif<a href="https://i.stack.imgur.com/oIqwF.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/oIqwF.gif" alt="enter image description here"></a></p>