diff --git a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Extensibility/Implementation/ExceptionConverterTest.cs b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Extensibility/Implementation/ExceptionConverterTest.cs index 1d54362be9..63effb4893 100644 --- a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Extensibility/Implementation/ExceptionConverterTest.cs +++ b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/Extensibility/Implementation/ExceptionConverterTest.cs @@ -185,6 +185,19 @@ public void DoesNotTrimShortExceptionMessages() Assert.AreEqual(5, expDetails.message.Length); } + [TestMethod] + [DataRow(null)] + [DataRow("")] + [DataRow(" ")] + public void ProvidesDefaultMessage(string message) + { + var exp = new ExceptionWithMessageOverride(message); + + ExceptionDetails expDetails = ExceptionConverter.ConvertToExceptionDetails(exp, null); + + Assert.AreEqual("n/a", expDetails.message); + } + [MethodImplAttribute(MethodImplOptions.NoInlining)] private Exception CreateException(int numberOfStackpoints) { @@ -212,5 +225,20 @@ private void FailedFunction(int numberOfStackpoints) throw new AggregateException("exception message"); } + + /// + /// Overrides the Message property of the base exception, so that null messages are + /// returned as null, rather than a default, e.g. "Exception of type 'System.Exception' was thrown". + /// + private class ExceptionWithMessageOverride : Exception + { + public ExceptionWithMessageOverride(string message) + : base(message) + { + Message = message; + } + + public override string Message { get; } + } } } \ No newline at end of file diff --git a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/External/ExceptionDetailsImplementation.cs b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/External/ExceptionDetailsImplementation.cs index d1915ac959..d1817d2e53 100644 --- a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/External/ExceptionDetailsImplementation.cs +++ b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/External/ExceptionDetailsImplementation.cs @@ -1,6 +1,7 @@ namespace Microsoft.ApplicationInsights.Extensibility.Implementation.External { using System; + using Microsoft.ApplicationInsights.DataContracts; /// /// Additional implementation for ExceptionDetails. @@ -21,7 +22,7 @@ internal static ExceptionDetails CreateWithoutStackInfo(Exception exception, Exc { id = exception.GetHashCode(), typeName = exception.GetType().FullName, - message = exception.Message, + message = Utils.PopulateRequiredNonWhitespaceStringValue(exception.Message, "message", typeof(ExceptionTelemetry).FullName), }; if (parentExceptionDetails != null) diff --git a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/Utils.cs b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/Utils.cs index c18a90e58a..876f0f172c 100644 --- a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/Utils.cs +++ b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/Utils.cs @@ -52,6 +52,20 @@ public static string PopulateRequiredStringValue(string value, string parameterN return value; } + /// + /// Validates the string and if null or empty populates it with '$parameterName is a required field for $telemetryType' value. + /// + public static string PopulateRequiredNonWhitespaceStringValue(string value, string parameterName, string telemetryType) + { + if (value.IsNullOrWhiteSpace()) + { + CoreEventSource.Log.PopulateRequiredStringWithValue(parameterName, telemetryType); + return "n/a"; + } + + return value; + } + /// /// Returns default Timespan value if not a valid Timespan. /// diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e782641d7..049c0f363d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## VNext +- [Populate required field Message with "n/a" if it is empty](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1066) ## Version 2.22.0 - no changes since beta.