背景
环境.net 4.5,做一个webapi筛选器,记录每个action请求的ip,和action运行时间
代码
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Http; using System.ServiceModel.Channels; using System.Web; using System.Web.Http.Controllers; using System.Web.Http.Filters; namespace DataWant.Filters { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class LogActionFilterAttribute : ActionFilterAttribute,IActionFilter { public LogActionFilterAttribute() { } private Stopwatch sw = new Stopwatch(); public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); sw.Restart(); } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { sw.Stop(); string ip = GetClientIp(actionExecutedContext.Request); var usageTime = (Int32)sw.Elapsed.TotalMilliseconds; //这里生产环境替换成,写入文件或数据库 Console.WriteLine("from:{0},usagetime:{1}",ip,usageTime); } private string GetClientIp(HttpRequestMessage request) { if (request.Properties.ContainsKey("MS_HttpContext")) { return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress; } else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop; prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]; return prop.Address; } else { return string.Empty; } } } }
异常
{"Message":"An error has occurred.","ExceptionMessage":"找不到方法:“System.Net.Http.HttpRequestMessage System.Web.Http.Filters.HttpActionExecutedContext.get_Request()”。","ExceptionType":"System.MissingMethodException","StackTrace":" 在 DataWant.Filters.LogActionFilterAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n 在 System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
我也出现这个问题,正在查,可能是某个dll没添加