Skip to content

Commit 5dfff3c

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

File tree

10 files changed

+141
-44
lines changed

10 files changed

+141
-44
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/MiniProfilerResultsHeader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static byte[] Write(MiniProfilerResultsHeader header)
8787
Array.Resize(ref buffer, (int)stream.Length);
8888
return buffer;
8989
}
90-
}
90+
}
9191
}
9292

9393
/// <summary>
@@ -104,9 +104,9 @@ private static byte[] Compress(byte[] buffer)
104104
{
105105
stream.CopyTo(tinyStream);
106106
}
107-
107+
108108
return outStream.ToArray();
109-
}
109+
}
110110
}
111111

112112
/// <summary>
@@ -125,4 +125,4 @@ private static byte[] Decompress(byte[] buffer)
125125
}
126126
}
127127
}
128-
}
128+
}

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/MiniProfiler.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace StackExchange.Profiling
1717
public partial class MiniProfiler
1818
{
1919
/// <summary>
20-
/// Initialises a new instance of the <see cref="MiniProfiler"/> class.
20+
/// Initialises a new instance of the <see cref="MiniProfiler"/> class.
2121
/// Obsolete - used for serialization.
2222
/// </summary>
2323
[Obsolete("Used for serialization")]
@@ -26,7 +26,7 @@ public MiniProfiler()
2626
}
2727

2828
/// <summary>
29-
/// Initialises a new instance of the <see cref="MiniProfiler"/> class. Creates and starts a new MiniProfiler
29+
/// Initialises a new instance of the <see cref="MiniProfiler"/> class. Creates and starts a new MiniProfiler
3030
/// for the root <paramref name="url"/>.
3131
/// </summary>
3232
public MiniProfiler(string url)
@@ -45,7 +45,7 @@ public MiniProfiler(string url)
4545
}
4646

4747
/// <summary>
48-
/// Initialises a new instance of the <see cref="MiniProfiler"/> class. Creates and starts a new MiniProfiler
48+
/// Initialises a new instance of the <see cref="MiniProfiler"/> class. Creates and starts a new MiniProfiler
4949
/// for the root <paramref name="url"/>, filtering <see cref="Timing"/> steps to <paramref name="level"/>.
5050
/// </summary>
5151
[Obsolete("Please use the MiniProfiler(string url) constructor instead of this one. ProfileLevel is going away")]
@@ -112,17 +112,19 @@ public MiniProfiler(string url, ProfileLevel level = ProfileLevel.Info) : this(u
112112
/// Json used to store Custom Links. Do not touch.
113113
/// </summary>
114114
[ScriptIgnore]
115-
public string CustomLinksJson {
116-
get { return CustomLinks != null ? CustomLinks.ToJson() : null; }
117-
set {
115+
public string CustomLinksJson
116+
{
117+
get { return CustomLinks != null ? CustomLinks.ToJson() : null; }
118+
set
119+
{
118120
if (value.HasValue())
119121
{
120122
CustomLinks = value.FromJson<Dictionary<string, string>>();
121123
}
122-
}
124+
}
123125
}
124-
125-
/// <summary>
126+
127+
/// <summary>
126128
/// Gets or sets the root timing.
127129
/// The first <see cref="Timing"/> that is created and started when this profiler is instantiated.
128130
/// All other <see cref="Timing"/>s will be children of this one.
@@ -159,7 +161,7 @@ public Timing Root
159161
for (int i = children.Count - 1; i >= 0; i--)
160162
{
161163
children[i].ParentTiming = timing;
162-
timings.Push(children[i]); // FLORIDA! TODO: refactor this and other stack creation methods into one
164+
timings.Push(children[i]); // FLORIDA! TODO: refactor this and other stack creation methods into one
163165
}
164166
}
165167
}
@@ -207,15 +209,15 @@ public Timing Root
207209
public bool HasUserViewed { get; set; }
208210

