Skip to content

Commit a3cc9a5

Browse files
server/logging: Added option to not log sql command string. (VIV-3681)
1 parent 042e4c3 commit a3cc9a5

File tree

7 files changed

+91
-5
lines changed

7 files changed

+91
-5
lines changed

MiniProfiler.WCF.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>MiniProfiler.WCF</id>
5-
<version>3.0.12.1-wcffix-006</version>
5+
<version>3.0.12.1-wcffix-009</version>
66
<authors>Marc Gravell, Sam Saffron, Jarrod Dixon</authors>
77
<owners>Marc Gravell, Sam Saffron, Jarrod Dixon</owners>
88
<description>MiniProfiler integration for WCF</description>

StackExchange.Profiling.Tests/StackExchange.Profiling.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<HintPath>..\packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll</HintPath>
9595
</Reference>
9696
<Reference Include="System.Runtime.Serialization" />
97+
<Reference Include="System.ServiceModel" />
9798
<Reference Include="System.Transactions" />
9899
<Reference Include="System.Web" />
99100
<Reference Include="System.Xml" />
@@ -122,6 +123,7 @@
122123
<Compile Include="Storage\MultiStorageProviderTests.cs" />
123124
<Compile Include="Storage\TestHttpRuntimeCacheStorage.cs" />
124125
<Compile Include="UnitTestStopwatch.cs" />
126+
<Compile Include="WCF\WcfMiniProfilerDispatchInspectorTest.cs" />
125127
</ItemGroup>
126128
<ItemGroup>
127129
<None Include="App.config" />
@@ -138,11 +140,16 @@
138140
<Project>{7F18DC76-61A2-4E7D-BA5A-FE159E789362}</Project>
139141
<Name>StackExchange.Profiling.EntityFramework</Name>
140142
</ProjectReference>
143+
<ProjectReference Include="..\StackExchange.Profiling.Wcf\StackExchange.Profiling.Wcf.csproj">
144+
<Project>{c471b0e5-0ae4-48e0-a938-02aeda030c5e}</Project>
145+
<Name>StackExchange.Profiling.Wcf</Name>
146+
</ProjectReference>
141147
<ProjectReference Include="..\StackExchange.Profiling\StackExchange.Profiling.csproj">
142148
<Project>{386222BD-6B6E-480F-A342-8DE1AB516E2C}</Project>
143149
<Name>StackExchange.Profiling</Name>
144150
</ProjectReference>
145151
</ItemGroup>
152+
<ItemGroup />
146153
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
147154
<PropertyGroup>
148155
<PostBuildEvent>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.ServiceModel;
5+
using System.ServiceModel.Channels;
6+
using System.Text;
7+
using NUnit.Framework;
8+
using StackExchange.Profiling.Wcf;
9+
10+
namespace StackExchange.Profiling.Tests.WCF
11+
{
12+
[TestFixture]
13+
public class WcfMiniProfilerDispatchInspectorTest
14+
{
15+
[Test]
16+
public void WillSuppressSQlCommandTextInHeader()
17+
{
18+
MiniProfiler.Settings.ProfilerProvider = new SingletonProfilerProvider();
19+
var profiler = MiniProfiler.Start(nameof(this.WillSuppressSQlCommandTextInHeader));
20+
profiler.Root.AddCustomTiming("sql",
21+
new CustomTiming(profiler, nameof(this.WillSuppressSQlCommandTextInHeader), 5));
22+
23+
var message = Message.CreateMessage(MessageVersion.Soap12, nameof(this.WillSuppressSQlCommandTextInHeader));
24+
25+
var wcfMiniProfilerDispatchInspector = new WcfMiniProfilerDispatchInspector(false);
26+
wcfMiniProfilerDispatchInspector.BeforeSendReply(ref message, new MiniProfilerRequestHeader());
27+
28+
var resultHeader = message.Headers.GetHeader<MiniProfilerResultsHeader>(MiniProfilerResultsHeader.HeaderName,
29+
MiniProfilerResultsHeader.HeaderNamespace);
30+
Assert.AreEqual(0, resultHeader.ProfilerResults.Root.CustomTimings.Count());
31+
}
32+
33+
[Test]
34+
public void WillSerializeProfilerWithoutSqlCommandText()
35+
{
36+
MiniProfiler.Settings.ProfilerProvider = new SingletonProfilerProvider();
37+
var profiler = MiniProfiler.Start(nameof(this.WillSuppressSQlCommandTextInHeader));
38+
39+
var message = Message.CreateMessage(MessageVersion.Soap12, nameof(this.WillSuppressSQlCommandTextInHeader));
40+
41+
var wcfMiniProfilerDispatchInspector = new WcfMiniProfilerDispatchInspector(false);
42+
wcfMiniProfilerDispatchInspector.BeforeSendReply(ref message, new MiniProfilerRequestHeader());
43+
44+
var resultHeader = message.Headers.GetHeader<MiniProfilerResultsHeader>(MiniProfilerResultsHeader.HeaderName,
45+
MiniProfilerResultsHeader.HeaderNamespace);
46+
Assert.IsNull(resultHeader.ProfilerResults.Root.CustomTimings);
47+
}
48+
}
49+
}

