internal static void AddProperties(ISupportProperties telemetry, object message, ICollection <string> categories,
                                           int eventId, string title, IDictionary <string, object> properties)
        {
            telemetry.Properties.Add("Message", message?.ToString().MaximizeLength(MaxValueLength));
            telemetry.Properties.Add("EventId", eventId.ToString());

            if (categories != null && categories.Count > 0)
            {
                telemetry.Properties.Add("Categories", string.Join(", ", categories));
            }

            if (!string.IsNullOrEmpty(title))
            {
                telemetry.Properties.Add("Title", title);
            }

            if (properties?.Any() ?? false)
            {
                foreach (var property in properties.Where(p => !string.IsNullOrEmpty(p.Key)))
                {
                    telemetry.Properties.Add(property.Key.MaximizeLength(MaxPropertyNameLength),
                                             property.Value?.ToString().MaximizeLength(MaxValueLength));
                }
            }
        }
Beispiel #2
0
        private static ITelemetry ConvertLogEventsToCustomTraceTelemetry(LogEvent logEvent, IFormatProvider formatProvider)
        {
            var telemetry = GetTelemetry(logEvent, formatProvider);

            telemetry.Context.Cloud.RoleInstance = _roleInstance;
            telemetry.Context.Cloud.RoleName     = _roleName;

            if (logEvent.Properties.ContainsKey("UserId"))
            {
                telemetry.Context.User.Id = logEvent.Properties["UserId"].ToString();
            }

            if (logEvent.Properties.ContainsKey("operation_Id"))
            {
                telemetry.Context.Operation.Id = logEvent.Properties["operation_Id"].ToString();
            }

            if (logEvent.Properties.ContainsKey("operation_parentId"))
            {
                telemetry.Context.Operation.ParentId = logEvent.Properties["operation_parentId"].ToString();
            }

            ISupportProperties supportProperties = (ISupportProperties)telemetry;

            var removeProps = new[] { "UserId", "operation_parentId", "operation_Id" };

            removeProps = removeProps.Where(prop => supportProperties.Properties.ContainsKey(prop)).ToArray();

            foreach (var prop in removeProps)
            {
                supportProperties.Properties.Remove(prop);
            }

            return(telemetry);
        }
Beispiel #3
0
        public void Initialize(ITelemetry telemetry)
        {
            if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
            {
                //set custom role name here
                telemetry.Context.Cloud.RoleName = "Sales Api";
                //telemetry.Context.Cloud..RoleInstance = "Custom RoleInstance";
            }

            var messageCorrelation = _messageAccessor.CorrelationId;

            if (messageCorrelation != Guid.Empty)
            {
                ISupportProperties properties = (ISupportProperties)telemetry;
                properties.Properties["CorrelationId"] = messageCorrelation.ToString();
            }
            else
            {
                var correlationContext = _accessor.CorrelationContext;
                if (correlationContext != null)
                {
                    ISupportProperties properties = (ISupportProperties)telemetry;
                    properties.Properties["CorrelationId"] = correlationContext.CorrelationId;
                }
            }
        }
Beispiel #4
0
        public void InitializeWillNotOverwriteExistingCustomDimension()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            envMock.Setup(env => env.ContainerID).Returns("Cid");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry         telemetry = new TraceTelemetry();
            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            telemetryWithProperties.Properties["K8s.Container.ID"] = "Existing Cid";
            target.Initialize(telemetry);

            Assert.Equal("Existing Cid", telemetryWithProperties.Properties["K8s.Container.ID"]);
        }
