Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 17e08ed

Browse files
committed
ITraceWriter example added to Demo project
based on TracingThrottleLogger by CraigKennedy
1 parent 83f9f93 commit 17e08ed

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

WebApiThrottle.Demo/App_Start/WebApiConfig.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Web.Http;
5+
using System.Web.Http.Tracing;
56
using WebApiThrottle.Demo.Helpers;
67

78
namespace WebApiThrottle.Demo
@@ -19,6 +20,14 @@ public static void Register(HttpConfiguration config)
1920
defaults: new { id = RouteParameter.Optional }
2021
);
2122

23+
//trace provider
24+
var traceWriter = new SystemDiagnosticsTraceWriter()
25+
{
26+
IsVerbose = true
27+
};
28+
config.Services.Replace(typeof(ITraceWriter), traceWriter);
29+
config.EnableSystemDiagnosticsTracing();
30+
2231
//Web API throttling
2332
config.MessageHandlers.Add(new ThrottlingHandler()
2433
{
@@ -53,7 +62,7 @@ public static void Register(HttpConfiguration config)
5362
}
5463
},
5564
Repository = new CacheRepository(),
56-
Logger = new CustomThrottleLogger()
65+
Logger = new TracingThrottleLogger(traceWriter)
5766
});
5867
}
5968
}

WebApiThrottle.Demo/WebApiThrottle.Demo.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
<Reference Include="System.ComponentModel.DataAnnotations" />
5959
<Reference Include="System.Core" />
6060
<Reference Include="System.Data.DataSetExtensions" />
61+
<Reference Include="System.Web.Http.Tracing">
62+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Tracing.5.0.0\lib\net45\System.Web.Http.Tracing.dll</HintPath>
63+
</Reference>
6164
<Reference Include="System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6265
<SpecificVersion>False</SpecificVersion>
6366
<HintPath>..\packages\Microsoft.AspNet.Web.Optimization.1.1.2\lib\net40\System.Web.Optimization.dll</HintPath>

WebApiThrottle.Demo/packages.config

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
1111
<package id="Microsoft.AspNet.WebApi.Core" version="5.0.0" targetFramework="net45" />
1212
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.0.0" targetFramework="net45" />
13+
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.0.0" targetFramework="net45" />
1314
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net45" />
1415
<package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
1516
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />

WebApiThrottle/TracingThrottleLogger.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public void Log(ThrottleLogEntry entry)
1515
{
1616
if (null != traceWriter)
1717
{
18-
traceWriter.Info(entry.Request, "WebApiThrottle", "{0} Request {1} to endpoint {2} from client {3} has been throttled (blocked), quota {4}/{5} exceeded by {6}",
19-
entry.LogDate, entry.RequestId, entry.ClientIp, entry.RateLimit,
20-
entry.Endpoint, entry.RateLimitPeriod, entry.TotalRequests);
18+
traceWriter.Info(entry.Request, "WebApiThrottle",
19+
"{0} Request {1} from {2} has been throttled (blocked), quota {3}/{4} exceeded by {5}",
20+
entry.LogDate, entry.RequestId, entry.ClientIp, entry.RateLimit, entry.RateLimitPeriod, entry.TotalRequests);
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)