lenmom

博客园 首页 新随笔 联系 订阅 管理

1. 通过Nuget安装System.Web.Http.Tracing.

 

2. 通过HttpConfiguration,注册SystemDiagnosticsTraceWriter

 

        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Filters.Add(new ValidateModelStateFilter());
            FluentValidationModelValidatorProvider.Configure(config);

            //regular registration
            config.Services.Replace(typeof(ITraceWriter), new SystemDiagnosticsTraceWriter());
            //through an extension method
            config.EnableSystemDiagnosticsTracing();
        }

3. 运行,即可看到整个运行过程的信息日志信息,通过日志信息可以窥探WebApi的运行内核信息。

发送请求:

 输出信息为:

iisexpress.exe Information: 0 : Request, Method=POST, Url=http://localhost:52187/api/default/, Message='http://localhost:52187/api/default/'
“iisexpress.exe”(CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131659877920590491): 已加载“C:\Users\lenmo\AppData\Local\Temp\Temporary ASP.NET Files\vs\89abac48\79d00737\assembly\dl3\a74c6d75\00e4889b_139bd301\System.Web.Http.resources.dll”。模块已生成,不包含符号。
iisexpress.exe Information: 0 : Message='Default', Operation=DefaultHttpControllerSelector.SelectController
iisexpress.exe Information: 0 : Message='WebApplication1.Controllers.DefaultController', Operation=DefaultHttpControllerActivator.Create
iisexpress.exe Information: 0 : Message='WebApplication1.Controllers.DefaultController', Operation=HttpControllerDescriptor.CreateController
iisexpress.exe Information: 0 : Message='所选操作“Post(UserModel user)”', Operation=ApiControllerActionSelector.SelectAction
“iisexpress.exe”(CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131659877920590491): 已加载“C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
iisexpress.exe Information: 0 : Message='读取的值=“WebApplication1.Controllers.UserModel”', Operation=JsonMediaTypeFormatter.ReadFromStreamAsync
iisexpress.exe Information: 0 : Message='参数“user”已绑定到值“WebApplication1.Controllers.UserModel”', Operation=FormatterParameterBinding.ExecuteBindingAsync
iisexpress.exe Information: 0 : Message='模型状态无效。user.Password: Please specify a first name', Operation=HttpActionBinding.ExecuteBindingAsync
iisexpress.exe Information: 0 : Message='将使用同一“JsonMediaTypeFormatter”格式化程序', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe Information: 0 : Message='所选格式化程序=“JsonMediaTypeFormatter”,content-type=“application/json; charset=utf-8”', Operation=DefaultContentNegotiator.Negotiate
iisexpress.exe Information: 0 : Operation=ValidateModelStateFilter.OnActionExecutingAsync, Status=400 (BadRequest)
iisexpress.exe Information: 0 : Operation=DefaultController.ExecuteAsync, Status=400 (BadRequest)
iisexpress.exe Information: 0 : Response, Status=400 (BadRequest), Method=POST, Url=http://localhost:52187/api/default/, Message='Content-type=“application/json; charset=utf-8”,content-length=未知'
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync
iisexpress.exe Information: 0 : Operation=DefaultController.Dispose

 

注意: 通过示例注册代码,默认将打印所有的运行日志,如果希望对日志进行筛选,可以通过以下方式:

            //regular registration
            config.Services.Replace(typeof(ITraceWriter), new SystemDiagnosticsTraceWriter() {   MinimumLevel= TraceLevel.Error});
            //through an extension method
            config.EnableSystemDiagnosticsTracing();

默认的注册方式为: Info,即打印所有信息出来。TraceLevel的定义为:

  //
    // 摘要:
    //     指定跟踪级别的枚举。
    public enum TraceLevel
    {
        //
        // 摘要:
        //     已禁用跟踪。
        Off = 0,
        //
        // 摘要:
        //     调试跟踪的跟踪级别。
        Debug = 1,
        //
        // 摘要:
        //     信息跟踪的跟踪级别。
        Info = 2,
        //
        // 摘要:
        //     警告跟踪的跟踪级别。
        Warn = 3,
        //
        // 摘要:
        //     错误跟踪的跟踪级别。
        Error = 4,
        //
        // 摘要:
        //     严重跟踪的跟踪级别。
        Fatal = 5
    }

 

也可以使用第三方的日志组件用于输出运行时信息:如

WebApiContrib.Tracing.Log4Net
WebApiContrib.Tracing.Nlog

安装方法,Nuget:

install-package WebApiContrib.Tracing.Nlog
install-package WebApiContrib.Tracing.Log4Net

注册代码类似:

httpConfiguration.Services.Replace(typeof(ITraceWriter), new NlogTraceWriter());

 

posted on 2018-03-20 10:58  老董  阅读(2226)  评论(0编辑  收藏  举报