Skip to content

Commit

Permalink
Fixing null ref in AI logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Feb 11, 2021
1 parent 67aa71a commit 5015ee9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,15 @@ private void LogTrace(LogLevel logLevel, IEnumerable<KeyValuePair<string, object
ApplyScopeAndStateProperties(properties, values);

var severityLevel = GetSeverityLevel(logLevel);
_telemetryClient.TrackTrace(formattedMessage, severityLevel.Value, properties);
if (severityLevel.HasValue)
{
_telemetryClient.TrackTrace(formattedMessage, severityLevel.Value, properties);
}
else
{
// LogLevel.None maps to null, so we have to handle that specially
_telemetryClient.TrackTrace(formattedMessage, properties);
}
}

private static SeverityLevel? GetSeverityLevel(LogLevel logLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,35 @@ public void Log_Exception_LogMessage()
Assert.Equal("Log message", exceptionTelemetry.Properties[LogConstants.FormattedMessageKey]);
}

[Fact]
public void Log_LogLevelNone_Succeeds()
{
Guid scopeGuid = Guid.NewGuid();

ILogger logger = CreateLogger(_functionCategoryName);

using (logger.BeginFunctionScope(CreateFunctionInstance(scopeGuid), _hostInstanceId))
{
logger.Log(LogLevel.None, "None");
}

Assert.Single(_channel.Telemetries);
Assert.Single(_channel.Telemetries.OfType<TraceTelemetry>());
foreach (var telemetry in _channel.Telemetries.Cast<TraceTelemetry>())
{
Enum.TryParse(telemetry.Message, out LogLevel expectedLogLevel);
Assert.Equal(expectedLogLevel.ToString(), telemetry.Properties[LogConstants.LogLevelKey]);
Assert.Null(telemetry.SeverityLevel);

Assert.Equal(5, telemetry.Properties.Count);
Assert.Equal(Process.GetCurrentProcess().Id.ToString(), telemetry.Properties[LogConstants.ProcessIdKey]);
Assert.Equal(_functionCategoryName, telemetry.Properties[LogConstants.CategoryNameKey]);
Assert.Equal(telemetry.Message, telemetry.Properties[LogConstants.CustomPropertyPrefix + LogConstants.OriginalFormatKey]);
Assert.Equal(scopeGuid.ToString(), telemetry.Properties[LogConstants.InvocationIdKey]);
Assert.Equal(telemetry.Message, telemetry.Properties[LogConstants.LogLevelKey]);
}
}

[Fact]
public void DuplicateProperties_LastStateWins()
{
Expand Down

0 comments on commit 5015ee9

Please sign in to comment.