コード例 #1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var level = LevelConvert.ToSerilogLevel(logLevel);

            if (!_logger.IsEnabled(level))
            {
                return;
            }

            var    logger          = _logger;
            string messageTemplate = null;

            var properties = new List <LogEventProperty>();

            if (state is IEnumerable <KeyValuePair <string, object> > structure)
            {
                foreach (var property in structure)
                {
                    if (property.Key == SerilogLoggerProvider.OriginalFormatPropertyName && property.Value is string value)
                    {
                        messageTemplate = value;
                    }
                    else if (property.Key.StartsWith("@"))
                    {
                        if (logger.BindProperty(property.Key.Substring(1), property.Value, true, out var destructured))
                        {
                            properties.Add(destructured);
                        }
                    }
                    else
                    {
                        if (logger.BindProperty(property.Key, property.Value, false, out var bound))
                        {
                            properties.Add(bound);
                        }
                    }
                }

                var stateType     = state.GetType();
                var stateTypeInfo = stateType.GetTypeInfo();
                // Imperfect, but at least eliminates `1 names
                if (messageTemplate == null && !stateTypeInfo.IsGenericType)
                {
                    messageTemplate = "{" + stateType.Name + ":l}";
                    if (logger.BindProperty(stateType.Name, AsLoggableValue(state, formatter), false, out var stateTypeProperty))
                    {
                        properties.Add(stateTypeProperty);
                    }
                }
            }

            if (messageTemplate == null)
            {
                string propertyName = null;
                if (state != null)
                {
                    propertyName    = "State";
                    messageTemplate = "{State:l}";
                }
                else if (formatter != null)
                {
                    propertyName    = "Message";
                    messageTemplate = "{Message:l}";
                }

                if (propertyName != null)
                {
                    if (logger.BindProperty(propertyName, AsLoggableValue(state, formatter), false, out var property))
                    {
                        properties.Add(property);
                    }
                }
            }

            if (eventId.Id != 0 || eventId.Name != null)
            {
                properties.Add(CreateEventIdProperty(eventId));
            }

            var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? "");
            var evt            = new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties);

            logger.Write(evt);
        }
コード例 #2
0
 public bool IsEnabled(LogLevel logLevel)
 {
     return(_logger.IsEnabled(LevelConvert.ToSerilogLevel(logLevel)));
 }