如何使用 NLog 跟踪每个请求 ASP.NET Web API
How to trace each request ASP.NET Web API with NLog
我用 ASP.NET Web API 创建了一个简单的 REST API。
出于测试目的,我想添加一些跟踪。所以我将 NLog 添加到我的项目中。此时我的日志是这样的:
// POST api/values
public void Post([FromBody]string value)
{
logger.Trace("Request: {0} api/values", Request.Method);
_repository.insert(value);
logger.Trace("Response: {0} api/values", Request.Method);
}
在每个方法中,我在方法的顶部和底部添加了一个 logger.Trace。我对这个方法有 2 个问题:
- 我需要记住将这些行添加到我的每个方法中
- 我不知道如何将 JSON body 添加到我的追踪
第 1 点目前不是真正的问题(见下文),但我很快需要一些东西来检查每个 JSON body 我的 API 收到的东西。
我已经试过了
// POST api/values
public void Post([FromBody]string value)
{
logger.Trace("Request: {0} api/values {1}", Request.Method, Request.Body);
_repository.insert(value);
logger.Trace("Response: {0} api/values", Request.Method);
}
但是没有Body属性请求。
我还为我的观点 1 找到了一个有趣的文档:http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi
这就是你有动作过滤器的目的...做某事 before/after 动作方法是 executing/executed
public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//Do something here before an action method starts executing
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
{
//Do something here after an action method finished executing
}
}
然后,您需要在 asp.net 管道中插入此过滤器...当应用程序启动时,无论您使用 owin/katana 还是 global.asax 都没有关系...
GlobalConfiguration.Configuration.Filters.Add(new MyCustomFilter());
上面的行会将过滤器添加到所有操作方法中。如果您想关闭某些操作方法的跟踪,只需将 flag/switch 属性 添加到操作过滤器,以便您可以关闭某些操作的跟踪...
public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public bool DisableTracing{get;set;}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(!DisableTracing){
//Do something here before an action method starts executing
}
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
{
if(!DisableTracing){
//Do something here before an action method starts executing
}
}
}
现在您可以在控制器操作中关闭它...
[MyCustomFilter(DisableTracing = true)]
public IHttpActionResult MyAction(int id){}
更新
要从请求正文中读取 JSON 对象,只需读取请求的内容如下...
request.Content.ReadAsStringAsync().Result;
Leo 的解决方案对于 MVC 似乎是正确的,但对于 Http REST API 我不得不实施 http://www.c-sharpcorner.com/UploadFile/1492b1/restful-day-sharp6-request-logging-and-exception-handingloggin/
的解决方案
public class HttpLoggingFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
{
//Do something here
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
//Do something here
}
}
在我的代码上测试这两种方法后,我可以看出 Leo 的代码是在页面刷新位上执行的,而不是在简单的 REST 请求上执行的。
我用 ASP.NET Web API 创建了一个简单的 REST API。 出于测试目的,我想添加一些跟踪。所以我将 NLog 添加到我的项目中。此时我的日志是这样的:
// POST api/values
public void Post([FromBody]string value)
{
logger.Trace("Request: {0} api/values", Request.Method);
_repository.insert(value);
logger.Trace("Response: {0} api/values", Request.Method);
}
在每个方法中,我在方法的顶部和底部添加了一个 logger.Trace。我对这个方法有 2 个问题:
- 我需要记住将这些行添加到我的每个方法中
- 我不知道如何将 JSON body 添加到我的追踪
第 1 点目前不是真正的问题(见下文),但我很快需要一些东西来检查每个 JSON body 我的 API 收到的东西。
我已经试过了
// POST api/values
public void Post([FromBody]string value)
{
logger.Trace("Request: {0} api/values {1}", Request.Method, Request.Body);
_repository.insert(value);
logger.Trace("Response: {0} api/values", Request.Method);
}
但是没有Body属性请求。
我还为我的观点 1 找到了一个有趣的文档:http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi
这就是你有动作过滤器的目的...做某事 before/after 动作方法是 executing/executed
public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//Do something here before an action method starts executing
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
{
//Do something here after an action method finished executing
}
}
然后,您需要在 asp.net 管道中插入此过滤器...当应用程序启动时,无论您使用 owin/katana 还是 global.asax 都没有关系...
GlobalConfiguration.Configuration.Filters.Add(new MyCustomFilter());
上面的行会将过滤器添加到所有操作方法中。如果您想关闭某些操作方法的跟踪,只需将 flag/switch 属性 添加到操作过滤器,以便您可以关闭某些操作的跟踪...
public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public bool DisableTracing{get;set;}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if(!DisableTracing){
//Do something here before an action method starts executing
}
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
{
if(!DisableTracing){
//Do something here before an action method starts executing
}
}
}
现在您可以在控制器操作中关闭它...
[MyCustomFilter(DisableTracing = true)]
public IHttpActionResult MyAction(int id){}
更新
要从请求正文中读取 JSON 对象,只需读取请求的内容如下...
request.Content.ReadAsStringAsync().Result;
Leo 的解决方案对于 MVC 似乎是正确的,但对于 Http REST API 我不得不实施 http://www.c-sharpcorner.com/UploadFile/1492b1/restful-day-sharp6-request-logging-and-exception-handingloggin/
的解决方案public class HttpLoggingFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
{
//Do something here
}
public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
//Do something here
}
}
在我的代码上测试这两种方法后,我可以看出 Leo 的代码是在页面刷新位上执行的,而不是在简单的 REST 请求上执行的。