From b68da9c1abad26a2f0da4312820e2cdf5c619850 Mon Sep 17 00:00:00 2001 From: Karol Zadora-Przylecki Date: Wed, 19 Apr 2017 11:28:19 -0700 Subject: [PATCH] Ensure that dates are sent in ISO 8601 format --- .../EventDataExtensions.cs | 6 +++- ...gnostics.EventFlow.Outputs.EventHub.csproj | 2 +- .../Properties/AssemblyInfo.cs | 2 ++ .../EventHubOutputTests.cs | 35 +++++++++++++++++++ ...Diagnostics.EventFlow.Outputs.Tests.csproj | 6 ++++ .../StdOutputTests.cs | 7 +++- 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/EventHubOutputTests.cs diff --git a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/EventDataExtensions.cs b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/EventDataExtensions.cs index 65491e62..711dd345 100644 --- a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/EventDataExtensions.cs +++ b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/EventDataExtensions.cs @@ -39,7 +39,11 @@ internal class ShoeBoxEventData public List records = new List(); } - private static readonly JsonSerializerSettings serializerSetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }; + private static readonly JsonSerializerSettings serializerSetting = new JsonSerializerSettings() + { + NullValueHandling = NullValueHandling.Ignore, + DateFormatHandling = DateFormatHandling.IsoDateFormat + }; public static MessagingEventData ToMessagingEventData(this EventData eventData, out int messageSize) { diff --git a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Microsoft.Diagnostics.EventFlow.Outputs.EventHub.csproj b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Microsoft.Diagnostics.EventFlow.Outputs.EventHub.csproj index 5aad9c8d..3a75dbc1 100644 --- a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Microsoft.Diagnostics.EventFlow.Outputs.EventHub.csproj +++ b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Microsoft.Diagnostics.EventFlow.Outputs.EventHub.csproj @@ -9,7 +9,7 @@ Microsoft.Diagnostics.EventFlow.Outputs.EventHub ../../PublicKey.snk true - 1.1.0 + 1.1.1 true Microsoft.Diagnostics.EventFlow.Outputs.EventHub Microsoft;Diagnostics;EventFlow;Outputs;Event Hub;Event Hubs diff --git a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Properties/AssemblyInfo.cs b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Properties/AssemblyInfo.cs index e3699dca..d57fde50 100644 --- a/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Properties/AssemblyInfo.cs +++ b/src/Microsoft.Diagnostics.EventFlow.Outputs.EventHub/Properties/AssemblyInfo.cs @@ -17,3 +17,5 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("7072db92-0fda-4b87-abfd-897882c9284a")] + +[assembly: InternalsVisibleTo("Microsoft.Diagnostics.EventFlow.Outputs.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")] diff --git a/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/EventHubOutputTests.cs b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/EventHubOutputTests.cs new file mode 100644 index 00000000..4faf9eb8 --- /dev/null +++ b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/EventHubOutputTests.cs @@ -0,0 +1,35 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. +// ------------------------------------------------------------ + +using System; +using System.IO; +using System.Text.RegularExpressions; +using Xunit; + +namespace Microsoft.Diagnostics.EventFlow.Outputs.Tests +{ +#if NET46 + public class EventHubOutputTests + { + [Fact] + public void UsesIsoDateFormat() + { + EventData e = new EventData(); + e.Payload.Add("DateTimeProperty", new DateTime(2017, 4, 19, 10, 15, 23, DateTimeKind.Utc)); + e.Payload.Add("DateTimeOffsetProperty", new DateTimeOffset(2017, 4, 19, 10, 16, 07, TimeSpan.Zero)); + + var messagingData = EventDataExtensions.ToMessagingEventData(e, out int messageSize); + StreamReader sr = new StreamReader(messagingData.GetBodyStream()); + string messageBody = sr.ReadToEnd(); + + var dateTimeRegex = new Regex(@"""DateTimeProperty"" \s* : \s* ""2017-04-19T10:15:23Z""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100)); + Assert.Matches(dateTimeRegex, messageBody); + + var dateTimeOffsetRegex = new Regex(@"""DateTimeOffsetProperty"" \s* : \s* ""2017-04-19T10:16:07\+00:00""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100)); + Assert.Matches(dateTimeOffsetRegex, messageBody); + } + } +#endif +} diff --git a/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/Microsoft.Diagnostics.EventFlow.Outputs.Tests.csproj b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/Microsoft.Diagnostics.EventFlow.Outputs.Tests.csproj index 887de497..e7d656e5 100644 --- a/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/Microsoft.Diagnostics.EventFlow.Outputs.Tests.csproj +++ b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/Microsoft.Diagnostics.EventFlow.Outputs.Tests.csproj @@ -4,6 +4,9 @@ netcoreapp1.0;net46 Microsoft.Diagnostics.EventFlow.Outputs.Tests true + true + ../../PublicKey.snk + true $(PackageTargetFallback);dotnet5.4;portable-net451+win8 1.1.0 @@ -16,7 +19,9 @@ + + @@ -26,6 +31,7 @@ + diff --git a/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/StdOutputTests.cs b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/StdOutputTests.cs index caee2174..73a626bf 100644 --- a/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/StdOutputTests.cs +++ b/test/Microsoft.Diagnostics.EventFlow.Outputs.Tests/StdOutputTests.cs @@ -1,4 +1,9 @@ -using System; +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. +// ------------------------------------------------------------ + +using System; using Moq; using Xunit;