thepirat000 / Audit.NET

An extensible framework to audit executing operations in .NET and .NET Core.
MIT License
2.29k stars 323 forks source link

Issue with logging HttpPatch methods #558

Closed mtinnes closed 1 year ago

mtinnes commented 1 year ago

Describe the bug Controller actions using HttpPatch method result in a serialization error when logging the audit.

To Reproduce Create a controller method using HttpPatch and with a JsonPatchDocument body parameter. Like this:

        [HttpPatch]
        public async Task<IActionResult> Patch([FromBody] JsonPatchDocument<Models.Dto.Doc> patch)
        {
        }

Expected behavior Audit.NET should serialize the body of the request if enabled.

Libraries (specify the Audit.NET extensions being used including version): For example:

Target .NET framework: For example:

Additional context Seems JSON serialization breaks on the JsonPatchDocument type, specifically on the IContractResolver property.

thepirat000 commented 1 year ago

I could not reproduce it, please include the full stack trace for the exception and the code where you set up the audit.

Also please clarify how do you configure the JSON input formatter / serialization mechanism for your ASP NET Core app.

In my test, the event was properly serialized using System.Text.Json and targeting .NET 7 and looks like this:

{
  "Action": {
    "TraceId": "0HMN0GFK2RVA6:00000001",
    "HttpMethod": "PATCH",
    "ControllerName": "My",
    "ActionName": "JsonPatch",
    "ActionParameters": {
      "patchDoc": {
        "Operations": [
          {
            "value": "Updated",
            "OperationType": 2,
            "path": "/Name",
            "op": "replace"
          }
        ],
        "ContractResolver": {}
      }
    },
    "RequestUrl": "http://localhost:27050/api/My/JsonPatch",
    "IpAddress": "::1",
    "ResponseStatus": "OK",
    "ResponseStatusCode": 200,
    "RequestBody": {
      "Type": "application/json-patch+json; charset=utf-8",
      "Length": 51,
      "Value": "[{\"value\":\"Updated\",\"path\":\"/Name\",\"op\":\"replace\"}]"
    },
    "ResponseBody": {
      "Type": "OkResult",
      "Value": 200
    },
    "Headers": {
      "Host": "localhost:27050",
      "Content-Type": "application/json-patch+json; charset=utf-8",
      "Content-Length": "51"
    },
    "ResponseHeaders": {}
  },
  "EventType": "PATCH My/JsonPatch",
  "Environment": {
      ...
  },
  "StartDate": "2022-12-17T23:10:39.2991708Z",
  "EndDate": "2022-12-17T23:10:43.2457292Z",
  "Duration": 3947
}
thepirat000 commented 1 year ago

Closing this for inactivity. If you're still having the issue, please comment below