コード例 #1
0
ファイル: Logger.cs プロジェクト: nickvane/serilog
        public void Write(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues)
        {
            if (messageTemplate == null)
            {
                return;
            }
            if (!IsEnabled(level))
            {
                return;
            }

            // Catch a common pitfall when a single non-object array is cast to object[]
            // Needs some more thought
            if (propertyValues != null &&
                propertyValues.GetType() != typeof(object[]))
            {
                propertyValues = new object[] { propertyValues }
            }
            ;

            var now = DateTimeOffset.Now;

            MessageTemplate parsedTemplate;
            IEnumerable <LogEventProperty> properties;

            _messageTemplateProcessor.Process(messageTemplate, propertyValues, out parsedTemplate, out properties);

            var logEvent = new LogEvent(now, level, exception, parsedTemplate, properties);

            Dispatch(logEvent);
        }
コード例 #2
0
        /// <summary>
        /// Uses configured scalar conversion and destructuring rules to bind a set of properties to a
        /// message template. Returns false if the template or values are invalid (<summary>Logger</summary>
        /// methods never throw exceptions).
        /// </summary>
        /// <param name="messageTemplate">Message template describing an event.</param>
        /// <param name="propertyValues">Objects positionally formatted into the message template.</param>
        /// <param name="parsedTemplate">The internal representation of the template, which may be used to
        /// render the <paramref name="boundProperties"/> as text.</param>
        /// <param name="boundProperties">Captured properties from the template and <paramref name="propertyValues"/>.</param>
        /// <example>
        /// MessageTemplate template;
        /// IEnumerable&lt;LogEventProperty&gt; properties>;
        /// if (Log.BindMessageTemplate("Hello, {Name}!", new[] { "World" }, out template, out properties)
        /// {
        ///     var propsByName = properties.ToDictionary(p => p.Name, p => p.Value);
        ///     Console.WriteLine(template.Render(propsByName, null));
        ///     // -> "Hello, World!"
        /// }
        /// </example>
        public bool BindMessageTemplate(string messageTemplate, object[] propertyValues, out MessageTemplate parsedTemplate, out IEnumerable <LogEventProperty> boundProperties)
        {
            if (messageTemplate == null)
            {
                parsedTemplate  = null;
                boundProperties = null;
                return(false);
            }

            _messageTemplateProcessor.Process(messageTemplate, propertyValues, out parsedTemplate, out boundProperties);
            return(true);
        }