Beispiel #5
0
        private void _propertiesProcessContext(ISupportProperties data, ProcessContext pc)
        {
            data.Properties.Add("ResourceId", pc.CurrentInfo.ResourceId);
            data.Properties.Add("ProcessType", pc.ProcessType.ToString());
            data.Properties.Add("ResultType", pc.ResultType.ToString());

            data.Properties.Add("Idx/Total", pc.Index.ToString() + "/" + pc.Total.ToString());

            if (pc.LastState != default)
            {
                data.Properties.Add("CheckSum_Old", pc.LastState.CheckSum);
                data.Properties.Add("Modified_Old", pc.LastState.Modified.ToString());
            }

            if (pc.NewState != default)
            {
                data.Properties.Add("RetryCount", pc.NewState.RetryCount.ToString());
                data.Properties.Add("RetrievedAt", pc.NewState.ToString());
                data.Properties.Add("CheckSum", pc.NewState.CheckSum);
                data.Properties.Add("Modified", pc.NewState.Modified.ToString());

                string extensionsString = JsonConvert.SerializeObject(pc.NewState.Extensions, ArkDefaultJsonSerializerSettings.Instance);
                data.Properties.Add("Extensions", extensionsString);
            }
        }
        public int GetHashCode(ITelemetry obj)
        {
            // now construct the basis for our hash
            StringBuilder sb = new StringBuilder();

            // first append the type
            sb.Append(obj.GetType().ToString());

            // then the name
            sb.Append(obj.GetNameOrMessage());

            if (obj.GetType().GetInterfaces().Contains(typeof(ISupportProperties)))
            {
                ISupportProperties objp = obj as ISupportProperties;

                // Properties may have been added out of order, which may result
                // in different hash values despite having the same properties.
                // Therefore we need to construct the string on which we base our
                // hash value in an ordered manner.
                SortedSet <string> sortedKeys = new SortedSet <string>();
                foreach (string key in objp.Properties.Keys)
                {
                    sortedKeys.Add(key);
                }

                // then each key/value pair in an ordered manner
                foreach (string key in sortedKeys)
                {
                    sb.Append(key).Append(objp.Properties[key]);
                }
            }

            // and finally calculate the hash
            return(sb.ToString().GetHashCode());
        }
Beispiel #7
0
 // Inserts properties into the telemetry's properties. Properly formats dates, removes nulls, applies prefix, etc.
 private static void ApplyProperties(ISupportProperties telemetry, IEnumerable <KeyValuePair <string, object> > values, string propertyPrefix = null)
 {
     foreach (var property in values)
     {
         ApplyProperty(telemetry, property.Key, property.Value, propertyPrefix);
     }
 }
        private bool IsEnabled(ITelemetry item)
        {
            bool enabled = true;

            ISupportProperties telemetry = item as ISupportProperties;

            if (telemetry != null && _filter != null)
            {
                string categoryName = null;
                if (!telemetry.Properties.TryGetValue(LogConstants.CategoryNameKey, out categoryName))
                {
                    // If no category is specified, it will be filtered by the default filter
                    categoryName = string.Empty;
                }

                // Extract the log level and apply the filter
                string   logLevelString = null;
                LogLevel logLevel;
                if (telemetry.Properties.TryGetValue(LogConstants.LogLevelKey, out logLevelString) &&
                    Enum.TryParse(logLevelString, out logLevel))
                {
                    enabled = _filter(categoryName, logLevel);
                }
            }

            return(enabled);
        }
        // Applies custom scope properties; does not apply 'system' used properties
        private static void ApplyCustomScopeProperties(ISupportProperties telemetry)
        {
            var scopeProperties = DictionaryLoggerScope.GetMergedStateDictionary()
                                  .Where(p => !SystemScopeKeys.Contains(p.Key, StringComparer.Ordinal));

            ApplyProperties(telemetry, scopeProperties, LogConstants.CustomPropertyPrefix);
        }