209211
/// <summary>
210-
/// Gets or sets whether or not filtering is allowed of <see cref="Timing"/> steps based on what <see cref="ProfileLevel"/>
212+
/// Gets or sets whether or not filtering is allowed of <see cref="Timing"/> steps based on what <see cref="ProfileLevel"/>
211213
/// the steps are created with.
212214
/// </summary>
213215
[Obsolete("If you don't want this removed, speak up at https://github.com/MiniProfiler/dotnet")]
214216
[ScriptIgnore]
215217
public ProfileLevel Level { get; set; }
216218

217219
/// <summary>
218-
/// Gets or sets points to the currently executing Timing.
220+
/// Gets or sets points to the currently executing Timing.
219221
/// </summary>
220222
[ScriptIgnore]
221223
public Timing Head { get; set; }
@@ -235,7 +237,7 @@ internal IStopwatch Stopwatch
235237
{
236238
get { return _sw; }
237239
}
238-
240+
239241
/// <summary>
240242
/// Gets the currently running MiniProfiler for the current HttpContext; null if no MiniProfiler was <see cref="Start(string)"/>ed.
241243
/// </summary>
@@ -249,7 +251,7 @@ public static MiniProfiler Current
249251
}
250252

251253
/// <summary>
252-
/// A <see cref="IStorage"/> strategy to use for the current profiler.
254+
/// A <see cref="IStorage"/> strategy to use for the current profiler.
253255
/// If null, then the <see cref="IStorage"/> set in <see cref="MiniProfiler.Settings.Storage"/> will be used.
254256
/// </summary>
255257
/// <remarks>Used to set custom storage for an individual request</remarks>
@@ -261,7 +263,7 @@ public static MiniProfiler Current
261263
/// </summary>
262264
public static MiniProfiler Start()
263265
{
264-
// ToDo - overloading the method too many times - will correct when delete the Obsolete version.
266+
// ToDo - overloading the method too many times - will correct when delete the Obsolete version.
265267
// Until then need it this way to prevent ambiguity
266268
return Start(null);
267269
}
@@ -300,7 +302,7 @@ public static MiniProfiler Start(ProfileLevel level, string sessionName = null)
300302
/// Ends the current profiling session, if one exists.
301303
/// </summary>
302304
/// <param name="discardResults">
303-
/// When true, clears the <see cref="MiniProfiler.Current"/> for this HttpContext, allowing profiling to
305+
/// When true, clears the <see cref="MiniProfiler.Current"/> for this HttpContext, allowing profiling to
304306
/// be prematurely stopped and discarded. Useful for when a specific route does not need to be profiled.
305307
/// </param>
306308
public static void Stop(bool discardResults = false)
@@ -359,7 +361,7 @@ public static MiniProfiler FromJson(string json)
359361

360362
private static JavaScriptSerializer GetJsonSerializer()
361363
{
362-
return new JavaScriptSerializer { MaxJsonLength = Settings.MaxJsonResponseSize };
364+
return new JavaScriptSerializer { MaxJsonLength = Settings.MaxJsonResponseSize };
363365
}
364366

365367
/// <summary>
@@ -377,22 +379,22 @@ private static JavaScriptSerializer GetJsonSerializer()
377379
/// <param name="startHidden">Should the profiler start as hidden. Default to null.</param>
378380
/// <returns>Script and link elements normally; an empty string when there is no active profiling session.</returns>
379381
public static IHtmlString RenderIncludes(
380-
RenderPosition? position = null,
381-
bool? showTrivial = null,
382-
bool? showTimeWithChildren = null,
383-
int? maxTracesToShow = null,
382+
RenderPosition? position = null,
383+
bool? showTrivial = null,
384+
bool? showTimeWithChildren = null,
385+
int? maxTracesToShow = null,
384386
bool? showControls = null,
385387
bool? useExistingjQuery = null, // TODO: we need to deprecate this
386388
bool samplingOnly = false, // TODO: can we remove this?
387389
bool? startHidden = null)
388390
{
389391
return MiniProfilerHandler.RenderIncludes(
390-
Current,
391-
position,
392-
showTrivial,
393-
showTimeWithChildren,
394-
maxTracesToShow,
395-
showControls,
392+
Current,
393+
position,
394+
showTrivial,
395+
showTimeWithChildren,
396+
maxTracesToShow,
397+
showControls,
396398
startHidden);
397399
}
398400

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)