예제 #1
0
        /// <summary>
        /// Adds a context property
        /// </summary>
        public IDisposable Context(Dictionary <string, string> properties)
        {
            if (properties == null || properties.Count == 0)
            {
                return(null);
            }

            return(LogContext.Push(properties));
        }
예제 #2
0
        /// <summary>
        /// Adds a context property
        /// </summary>
        public static IDisposable Context(IEnumerable <KeyValuePair <string, string> > properties)
        {
            if (properties == null)
            {
                return(null);
            }

            return(LogContext.Push(properties));
        }
예제 #3
0
        /// <summary>
        /// Adds a context property
        /// </summary>
        public static IDisposable Context(params KeyValuePair <string, string>[] properties)
        {
            if (properties == null || properties.Length == 0)
            {
                return(null);
            }

            return(LogContext.Push(properties));
        }
예제 #4
0
        /// <summary>
        /// Adds one or more context properties.
        /// </summary>
        /// <param name="properties">
        /// Array or properties where even numbers are property names and odd numbers are property values.
        /// If you have an odd number of array elements the last one is discarded.
        /// </param>
        public IDisposable Context(params string[] properties)
        {
            if (properties == null || properties.Length < 2)
            {
                return(null);
            }

            var d = new Dictionary <string, string>();

            int maxLength = properties.Length - properties.Length % 2;

            for (int i = 0; i < maxLength; i += 2)
            {
                d[properties[i]] = properties[i + 1];
            }

            return(LogContext.Push(d));
        }
예제 #5
0
        /// <summary>
        /// Marks a start of an operation pushing operation id to the current context. If context already contains operation id,
        /// then it's set as operation's parent ID, unless they are equal.
        /// </summary>
        /// <param name="id">ID of the operation. When ommitted a new uniqueue ID is generated</param>
        public static IDisposable Operation(string id = null)
        {
            string existingId  = L.GetContextValue(KnownProperty.OperationId);
            string operationId = id == null?Guid.NewGuid().ToString() : id;

            string parentId = existingId;

            var ps = new Dictionary <string, string>
            {
                [KnownProperty.OperationId] = operationId
            };

            if (parentId != null && operationId != parentId)
            {
                ps[KnownProperty.OperationParentId] = parentId;
            }

            return(LogContext.Push(ps));
        }