IAsyncActionFilter
を実装するフィルタを作成し、現在のリクエストのコンテキストのActionParameters
とそのResult
からデータを取得したいと考えています。私はカスタム属性MyLogAttribute
を使用してロギングの挙動を指示しています。重要な情報を含むフィールドをログに記録して表示するようオプトインします。ASP.NETのコアAsyncActionFilterでアクションのアーギュメントと結果を記録する
public class AsyncMyLogFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (actionDescriptor != null)
{
var attribute = actionDescriptor.MethodInfo.GetCustomAttribute<MyLogAttribute>();
if (attribute != null)
{
await next();
// This is where the magic is supposed to happen:
LoggerHelper.Log(context.ActionArguments, context.Result);
}
return;
}
await next();
}
}
フィルタはnext()
デリゲートを提供方法は、そのポイントを過ぎてアクションが完了されるだろうとObjectResult
として検査結果オブジェクトが利用できることを信じるように私を導きました。しかし、フィルタは問題なくActionArguments
を取得することができますが、残念なことに、このプロパティは役に立たないだけです。
明白な代替、同期IActionFilter
は、私がOnActionExecuted
ステージ上Result
オブジェクトを調べることができますが、この時点でActionArguments
辞書が利用できません。
同じ方法の範囲内でActionArguments
とResult
の両方にアクセスする方法はありますか?
-S
引数は 'ActionExecutedContext.ActionDescriptor.Parameters'では使用できませんか? –
Parametersコレクションにはパラメータの名前と型が含まれていますが、値は保持されていません。 –