コード例 #1
0
        /// <summary>
        /// Create a logger that enriches log events with the specified property.
        /// </summary>
        /// <param name="propertyName">The name of the property. Must be non-empty.</param>
        /// <param name="value">The property value.</param>
        /// <param name="destructureObjects">If true, the value will be serialized as a structured
        /// object if possible; if false, the object will be recorded as a scalar or simple array.</param>
        /// <returns>A logger that will enrich log events as specified.</returns>
        public Logger ForContext(string propertyName, object value, bool destructureObjects = false)
        {
            if (!LogEventProperty.IsValidName(propertyName))
            {
                SelfLog.WriteLine("Attempt to call ForContext() with invalid property name `{0}` (value: `{1}`)", propertyName, value);
                return(this);
            }

            // It'd be nice to do the destructuring lazily, but unfortunately `value` may be mutated between
            // now and the first log event written...
            // A future optimization opportunity may be to implement ILogEventEnricher on LogEventProperty to
            // remove one more allocation.
            var enricher = new FixedPropertyEnricher(_messageTemplateProcessor.CreateProperty(propertyName, value, destructureObjects));

            var levelSwitch = _levelSwitch;

            if (_overrideMap != null && propertyName == Constants.SourceContextPropertyName)
            {
                var context = value as string;
                if (context != null)
                {
                    _overrideMap.GetEffectiveLevel(context, out levelSwitch);
                }
            }

            return(new Logger(
                       this,
                       enricher,
                       null,
                       levelSwitch,
                       _overrideMap));
        }
コード例 #2
0
ファイル: Logger.cs プロジェクト: nickvane/serilog
 public ILogger ForContext(string propertyName, object value, bool destructureObjects = false)
 {
     return(ForContext(new[] {
         new FixedPropertyEnricher(
             _messageTemplateProcessor.CreateProperty(propertyName, value, destructureObjects))
     }));
 }