コード例 #1
0
ファイル: LoggerJsonMessage.cs プロジェクト: uzbekdev1/core-1
        private static void AddScopeItemToMessage(object scopeItem, LoggerJsonMessage message, KeyValuePair <string, object?> scopeSubItem)
        {
            if (scopeSubItem.Key == "{OriginalFormat}")
            {
                if (message.Scope == null)
                {
                    message.Scope = RentScopeList();
                }

                // Item should be a FormattedLogValues instance here, which formats when ToString is called.
                string FormattedMessage = scopeItem.ToString();
                if (FormattedMessage == "[null]")
                {
                    message.Scope.Add(null);
                }
                else
                {
                    message.Scope.Add(FormattedMessage);
                }
                return;
            }

            if (message.Data == null)
            {
                message.Data = RentDataDictionary();
            }

            message.Data[scopeSubItem.Key] = scopeSubItem.Value;
        }
コード例 #2
0
ファイル: LoggerJsonMessage.cs プロジェクト: uzbekdev1/core-1
        private static void AddStateItemToMessage <TState>(TState state, LoggerJsonMessage message, KeyValuePair <string, object?> item)
        {
            if (item.Key == "{OriginalFormat}")
            {
                // state should be a FormattedLogValues instance here, which formats when ToString is called.
                string FormattedMessage = state !.ToString();
                if (FormattedMessage != "[null]")
                {
                    message.Content = FormattedMessage;
                }
                return;
            }

            if (message.Data == null)
            {
                message.Data = RentDataDictionary();
            }

            if (item.Key == "{Data}")
            {
                AddDataToMessage(message.Data, item.Value);
                return;
            }

            message.Data[item.Key] = item.Value;
        }
コード例 #3
0
ファイル: LoggerJsonMessage.cs プロジェクト: uzbekdev1/core-1
        private static void ParseScopeItem(object item, LoggerJsonMessage message)
        {
            if (item is LoggerGroup LoggerGroup)
            {
                if (message._Group == null || LoggerGroup.Priority >= message._Group.Priority)
                {
                    message._Group = LoggerGroup;
                }
            }
            else if (item is string)
            {
                if (message.Scope == null)
                {
                    message.Scope = RentScopeList();
                }
                message.Scope.Add(item);
            }
            else if (item is IReadOnlyList <KeyValuePair <string, object?> > scopeList)
            {
                for (int i = 0; i < scopeList.Count; i++)
                {
                    AddScopeItemToMessage(item, message, scopeList[i]);
                }
            }
            else if (item is IEnumerable <KeyValuePair <string, object?> > scopeValues)
            {
                foreach (KeyValuePair <string, object?> SubItem in scopeValues)
                {
                    AddScopeItemToMessage(item, message, SubItem);
                }
            }
            else if (item.GetType().IsValueType)
            {
                if (message.Scope == null)
                {
                    message.Scope = RentScopeList();
                }
                message.Scope.Add(item);
            }
            else
            {
                if (message.Data == null)
                {
                    message.Data = RentDataDictionary();
                }

                AddObjectPropertiesToMessageData(message.Data, item);
            }
        }
コード例 #4
0
ファイル: LoggerJsonMessage.cs プロジェクト: uzbekdev1/core-1
        /// <summary>
        /// Returns any rented resources for the <see cref="LoggerJsonMessage"/> back to their parent pools.
        /// </summary>
        /// <param name="message"><see cref="LoggerJsonMessage"/>.</param>
        public static void Return(LoggerJsonMessage message)
        {
            Debug.Assert(message != null);

            if (message.Scope != null && s_ScopeListPool.Count < 1024)
            {
                message.Scope.Clear();
                s_ScopeListPool.Add(message.Scope);
                message.Scope = null;
            }

            if (message.Data != null && s_DataDictionaryPool.Count < 1024)
            {
                message.Data.Clear();
                s_DataDictionaryPool.Add(message.Data);
                message.Data = null;
            }

            if (message.Exception != null)
            {
                LoggerJsonMessageException.Return(message.Exception);
            }
        }
コード例 #5
0
ファイル: LoggerJsonMessage.cs プロジェクト: tsvx/core
        /// <summary>
        /// Create a <see cref="LoggerJsonMessage"/> instance from Logger data.
        /// </summary>
        /// <param name="groupName">Group name associated with this entry.</param>
        /// <param name="categoryName">Category name associated with this entry.</param>
        /// <param name="scope">Scope data associated with this entry.</param>
        /// <param name="logLevel">Entry will be written on this level.</param>
        /// <param name="eventId">Id of the event.</param>
        /// <param name="state">The entry to be written. Can be also an object.</param>
        /// <param name="exception">The exception related to this entry.</param>
        /// <param name="formatter">Function to create a <see cref="string"/> message of the <paramref name="state"/> and <paramref name="exception"/>.</param>
        /// <typeparam name="TState">The type of the object to be written.</typeparam>
        /// <returns>Created <see cref="LoggerJsonMessage"/> instance.</returns>
        public static LoggerJsonMessage FromLoggerData <TState>(
            string?groupName,
            string categoryName,
            IEnumerable <object>?scope,
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception?exception,
            Func <TState, Exception?, string> formatter)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            LoggerJsonMessage Message = new LoggerJsonMessage()
            {
                TimestampUtc = DateTime.UtcNow,
                ThreadId     = Thread.CurrentThread.ManagedThreadId,
                EventId      = eventId.Id != 0 ? (int?)eventId.Id : null,
                LogLevel     = logLevel,
                GroupName    = groupName,
                CategoryName = categoryName
            };

            if (scope != null)
            {
                (Message.Data, Message.Scope) = ParseScope(scope);
            }

            if (exception != null)
            {
                Message.Exception = LoggerJsonMessageException.FromException(exception);
            }

            if (state is IEnumerable <KeyValuePair <string, object?> > StateValues)
            {
                foreach (KeyValuePair <string, object?> Item in StateValues)
                {
                    if (Item.Key == "{OriginalFormat}")
                    {
                        // state should be a FormattedLogValues instance here, which formats when ToString is called.
                        string FormattedMessage = state.ToString();
                        if (FormattedMessage != "[null]")
                        {
                            Message.Content = FormattedMessage;
                        }
                        continue;
                    }

                    if (Message.Data == null)
                    {
                        Message.Data = new Dictionary <string, object?>();
                    }

                    if (Item.Key == "{Data}")
                    {
                        AddDataToMessage(Message.Data, Item.Value);
                        continue;
                    }

                    Message.Data[Item.Key] = Item.Value;
                }
            }

            if (string.IsNullOrEmpty(Message.Content))
            {
                string FormattedMessage = formatter(state, null);
                if (FormattedMessage != "[null]")
                {
                    Message.Content = FormattedMessage;
                }
            }

            return(Message);
        }