Example #1
0
        /// <summary>
        /// Push a property onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns>A handle to later remove the property from the context.</returns>
        /// <param name="destructureObjects">If true, and the value is a non-primitive, non-array type,
        /// then the value will be converted to a structure; otherwise, unknown types will
        /// be converted to scalars, which are generally stored as strings.</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        public static IDisposable PushProperty(string name, object value, bool destructureObjects = false)
        {
            var stack    = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = stack.Push(new PropertyEnricher(name, value, destructureObjects));

            return(bookmark);
        }
Example #2
0
        /// <summary>
        /// Push a property onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns>A handle to later remove the property from the context.</returns>
        /// <param name="destructureObjects">If true, and the value is a non-primitive, non-array type,
        /// then the value will be converted to a structure; otherwise, unknown types will
        /// be converted to scalars, which are generally stored as strings.</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        public static IDisposable PushProperty(string name, object value, bool destructureObjects = false)
        {
            var stack = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = stack.Push(new PropertyEnricher(name, value, destructureObjects));

            return bookmark;
        }
Example #3
0
        /// <summary>
        /// Remove all data from the context so that
        /// cross-<see cref="AppDomain"/> calls can be made without requiring
        /// Serilog assemblies to be present in the remote domain.
        /// </summary>
        /// <returns>A token that will restore the suspended log context data, if any.</returns>
        /// <remarks>The <see cref="LogContext"/> should not be manipulated further
        /// until the return value from this method has been disposed.</remarks>
        /// <returns></returns>
        public static IDisposable Suspend()
        {
            var stack    = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = null;

            return(bookmark);
        }
Example #4
0
        /// <summary>
        /// Remove all enrichers from the <see cref="LogContext"/>, returning an <see cref="IDisposable"/>
        /// that must later be used to restore enrichers that were on the stack before <see cref="Suspend"/> was called.
        /// </summary>
        /// <returns>A token that must be disposed, in order, to restore properties back to the stack.</returns>
        public static IDisposable Suspend()
        {
            var stack    = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = ImmutableStack <ILogEventEnricher> .Empty;

            return(bookmark);
        }
Example #5
0
            public static IDisposable PushOperationId(object value)
            {
                var stack    = GetOrCreateStack();
                var bookmark = new ContextStackBookmark(stack);

                Values = stack.Push(value);

                return(bookmark);
            }
        public static IDisposable AsOf(DateTime targetDateTime)
        {
            var stack    = CurrentContext;
            var bookmark = new ContextStackBookmark(stack);

            ContextStack.Value = stack.Push(new Context(targetDateTime));

            return(bookmark);
        }
Example #7
0
        /// <summary>
        /// Push an enricher onto the context, returning an <see cref="IDisposable"/>
        /// that must later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="enricher">An enricher to push onto the log context</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IDisposable Push(ILogEventEnricher enricher)
        {
            if (enricher == null)
            {
                throw new ArgumentNullException(nameof(enricher));
            }

            var stack    = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = stack.Push(enricher);

            return(bookmark);
        }
Example #8
0
        /// <summary>
        /// Push multiple properties onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the properties. The properties must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="properties">Log Properties to push onto the log context</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IDisposable PushProperties(params PropertyEnricher[] properties)
        {
            if (properties == null) throw new ArgumentNullException("properties");

            var stack = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            foreach (var prop in properties)
                stack = stack.Push(prop);

            Enrichers = stack;

            return bookmark;
        }
Example #9
0
        /// <summary>
        /// Push multiple properties onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the properties. The properties must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="properties">Log Properties to push onto the log context</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IDisposable PushProperties(params ILogEventEnricher[] properties)
        {
            if (properties == null) throw new ArgumentNullException("properties");

            var stack = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            foreach (var prop in properties)
                stack = stack.Push(prop);

            Enrichers = stack;

            return bookmark;
        }
Example #10
0
        /// <summary>
        /// Push a property onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns>A handle to later remove the property from the context.</returns>
        /// <param name="destructureObjects">If true, and the value is a non-primitive, non-array type,
        /// then the value will be converted to a structure; otherwise, unknown types will
        /// be converted to scalars, which are generally stored as strings.</param>
        /// <returns></returns>
        public static IDisposable PushProperty(string name, object value, bool destructureObjects = false)
        {
            var enrichers = Enrichers;

            if (enrichers == null)
            {
                enrichers = ImmutableStack <ILogEventEnricher> .Empty;
                Enrichers = enrichers;
            }

            var bookmark = new ContextStackBookmark(enrichers);

            Enrichers = enrichers.Push(new LazyFixedPropertyEnricher(name, value, destructureObjects));
            return(bookmark);
        }
Example #11
0
        /// <summary>
        /// Push multiple enrichers onto the context, returning an <see cref="IDisposable"/>
        /// that must later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <seealso cref="PropertyEnricher"/>.
        /// <param name="enrichers">Enrichers to push onto the log context</param>
        /// <returns>A token that must be disposed, in order, to pop properties back off the stack.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IDisposable Push(params ILogEventEnricher[] enrichers)
        {
            if (enrichers == null)
            {
                throw new ArgumentNullException(nameof(enrichers));
            }

            var stack    = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            for (var i = 0; i < enrichers.Length; ++i)
            {
                stack = stack.Push(enrichers[i]);
            }

            Enrichers = stack;

            return(bookmark);
        }
Example #12
0
        /// <summary>
        /// Remove all data from the context so that
        /// cross-<see cref="AppDomain"/> calls can be made without requiring
        /// Serilog assemblies to be present in the remote domain.
        /// </summary>
        /// <returns>A token that will restore the suspended log context data, if any.</returns>
        /// <remarks>The <see cref="LogContext"/> should not be manipulated further
        /// until the return value from this method has been disposed.</remarks>
        /// <returns></returns>
        public static IDisposable Suspend()
        {
            var stack = GetOrCreateEnricherStack();
            var bookmark = new ContextStackBookmark(stack);

            Enrichers = null;

            return bookmark;
        }
Example #13
0
        /// <summary>
        /// Push a property onto the context, returning an <see cref="IDisposable"/>
        /// that can later be used to remove the property, along with any others that
        /// may have been pushed on top of it and not yet popped. The property must
        /// be popped from the same thread/logical call context.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns>A handle to later remove the property from the context.</returns>
        /// <param name="destructureObjects">If true, and the value is a non-primitive, non-array type,
        /// then the value will be converted to a structure; otherwise, unknown types will
        /// be converted to scalars, which are generally stored as strings.</param>
        /// <returns></returns>
        public static IDisposable PushProperty(string name, object value, bool destructureObjects = false)
        {
            var enrichers = Enrichers;
            if (enrichers == null)
            {
                enrichers = ImmutableStack<ILogEventEnricher>.Empty;
                Enrichers = enrichers;
            }

            var bookmark = new ContextStackBookmark(enrichers);
            Enrichers = enrichers.Push(new LazyFixedPropertyEnricher(name, value, destructureObjects));
            return bookmark;
        }
Example #14
0
            public static IDisposable PushOperationId(object value)
            {
                var stack = GetOrCreateStack();
                var bookmark = new ContextStackBookmark(stack);

                Values = stack.Push(value);

                return bookmark;
            }