Beispiel #10
0
 private void PrintProperties(ISupportProperties itemProps)
 {
     foreach (var prop in itemProps.Properties)
     {
         this.output.WriteLine(prop.Key + ":" + prop.Value);
     }
 }
        private void AddProperty(ISupportProperties item, string propertyName, string propertyValue)
        {
            if (propertyName == null)
            {
                return;
            }

            var properties = item.Properties;

            if (!properties.ContainsKey(propertyName))
            {
                properties.Add(propertyName, propertyValue);
                return;
            }

            string newPropertyName = propertyName + "_";

            //update property key till there is no such key in dict
            do
            {
                newPropertyName += RandomGenerator.Value.Next(0, 10);
            }while (properties.ContainsKey(newPropertyName));

            properties.Add(newPropertyName, propertyValue);
        }
        public static ITelemetry UpdateWithContextTelemitryProperties(this ITelemetry telemetry, LogEvent logEvent)
        {
            // Post-process the telemetry's context to contain the user id as desired
            if (logEvent.Properties.ContainsKey("userId"))
            {
                telemetry.Context.User.Id = logEvent.Properties["userId"].ToString();
            }
            // Post-process the telemetry's context to contain the operation id
            if (logEvent.Properties.ContainsKey("RequestId"))
            {
                telemetry.Context.Operation.Id = logEvent.Properties["RequestId"].ToString();
            }
            // Post-process the telemetry's context to contain the operation parent id
            if (logEvent.Properties.ContainsKey("parentActivityId"))
            {
                telemetry.Context.Operation.ParentId = logEvent.Properties["parentActivityId"].ToString();
            }
            // Typecast to ISupportProperties so you can manipulate the properties as desired
            ISupportProperties propTelematry = (ISupportProperties)telemetry;

            // Find now redundent properties
            var removeProps = new[] { "userId", "RequestId", "parentActivityId" };

            removeProps = removeProps.Where(prop => propTelematry.Properties.ContainsKey(prop)).ToArray();

            foreach (var prop in removeProps)
            {
                // Remove those properties
                propTelematry.Properties.Remove(prop);
            }
            return(telemetry);
        }
        private void ApplyCustomProperties(ISupportProperties telemetry, IEnumerable <KeyValuePair <string, object> > values)
        {
            telemetry.Properties.Add(LoggingKeys.CategoryName, _categoryName);

            foreach (var property in values)
            {
                string stringValue = null;

                // drop null properties
                if (property.Value == null)
                {
                    continue;
                }

                // Format dates
                Type propertyType = property.Value.GetType();
                if (propertyType == typeof(DateTime))
                {
                    stringValue = ((DateTime)property.Value).ToUniversalTime().ToString(DateTimeFormatString);
                }
                else if (propertyType == typeof(DateTimeOffset))
                {
                    stringValue = ((DateTimeOffset)property.Value).UtcDateTime.ToString(DateTimeFormatString);
                }
                else
                {
                    stringValue = property.Value.ToString();
                }

                // Since there is no nesting of properties, apply a prefix before the property name to lessen
                // the chance of collisions.
                telemetry.Properties.Add(LoggingKeys.CustomPropertyPrefix + property.Key, stringValue);
            }
        }
Beispiel #14
0
 private void AddProperty(ISupportProperties item, string propertyName, string propertyValue)
 {
     if (propertyValue != null)
     {
         item.Properties.Add(propertyName, propertyValue);
     }
 }
        private void SetContextProperties(ITelemetry telemetry)
        {
            telemetry.Context.Cloud.RoleName     = FabricEnvironmentVariable.ServicePackageName;
            telemetry.Context.Cloud.RoleInstance = FabricEnvironmentVariable.ServicePackageActivationId ?? FabricEnvironmentVariable.ServicePackageInstanceId;
            telemetry.Context.Component.Version  = _context.CodePackageActivationContext.CodePackageVersion;


            ISupportProperties propTelematry = (ISupportProperties)telemetry;

            if (!propTelematry.Properties.ContainsKey(ServiceContextProperties.NodeName))
            {
                if (!string.IsNullOrEmpty(FabricEnvironmentVariable.NodeName))
                {
                    propTelematry.Properties.Add(ServiceContextProperties.NodeName, FabricEnvironmentVariable.NodeName);
                }
            }

#if Debug
            telemetry.Context.Operation.SyntheticSource = "DebugSession";
#else
            if (Debugger.IsAttached)
            {
                telemetry.Context.Operation.SyntheticSource = "DebuggerAttached";
            }
#endif

            if (_logEvent.Properties.TryGetValue(SharedProperties.TraceId, out LogEventPropertyValue value))
            {
                var id = ((ScalarValue)value).Value.ToString();
                telemetry.Context.Operation.ParentId = id;
                telemetry.Context.Operation.Id       = id;
            }
        }
