2

We are currently using Serilog in ASP.NET Core WebApi with its standard middleware UseSerilogRequestLogging, which captures enough information for all GET cases. Now I am trying to figure out, couple of things.

  1. In case of POST, if I want to log the request body, only in case of failures (response code > 299), is there an existing option that I can use?
  2. If I am able to log the request body, how do I write it to separate sink so that I can push info to a separate destination ?

Just FYI, we are using file logging on VMs and console logging in Container. These are eventually forwarded to Splunk. We are using .Net 6.0

Thanks!

askids
  • 1,406
  • 1
  • 15
  • 32
  • Just to be clear you want to log body only in case of failure. Right ? – CodingMytra Sep 22 '22 at 15:18
  • Right, only for failure cases, I want to log. For successful calls, I dont want to log. For failed requests, this will help me troubleshoot the issue. – askids Sep 23 '22 at 03:58
  • then solution from @Jason below will not solve your complete issue. as it will log all the requests failure or success. – CodingMytra Sep 23 '22 at 04:35

1 Answers1

0

You can capture enough information for all Get request.

Because all the important information are in request url. For Post request, it save data in body, it won't show in url, that's why you think it not log all the information.

If you want get all the post information, you need to create a custom Middleware. You can check the link below.

How to add 'request body' in serilog's output .net core?

If you are confusing about the answer in the link, it don't mentioned serilog, I think there is not a existing option for us to enable this feature.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29