StackExchange.Profiling.Wcf/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("a95b0bf2-e9cf-4029-b77a-85b9e66fd3d5")]
1414
[assembly: AssemblyVersion("3.0.12.1")]
15-
[assembly: AssemblyInformationalVersion("3.0.12.1-wcffix-006")]
15+
[assembly: AssemblyInformationalVersion("3.0.12.1-wcffix-009")]

StackExchange.Profiling.Wcf/WcfMiniProfilerBehavior.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace StackExchange.Profiling.Wcf
22
{
33
using System;
4+
using System.Configuration;
45
using System.ServiceModel.Configuration;
56
using System.ServiceModel.Description;
67

@@ -9,6 +10,21 @@
910
/// </summary>
1011
public class WcfMiniProfilerBehavior : BehaviorExtensionElement, IEndpointBehavior
1112
{
13+
[ConfigurationProperty("logSql")]
14+
public bool LogSqlCommandText
15+
{
16+
get
17+
{
18+
if (bool.TryParse(this["logSql"]?.ToString(), out var val))
19+
{
20+
return val;
21+
}
22+
return false;
23+
}
24+
25+
set => this["logSql"] = value;
26+
}
27+
1228
/// <summary>
1329
/// Gets the behaviour type.
1430
/// </summary>
@@ -44,7 +60,7 @@ public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Di
4460
/// <param name="endpointDispatcher">The endpoint dispatcher.</param>
4561
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
4662
{
47-
var inspector = new WcfMiniProfilerDispatchInspector();
63+
var inspector = new WcfMiniProfilerDispatchInspector(this.LogSqlCommandText);
4864
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
4965
}
5066

@@ -65,4 +81,4 @@ protected override object CreateBehavior()
6581
return this;
6682
}
6783
}
68-
}
84+
}

StackExchange.Profiling.Wcf/WcfMiniProfilerDispatchInspector.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
/// </summary>
1313
public class WcfMiniProfilerDispatchInspector : IDispatchMessageInspector
1414
{
15+
protected readonly bool _logSqlCommandText;
16+
1517
/// <summary>
1618
/// true if the binding is using http.
1719
/// </summary>
1820
private bool _http;
1921

22+
public WcfMiniProfilerDispatchInspector(bool logSqlCommandText)
23+
{
24+
this._logSqlCommandText = logSqlCommandText;
25+
}
26+
2027
/// <summary>
2128
/// after the request is received.
2229
/// </summary>
@@ -86,6 +93,13 @@ public void BeforeSendReply(ref Message reply, object correlationState)
8693
miniProfiler.Root.RemoveTrivialTimings();
8794
}
8895

96+
if ((false == this._logSqlCommandText) && (null != miniProfiler.Root.CustomTimings)
97+
&& (miniProfiler.Root.CustomTimings.ContainsKey("sql")))
98+
{
99+
miniProfiler = miniProfiler.Clone();
100+
miniProfiler.Root.CustomTimings.Remove("sql");
101+
}
102+
89103
var header = new MiniProfilerResultsHeader
90104
{
91105
ProfilerResults = miniProfiler

StackExchange.Profiling/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("f41f8a8f-55ef-44ac-beaf-d6fa475939a7")]
1414
[assembly: AssemblyVersion("3.2.1.1")]
15-
[assembly: AssemblyInformationalVersion("3.2.1.1-wcffix-004")]
15+
[assembly: AssemblyInformationalVersion("3.2.1.1-wcffix-004-john")]
1616
[assembly: InternalsVisibleTo("StackExchange.Profiling.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001001172d8b313c5125b3503ab4656bc2c04305498b2c75560f2e4fdc70e22fd098b9fdce619c318b5fb92670406d2122c9912ee718cb24544f69b7a92a825bd6e439bf1b51cf8e32be536954881c067a81e3da26ca348f7a17de7dfc07ae6df14ce132a7cfcb6d5f53f939bc9f623ec0e5ff9edab439c04a9b8bf8c110ebe8908be")]

0 commit comments

Comments
 (0)