Beispiel #16
0
        /// <summary>
        /// Flattens Extension object on ITelemetry if exists into the properties and measurements.
        /// </summary>
        internal static void FlattenIExtensionIfExists(this ITelemetry telemetry)
        {
            if (telemetry.Extension != null)
            {
                ISupportProperties itemWithProperties = telemetry as ISupportProperties;
                ISupportMetrics    itemWithMetrics    = telemetry as ISupportMetrics;

                // Do not serialize if data cannot be stored on the item
                if (itemWithProperties != null || itemWithMetrics != null)
                {
                    DictionarySerializationWriter extensionSerializationWriter = new DictionarySerializationWriter();
                    telemetry.Extension.Serialize(extensionSerializationWriter);

                    if (itemWithProperties != null)
                    {
                        Utils.CopyDictionary(extensionSerializationWriter.AccumulatedDictionary, itemWithProperties.Properties);
                    }

                    if (itemWithMetrics != null)
                    {
                        Utils.CopyDictionary(extensionSerializationWriter.AccumulatedMeasurements, itemWithMetrics.Metrics);
                    }
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Merges the properties from an <see cref="IDictionary{TKey, TValue}"/> into the <see cref="ISupportProperties" />Telemetry object
        /// </summary>
        /// <param name="telemetry">The object into which the properties will be merged.</param>
        /// <param name="properties">The properties to merge in.</param>
        public static void MergeProperties(this ISupportProperties telemetry, IDictionary <string, string> properties)
        {
            if (telemetry == null)
            {
                throw new ArgumentNullException(nameof(telemetry));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            foreach (KeyValuePair <string, string> property in properties)
            {
                string kn;

                if (telemetry.Properties.ContainsKey(property.Key))
                {
                    kn = string.Concat("MERGED_", property.Key);
                }
                else
                {
                    kn = property.Key;
                }

                telemetry.Properties.Add(kn, property.Value);
            }
        }
Beispiel #18
0
        public void TimeoutGettingK8sEnvNoException()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(envMock.Object, TimeSpan.FromMinutes(1));
            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);

            ITelemetry         telemetry = new TraceTelemetry();
            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            telemetryWithProperties.Properties["K8s.Container.ID"] = "No Crash";
            target.Initialize(telemetry);

            Assert.Equal("No Crash", telemetryWithProperties.Properties["K8s.Container.ID"]);
        }
Beispiel #19
0
        private void AddEsquioProperties(ITelemetry telemetryItem)
        {
            if (_httpContextAccessor.HttpContext != null && telemetryItem != null)
            {
                var evaluation = _httpContextAccessor.HttpContext?
                                 .Items
                                 .Where(k => k.Key.ToString() == EsquioConstants.ESQUIO)
                                 .Select(i => i.Value)
                                 .SingleOrDefault();

                if (evaluation != null)
                {
                    var items = evaluation as Dictionary <string, bool>;

                    if (items != null)
                    {
                        ISupportProperties telemetry = telemetryItem as ISupportProperties;

                        foreach (var(key, value) in items)
                        {
                            if (!telemetry.Properties.ContainsKey(key.ToString()))
                            {
                                telemetry.Properties.Add(key.ToString(), value.ToString());
                            }
                        }
                    }
                }
            }
        }
Beispiel #20
0
        public void ShouldUseTheValueByTheKeyProcessorForTelemetry()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello.RoleName");
            envMock.Setup(env => env.ContainerID).Returns("Hello.Cid");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input.Replace('.', '_'));

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            Assert.False(telemetryWithProperties.Properties.ContainsKey("Kubernetes.Container.ID"));
            Assert.True(telemetryWithProperties.Properties.ContainsKey("Kubernetes_Container_ID"));
        }
Beispiel #21
0
 private void AddCommonData(ISupportProperties telemetry)
 {
     if (_httpContextAccessor != null)
     {
         telemetry.Properties.Add("User", _httpContextAccessor.HttpContext.User.Identity.Name);
     }
 }
        // Inserts properties into the telemetry's properties. Properly formats dates, removes nulls, applies prefix, etc.
        private static void ApplyProperties(ISupportProperties telemetry, IEnumerable <KeyValuePair <string, object> > values, string propertyPrefix = null)
        {
            foreach (var property in values)
            {
                string stringValue = null;

                // drop null properties
                if (property.Value == null)
                {
                    continue;
                }

                // Format dates
                Type propertyType = property.Value.GetType();
                if (propertyType == typeof(DateTime))
                {
                    stringValue = ((DateTime)property.Value).ToUniversalTime().ToString(DateTimeFormatString);
                }
                else if (propertyType == typeof(DateTimeOffset))
                {
                    stringValue = ((DateTimeOffset)property.Value).UtcDateTime.ToString(DateTimeFormatString);
                }
                else
                {
                    stringValue = property.Value.ToString();
                }

                telemetry.Properties.Add($"{propertyPrefix}{property.Key}", stringValue);
            }
        }
