Exemple #1
0
        private static string GetCorrelationContext(HttpClient http, ILoggingContext context)
        {
            UnsafeStringBuilder correlationContextBuilder = null;
            var propertyNames = new HashSet <string>();

            try
            {
                context.ForEachProperty((LoggingProperty property, object value, ref object _) =>
                {
                    if (!property.IsBaggage || !propertyNames.Add(property.Name))
                    {
                        return;
                    }

                    if (correlationContextBuilder == null)
                    {
                        propertyNames             = new HashSet <string>();
                        correlationContextBuilder = new UnsafeStringBuilder(1024);
                    }

                    if (correlationContextBuilder.Length > 0)
                    {
                        correlationContextBuilder.Append(", ");
                    }

                    correlationContextBuilder.Append(property.Name);
                    correlationContextBuilder.Append('=');

                    var formatter =
                        property.Formatter ?? LoggingServices.Formatters.Get(value.GetType());

                    formatter.Write(correlationContextBuilder, value);
                });

                return(correlationContextBuilder?.ToString());
            }
            finally
            {
                correlationContextBuilder?.Dispose();
            }
        }