Skip to content

Commit

Permalink
Add flag to prevent handling of dictionary if turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
sgryphon committed Jul 20, 2017
1 parent c9a599e commit 3a3029d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SeqTraceListener : TraceListenerBase
"batchTimeout", "BatchTimeout", "batchtimeout", "batchTimeOut", "BatchTimeOut",
"maxQueueSize", "MaxQueueSize", "maxqueuesize",
"maxRetries", "MaxRetries", "maxretries",
"processDictionaryData", "ProcessDictionaryData", "processdictionarydata",
};

/// <summary>
Expand Down Expand Up @@ -275,6 +276,26 @@ public int MaxRetries
}
}

/// <summary>
/// Gets or sets whether a single data of type IDictionary&lt;string,object&gt; is treated as structured data. Default is true.
/// </summary>
public bool ProcessDictionaryData
{
get
{
var processDictionaryData = true;
if (Attributes.ContainsKey("processDictionaryData"))
{
bool.TryParse(Attributes["processDictionaryData"], out processDictionaryData);
}
return processDictionaryData;
}
set
{
Attributes["processDictionaryData"] = value.ToString(CultureInfo.InvariantCulture);
}
}

/// <summary>
/// Allowed attributes for this trace listener.
/// </summary>
Expand Down Expand Up @@ -459,7 +480,8 @@ private TraceData CreateTraceData(TraceEventCache eventCache, string source, Tra
&& (messageArgs == null || messageArgs.Length == 0)
&& data != null
&& data.Length == 1
&& (data[0] is IStructuredData || data[0] is IDictionary<string, object>))
&& (data[0] is IStructuredData ||
(data[0] is IDictionary<string, object> && ProcessDictionaryData)))
{
var structuredData = (IDictionary<string, object>)data[0];
AddStructuredData(properties, structuredData, ref exception, ref messageFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ public void SeqStructuredRecursiveDictionaryShouldStop()
StringAssert.Contains(requestBody, "\"b\":{\"X\":2,\"Y\":{\"A\":1,\"B\":\"System.Collections.Generic.Dictionary`2[System.String,System.Object]\"}}");
}


[TestMethod]
public void SeqHandlesStructuredDictionary()
{
Expand Down Expand Up @@ -321,6 +320,36 @@ public void SeqHandlesStructuredDictionary()
StringAssert.DoesNotMatch(requestBody, regexData);
}

[TestMethod]
public void SeqIgnoresStructuredDictionaryWhenTurnedOff()
{
var mockRequestFactory = new MockHttpWebRequestFactory();
mockRequestFactory.ResponseQueue.Enqueue(
new MockHttpWebResponse(HttpStatusCode.OK, null)
);

var listener = new SeqTraceListener("http://testuri");
listener.BatchSize = 0;
listener.BatchSender.HttpWebRequestFactory = mockRequestFactory;
listener.ProcessDictionaryData = false;

var dictionaryData = new Dictionary<string, object>() {
{ "MessageTemplate", "{a}" },
{ "a", 1 }
};
listener.TraceData(null, "TestSource", TraceEventType.Warning, 1, dictionaryData);

Assert.AreEqual(1, mockRequestFactory.RequestsCreated.Count);

var request = mockRequestFactory.RequestsCreated[0];
var requestBody = request.RequestBody;
Console.WriteLine(requestBody);
var regexTemplateData = new Regex("\"MessageTemplate\":\"{Data}\"");
StringAssert.Matches(requestBody, regexTemplateData);
var regexData = new Regex("\"Data\":");
StringAssert.Matches(requestBody, regexData);
}

[TestMethod]
public void SeqStructuredCustomObject()
{
Expand Down

0 comments on commit 3a3029d

Please sign in to comment.