C#web api每个请求跟踪一个

时间:2014-07-16 01:46:58

标签: c# asp.net-web-api tracing

在C#Web API中是否有一种简单的方法来启用跟踪,但每个请求只有一个条目?此条目应包含有关请求的所有可用信息 - 也可能包含响应(如果可能的话)。

覆盖默认跟踪似乎生成的条目多于真正有用的条目,我只希望每个请求有一个条目。

编辑:

这是我们到目前为止所做的:

  • 在WebAPIConfig中 - >注册,添加了以下行:

    config.Services.Replace(typeof(ITraceWriter),new MyAppTraceWriter());

  • MyAppTraceWriter类具有以下成员和功能。 _tracer对象是一个Log4Net对象,只输出到txt文件:

        public static ILog _tracer;
    
    public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
    {
        _tracer = Logger.GetTracer();
    
        var rec = new TraceRecord(request, category, level);
        traceAction(rec);
        WriteTrace(rec);
    }
    
    protected void WriteTrace(TraceRecord rec)
    {
        var message = string.Format("{0};{1};{2};{3}", rec.Category, rec.Operator, rec.Operation, rec.Message);
    
        System.Diagnostics.Trace.WriteLine(message, rec.Category);
        _tracer.Info(message);
    }
    
    • 对特定资源执行一个HTTP DELETE方法会导致以下跟踪。对不起文本墙,但那里有32个条目。我希望每个请求只有一个条目,但是尽可能多地提供有关请求的信息,当前登录的用户,响应等。
  

跟踪内容:

2014-07-16 01:30:24,384 [7] INFO  DevTracer [(null)] - System.Web.Http.Request;;;http://server/api/v1/courses/24744/teachers/0000000711
2014-07-16 01:30:24,386 [7] INFO  DevTracer [(null)] - System.Web.Http.MessageHandlers;CorsMessageHandler;SendAsync;
2014-07-16 01:30:24,397 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;DefaultHttpControllerSelector;SelectController;Route='MS_SubRoutes:System.Web.Http.Routing.IHttpRouteData[]'
2014-07-16 01:30:24,420 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;DefaultHttpControllerSelector;SelectController;Courses
2014-07-16 01:30:24,442 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;HttpControllerDescriptor;CreateController;
2014-07-16 01:30:24,466 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;DefaultHttpControllerActivator;Create;
2014-07-16 01:30:24,520 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;DefaultHttpControllerActivator;Create;API.Controllers.CoursesController
2014-07-16 01:30:24,544 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;HttpControllerDescriptor;CreateController;API.Controllers.CoursesController
2014-07-16 01:30:24,548 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;CoursesController;ExecuteAsync;
2014-07-16 01:30:24,559 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ApiControllerActionSelector;SelectAction;
2014-07-16 01:30:24,581 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ApiControllerActionSelector;SelectAction;Selected action 'RemoveTeacherFromCourse(Int32 courseInstanceID, String ssn)'
2014-07-16 01:30:24,614 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;HttpActionBinding;ExecuteBindingAsync;
2014-07-16 01:30:24,620 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Binding parameter 'courseInstanceID'
2014-07-16 01:30:24,664 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Parameter 'courseInstanceID' bound to the value '24744'
2014-07-16 01:30:24,685 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Binding parameter 'ssn'
2014-07-16 01:30:24,689 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;ModelBinderParameterBinding;ExecuteBindingAsync;Parameter 'ssn' bound to the value '0000000711'
2014-07-16 01:30:24,692 [7] INFO  DevTracer [(null)] - System.Web.Http.ModelBinding;HttpActionBinding;ExecuteBindingAsync;Model state is valid. Values: courseInstanceID=24744, ssn=0000000711
2014-07-16 01:30:24,715 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ApiControllerActionInvoker;InvokeActionAsync;Action='RemoveTeacherFromCourse(courseInstanceID=24744, ssn=0000000711)'
2014-07-16 01:30:24,718 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ReflectedHttpActionDescriptor;ExecuteAsync;Invoking action 'RemoveTeacherFromCourse(courseInstanceID=24744, ssn=0000000711)'
2014-07-16 01:30:25,338 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ReflectedHttpActionDescriptor;ExecuteAsync;Action returned 'System.Web.Http.Results.OkResult'
2014-07-16 01:30:25,340 [7] INFO  DevTracer [(null)] - System.Web.Http.Action;ApiControllerActionInvoker;InvokeActionAsync;
2014-07-16 01:30:25,341 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;CoursesController;ExecuteAsync;
2014-07-16 01:30:25,371 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;AttributeBasedPolicyProviderFactory;GetCorsPolicyProvider;CorsRequestContext: 'Origin: http://server, HttpMethod: DELETE, IsPreflight: False, Host: server, AccessControlRequestMethod: null, RequestUri: http://server/api/v1/courses/24744/teachers/0000000711, AccessControlRequestHeaders: {}'
2014-07-16 01:30:25,375 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;AttributeBasedPolicyProviderFactory;GetCorsPolicyProvider;CorsPolicyProvider selected: 'System.Web.Http.Cors.EnableCorsAttribute'
2014-07-16 01:30:25,377 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;EnableCorsAttribute;GetCorsPolicyAsync;CorsRequestContext: 'Origin: http://server, HttpMethod: DELETE, IsPreflight: False, Host: server, AccessControlRequestMethod: null, RequestUri: http://server/api/v1/courses/24744/teachers/0000000711, AccessControlRequestHeaders: {}'
2014-07-16 01:30:25,382 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;EnableCorsAttribute;GetCorsPolicyAsync;CorsPolicy selected: 'AllowAnyHeader: True, AllowAnyMethod: True, AllowAnyOrigin: True, PreflightMaxAge: null, SupportsCredentials: False, Origins: {}, Methods: {}, Headers: {}, ExposedHeaders: {}'
2014-07-16 01:30:25,384 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;CorsEngine;EvaluatePolicy;
2014-07-16 01:30:25,389 [7] INFO  DevTracer [(null)] - System.Web.Http.Cors;CorsEngine;EvaluatePolicy;CorsResult returned: 'IsValid: True, AllowCredentials: False, PreflightMaxAge: null, AllowOrigin: *, AllowExposedHeaders: {}, AllowHeaders: {}, AllowMethods: {}, ErrorMessages: {}'
2014-07-16 01:30:25,392 [7] INFO  DevTracer [(null)] - System.Web.Http.MessageHandlers;CorsMessageHandler;SendAsync;
2014-07-16 01:30:25,393 [7] INFO  DevTracer [(null)] - System.Web.Http.Request;;;Content-type='none', content-length=unknown
2014-07-16 01:30:25,403 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;CoursesController;Dispose;
2014-07-16 01:30:25,404 [7] INFO  DevTracer [(null)] - System.Web.Http.Controllers;CoursesController;Dispose;

0 个答案:

没有答案