コード例 #1
0
        public void Log_EmitsExpectedEvent()
        {
            string message = "TestMessage";
            string functionInvocationId = Guid.NewGuid().ToString();
            string activityId           = Guid.NewGuid().ToString();

            string properties = null;

            _mockEventGenerator.Setup(p => p.LogAzureMonitorDiagnosticLogEvent(LogLevel.Debug, _websiteHostName, AzureMonitorDiagnosticLogger.AzureMonitorOperationName, AzureMonitorDiagnosticLogger.AzureMonitorCategoryName, _regionName, It.IsAny <string>()))
            .Callback <LogLevel, string, string, string, string, string>((t, r, o, c, l, p) =>
            {
                // Store off the properties for later validation
                properties = p;
            });

            using (CreateScope(activityId: activityId, functionName: _functionName, functionInvocationId: functionInvocationId))
            {
                _logger.LogDebug(new EventId(123, "TestEvent"), message);
            }

            _mockEventGenerator.VerifyAll();

            JObject actual = JObject.Parse(properties);

            var     level    = LogLevel.Debug;
            JObject expected = JObject.FromObject(new
            {
                appName      = _appServiceOptionsWrapper.CurrentValue.AppName,
                roleInstance = _roleInstance,
                message,
                category    = _category,
                hostVersion = ScriptHost.Version,
                functionInvocationId,
                functionName   = _functionName,
                hostInstanceId = _hostInstanceId,
                activityId,
                level     = level.ToString(),
                levelId   = (int)level,
                processId = _processId,
                eventId   = 123,
                eventName = "TestEvent"
            });

            Assert.True(JToken.DeepEquals(actual, expected), $"Actual: {actual.ToString()}{Environment.NewLine}Expected: {expected.ToString()}");
        }