Beispiel #23
0
        private void SetCustomDimensions(ISupportProperties telemetry, Stopwatch cpuWatch, TimeSpan startCPUTime)
        {
            SetCustomDimensions(telemetry);

            // Add CPU/Memory metrics to telemetry.
            Process  process    = Process.GetCurrentProcess();
            TimeSpan endCPUTime = process.TotalProcessorTime;

            cpuWatch.Stop();

            string cpuString = "NaN";

            if (cpuWatch.ElapsedMilliseconds > 0)
            {
                int processorCount = Environment.ProcessorCount;
                Debug.Assert(processorCount > 0, $"How could process count be {processorCount}?");
                // A very simple but not that accurate evaluation of how much CPU the process is take out of a core.
                double CPUPercentage = (endCPUTime - startCPUTime).TotalMilliseconds / (double)(cpuWatch.ElapsedMilliseconds);
                cpuString = CPUPercentage.ToString("P2", CultureInfo.InvariantCulture);
            }

            long memoryInBytes = process.VirtualMemorySize64;

            SetCustomDimension(telemetry, Invariant($"{ProcessString}.{CPU}(%)"), cpuString);
            SetCustomDimension(telemetry, Invariant($"{ProcessString}.{Memory}"), memoryInBytes.GetReadableSize());
        }
Beispiel #24
0
        private static void ApplyProperty(ISupportProperties telemetry, string key, object value, string propertyPrefix = null)
        {
            // do not apply null values
            if (value == null)
            {
                return;
            }

            string stringValue = null;

            // Format dates
            Type propertyType = value.GetType();

            if (propertyType == typeof(DateTime))
            {
                stringValue = ((DateTime)value).ToUniversalTime().ToString(DateTimeFormatString);
            }
            else if (propertyType == typeof(DateTimeOffset))
            {
                stringValue = ((DateTimeOffset)value).UtcDateTime.ToString(DateTimeFormatString);
            }
            else
            {
                stringValue = value.ToString();
            }

            telemetry.Properties[$"{propertyPrefix}{key}"] = stringValue;
        }
Beispiel #25
0
 private void ForwardEventPropertiesToTelemetryProperties(LogEvent logEvent, ISupportProperties telemetryProperties, IFormatProvider formatProvider)
 {
     ForwardPropertiesToTelemetryProperties(logEvent, telemetryProperties, formatProvider,
                                            includeLogLevel: false,
                                            includeRenderedMessage: false,
                                            includeMessageTemplate: false);
 }
        private static KeyValuePair <string, string>[] GetProperties(ISupportProperties telemetry, string specialPropertyName = null)
        {
            Dictionary <string, string> properties = null;

            if (telemetry.Properties != null && telemetry.Properties.Count > 0)
            {
                properties = new Dictionary <string, string>(MaxPropertyCount + 1);

                foreach (var prop in
                         telemetry.Properties.Where(p => !string.Equals(p.Key, specialPropertyName, StringComparison.Ordinal)).Take(MaxPropertyCount))
                {
                    string truncatedKey = TruncateValue(prop.Key);

                    if (!properties.ContainsKey(truncatedKey))
                    {
                        properties.Add(truncatedKey, TruncateValue(prop.Value));
                    }
                }

                if (specialPropertyName != null)
                {
                    string specialPropertyValue;
                    if (telemetry.Properties.TryGetValue(specialPropertyName, out specialPropertyValue))
                    {
                        properties.Add(TruncateValue(specialPropertyName), TruncateValue(specialPropertyValue));
                    }
                }
            }

            return(properties != null?properties.ToArray() : null);
        }
