/// <summary> /// Create a <see cref="LoggerJsonMessage"/> instance from Logger data. /// </summary> /// <param name="categoryName">Category name associated with this entry.</param> /// <param name="scopeProvider"><see cref="IExternalScopeProvider"/>.</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 categoryName, IExternalScopeProvider?scopeProvider, LogLevel logLevel, EventId eventId, TState state, Exception?exception, Func <TState, Exception?, string> formatter) { Debug.Assert(formatter != null); LoggerJsonMessage Message = new LoggerJsonMessage() { TimestampUtc = DateTime.UtcNow, ThreadId = Thread.CurrentThread.ManagedThreadId, EventId = eventId.Id != 0 ? eventId.Id : null, CategoryName = categoryName, LogLevel = logLevel switch { Microsoft.Extensions.Logging.LogLevel.Information => "Information", Microsoft.Extensions.Logging.LogLevel.Warning => "Warning", Microsoft.Extensions.Logging.LogLevel.Error => "Error", Microsoft.Extensions.Logging.LogLevel.Critical => "Critical", Microsoft.Extensions.Logging.LogLevel.Trace => "Trace", Microsoft.Extensions.Logging.LogLevel.Debug => "Debug", Microsoft.Extensions.Logging.LogLevel.None => "None", _ => throw new NotSupportedException($"LogLevel [{logLevel}] is not supported."), } }; scopeProvider?.ForEachScope(s_ParseScopeItem, Message); Message.GroupName = Message._Group?.GroupName; if (exception != null) { Message.Exception = LoggerJsonMessageException.FromException(exception); } if (state is FormattedLogValues formattedLogValues) { foreach (KeyValuePair <string, object?> Item in formattedLogValues) { AddStateItemToMessage(state, Message, Item); } } else if (state is IReadOnlyList <KeyValuePair <string, object?> > stateList) { for (int i = 0; i < stateList.Count; i++) { AddStateItemToMessage(state, Message, stateList[i]); } } else if (state is IEnumerable <KeyValuePair <string, object?> > stateValues) { foreach (KeyValuePair <string, object?> Item in stateValues) { AddStateItemToMessage(state, Message, Item); } } if (string.IsNullOrEmpty(Message.Content)) { string FormattedMessage = formatter(state, null); if (FormattedMessage != "[null]") { Message.Content = FormattedMessage; } } return(Message); }
/// <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); }