Sample of my request
http://localhost:8065/api/note
POST
content-type:application/json
request body: { \"id\" : \"1234\", \"title\" : \"test\", \"status\" : \"dr
Following snippet worked for me, to log request only if there is any exception.(.Net Core 3.1)
{
public class ExceptionFilter : IActionFilter
{
private ConcurrentDictionary requests = new ConcurrentDictionary();
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Exception != null)
{
StringBuilder parameters = new StringBuilder();
_logger.LogError("Error while executing action:" + context.ActionDescriptor.DisplayName);
string errRequest;
if(requests.TryGetValue(context.HttpContext.TraceIdentifier,out errRequest))
{
_logger.LogError(errRequest);
}
_logger.LogError(context.Exception);
context.Result = new ObjectResult("Error!!!")
{
StatusCode = 500,
};
context.ExceptionHandled = true;
}
string req;
requests.Remove(context.HttpContext.TraceIdentifier, out req);
}
public void OnActionExecuting(ActionExecutingContext context)
{
StringBuilder sb = new StringBuilder();
foreach (var arg in context.ActionArguments)
{
sb.Append(arg.Key.ToString() + ":" + Newtonsoft.Json.JsonConvert.SerializeObject(arg.Value) + "\n");
}
requests.TryAdd(context.HttpContext.TraceIdentifier, sb.ToString());
}
}
}