Skip to content
Browse files

Add HttpConfigurationExtensions.EnableTracing to System.Web.Http.Trac…

…ing.dll
  • Loading branch information...
1 parent ff3c3ba commit 3c7f7091bce2459d25ad3da8e1811b4e5e29ef47 phenning committed Nov 9, 2012
View
2 src/System.Web.Http.Tracing/GlobalSuppressions.cs
@@ -22,3 +22,5 @@
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "System.Web.Http.Error.#Argument(System.String,System.String,System.Object[])", Justification = "Error imported from main project")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "System.Web.Http.Error.#Argument(System.String,System.Object[])", Justification = "Error imported from main project")]
[assembly: SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope = "member", Target = "System.Web.Http.Error.#ArgumentUriNotAbsolute(System.String,System.Uri)", Justification = "Error imported from main project")]
+[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Web.Http", Justification = "Small extension project, distinct namespace")]
+[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Web.Http.Tracing", Justification = "Small extension project, distinct namespace")]
View
42 src/System.Web.Http.Tracing/HttpConfigurationExtensions.cs
@@ -0,0 +1,42 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.ComponentModel;
+using System.Web.Http.Tracing;
+
+namespace System.Web.Http
+{
+ /// <summary>
+ /// This static class contains helper methods related to the registration
+ /// of <see cref="ITraceWriter"/> instances.
+ /// </summary>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public static class HttpConfigurationTracingExtensions
+ {
+ /// <summary>
+ /// Creates and registers an <see cref="ITraceWriter"/> implementation to use
+ /// for this application.
+ /// </summary>
+ /// <param name="configuration">The <see cref="HttpConfiguration"/> for which
+ /// to register the created trace writer.</param>
+ /// <remarks>The returned SystemDiagnosticsTraceWriter may be further configured to change it's default settings.</remarks>
+ /// <returns>The <see cref="SystemDiagnosticsTraceWriter"/> which was created and registered.</returns>
+ public static SystemDiagnosticsTraceWriter EnableSystemDiagnosticsTracing(this HttpConfiguration configuration)
+ {
+ if (configuration == null)
+ {
+ throw new ArgumentNullException("configuration");
+ }
+
+ SystemDiagnosticsTraceWriter traceWriter =
+ new SystemDiagnosticsTraceWriter()
+ {
+ MinimumLevel = TraceLevel.Info,
+ IsVerbose = false
+ };
+
+ configuration.Services.Replace(typeof(ITraceWriter), traceWriter);
+
+ return traceWriter;
+ }
+ }
+}
View
1 src/System.Web.Http.Tracing/System.Web.Http.Tracing.csproj
@@ -92,6 +92,7 @@
<Link>Common\Error.cs</Link>
</Compile>
<Compile Include="GlobalSuppressions.cs" />
+ <Compile Include="HttpConfigurationExtensions.cs" />
<Compile Include="SystemDiagnosticsTraceWriter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\SRResources.Designer.cs">
View
26 test/System.Web.Http.Tracing.Test/HttpConfigurationTracingExtensionsTest.cs
@@ -0,0 +1,26 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.Web.Http.Tracing;
+using Microsoft.TestCommon;
+
+namespace System.Web.Http.Test
+{
+ public class HttpConfigurationTracingExtensionsTest
+ {
+ [Fact]
+ public void EnableSystemDiagnosticsTracing_Adds_TraceWriter()
+ {
+ HttpConfiguration config = new HttpConfiguration();
+ SystemDiagnosticsTraceWriter returnedTraceWriter = config.EnableSystemDiagnosticsTracing();
+ ITraceWriter setTraceWriter = config.Services.GetService(typeof(ITraceWriter)) as ITraceWriter;
+
+ Assert.ReferenceEquals(returnedTraceWriter, setTraceWriter);
+ }
+
+ [Fact]
+ public void EnableSystemDiagnosticsTracing_ThrowsIfNull()
+ {
+ Assert.ThrowsArgumentNull(() => HttpConfigurationTracingExtensions.EnableSystemDiagnosticsTracing(null), "configuration");
+ }
+ }
+}
View
1 test/System.Web.Http.Tracing.Test/System.Web.Http.Tracing.Test.csproj
@@ -84,6 +84,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="HttpConfigurationTracingExtensionsTest.cs" />
<Compile Include="SystemDiagnosticsTraceWriterTest.cs" />
</ItemGroup>
<ItemGroup>
View
2 test/System.Web.Http.Tracing.Test/SystemDiagnosticsTraceWriterTest.cs
@@ -1040,7 +1040,7 @@ public void TranslateHttpResponseException_Unpacks_HttpError_With_No_Fields_Set(
Assert.Equal(TraceLevel.Warn, traceRecord.Level);
Assert.Equal(string.Empty, traceRecord.Message);
}
-
+
private static void AssertContainsExactly(string trace, IDictionary<string, string> expected)
{
IDictionary<string, string> actual = ParseTrace(trace);

0 comments on commit 3c7f709

Please sign in to comment.
Something went wrong with that request. Please try again.