Beispiel #27
0
 public static string GetProperty(this ISupportProperties propertiesTelemetry, string key, string defaultValue = null)
 {
     if (propertiesTelemetry.Properties.ContainsKey(key))
     {
         return(propertiesTelemetry.Properties[key]);
     }
     return(defaultValue);
 }
        protected void AssignTelemetryContextProperties(LogEvent logEvent, ISupportProperties telemetry)
        {
            AssignContextPropertiesFromDictionaryProperty(logEvent, telemetry, ContextProperties.TelemetryContext);

#pragma warning disable 618 // Until we remove obsolete 'EventDescription'.
            AssignContextPropertiesFromDictionaryProperty(logEvent, telemetry, ContextProperties.EventTracking.EventContext);
#pragma warning restore 618
        }
        /// <summary>
        /// Populates the state, scope and event information for the logging event.
        /// </summary>
        /// <typeparam name="TState">State information for the current event.</typeparam>
        /// <param name="telemetryItem">Telemetry item.</param>
        /// <param name="state">Event state information.</param>
        /// <param name="eventId">Event Id information.</param>
        private void PopulateTelemetry <TState>(ISupportProperties telemetryItem, TState state, EventId eventId)
        {
            IDictionary <string, string> dict = telemetryItem.Properties;

            dict["CategoryName"] = this.categoryName;

            if (eventId.Id != 0)
            {
                dict["EventId"] = eventId.Id.ToString(CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(eventId.Name))
            {
                dict["EventName"] = eventId.Name;
            }

            if (this.applicationInsightsLoggerOptions.IncludeScopes)
            {
                if (state is IReadOnlyCollection <KeyValuePair <string, object> > stateDictionary)
                {
                    foreach (KeyValuePair <string, object> item in stateDictionary)
                    {
                        dict[item.Key] = Convert.ToString(item.Value, CultureInfo.InvariantCulture);
                    }
                }

                if (this.ExternalScopeProvider != null)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    this.ExternalScopeProvider.ForEachScope(
                        (activeScope, builder) =>
                    {
                        // Ideally we expect that the scope to implement IReadOnlyList<KeyValuePair<string, object>>.
                        // But this is not guaranteed as user can call BeginScope and pass anything. Hence
                        // we try to resolve the scope as Dictionary and if we fail, we just serialize the object and add it.

                        if (activeScope is IReadOnlyCollection <KeyValuePair <string, object> > activeScopeDictionary)
                        {
                            foreach (KeyValuePair <string, object> item in activeScopeDictionary)
                            {
                                dict[item.Key] = Convert.ToString(item.Value, CultureInfo.InvariantCulture);
                            }
                        }
                        else
                        {
                            builder.Append(" => ").Append(activeScope);
                        }
                    },
                        stringBuilder);

                    if (stringBuilder.Length > 0)
                    {
                        dict["Scope"] = stringBuilder.ToString();
                    }
                }
            }
        }
Beispiel #30
0
        /// <summary>
        /// Add properties if not null or empty to a telemetry object that supports properties
        /// </summary>
        /// <param name="propertiesObject"></param>
        /// <param name="dataName"></param>
        /// <param name="dataValue"></param>
        private void AddPropertyData(ISupportProperties propertiesObject, string dataName, string dataValue)
        {
            if (propertiesObject == null || string.IsNullOrEmpty(dataName) || string.IsNullOrEmpty(dataValue))
            {
                return;
            }

            propertiesObject.Properties[dataName] = dataValue;
        }
 /// <summary>
 /// Forwards the log event properties to the provided <see cref="ISupportProperties" /> instance.
 /// </summary>
 /// <param name="telemetry">The telemetry.</param>
 /// <param name="logEvent">The log event.</param>
 /// <param name="renderedMessage">The rendered message.</param>
 /// <returns></returns>
 private void ForwardLogEventPropertiesToTelemetryProperties(ISupportProperties telemetry, LogEvent logEvent, string renderedMessage)
 {
     telemetry.Properties.Add("LogLevel", logEvent.Level.ToString());
     telemetry.Properties.Add("RenderedMessage", renderedMessage);
     
     foreach (var property in logEvent.Properties.Where(property => property.Value != null && !telemetry.Properties.ContainsKey(property.Key)))
     {
         telemetry.Properties.Add(property.Key, property.Value.ToString());
     }
 }
        private static KeyValuePair<string, string>[] GetProperties(ISupportProperties telemetry, string specialPropertyName = null)
        {
            Dictionary<string, string> properties = null;

            if (telemetry.Properties != null && telemetry.Properties.Count > 0)
            {
                properties = new Dictionary<string, string>(MaxPropertyCount + 1);

                foreach (var prop in
                    telemetry.Properties
                    .Where(p => !string.Equals(p.Key, specialPropertyName, StringComparison.Ordinal))
                    .Take(MaxPropertyCount))
                {
                    string truncatedKey = TruncateValue(prop.Key);

                    if (!properties.ContainsKey(truncatedKey))
                    {
                        properties.Add(truncatedKey, TruncateValue(prop.Value));
                    }
                }

                if (specialPropertyName != null)
                {
                    string specialPropertyValue;
                    if (telemetry.Properties.TryGetValue(specialPropertyName, out specialPropertyValue))
                    {
                        properties.Add(TruncateValue(specialPropertyName), TruncateValue(specialPropertyValue));
                    }
                }
            }

            return properties != null ? properties.ToArray() : null;
        }