Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
824c4d3
structured logger formatter prototype
maryamariyan Apr 15, 2020
833b53d
add ref to Linq + add public apis
maryamariyan Apr 15, 2020
1f02cdc
New APIs for Structured Log Formatting
maryamariyan May 12, 2020
8ecb51e
Apply review feedback
maryamariyan May 14, 2020
ed1f429
nit cleanup
maryamariyan May 14, 2020
4516d5a
- messages can color vars in messages (Compact)
maryamariyan May 15, 2020
ae2e2fa
TODO
maryamariyan May 15, 2020
b89910d
Rename to XConsoleLog...
maryamariyan May 15, 2020
ecf8bdd
FormatterNames const not instance properties
maryamariyan May 15, 2020
fc946f0
add AddCompact and other helpers
maryamariyan May 15, 2020
8ebc659
Rename AddCompactFormatter for short?
maryamariyan May 15, 2020
5141aba
Revert "Rename AddCompactFormatter for short?"
maryamariyan May 21, 2020
6d04fb4
Add back deprecated APIs
maryamariyan May 21, 2020
b9a004e
exception messaging in compact remains single line
maryamariyan May 21, 2020
44906fb
cleanup
maryamariyan May 26, 2020
bdb4244
rename default to colored
maryamariyan May 26, 2020
d2e187a
deprecate ConsoleLoggerFormat
maryamariyan May 26, 2020
bc056e2
minor rename
maryamariyan May 26, 2020
6ffc7e7
Rename back to default formatter
maryamariyan May 27, 2020
58d3e88
triple slash comments on new helpers
maryamariyan May 28, 2020
f514a8c
slight impl fixup
maryamariyan May 28, 2020
c4bc2ef
ConsoleLoggerProvider: keep ctor not deprecate
maryamariyan May 28, 2020
0b41bed
Added triple slash comments
maryamariyan May 28, 2020
fa81978
corner cases
maryamariyan May 29, 2020
22d2eff
version 3 log formatter
maryamariyan Jun 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add back deprecated APIs
Move Options properties to common base class
Only Keep IConsoleLogFormatter.Format<TState>
  • Loading branch information
maryamariyan committed Jun 11, 2020
commit 6d04fb4e958a6e2675f1e1ab432afb8052425ee3
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ public static partial class ConsoleLoggerExtensions
}
namespace Microsoft.Extensions.Logging.Console
{
public partial class CompactLogFormatterOptions
public abstract partial class BaseOptions
{
public CompactLogFormatterOptions() { }
public bool DisableColors { get { throw null; } set { } }
protected BaseOptions() { }
public bool IncludeScopes { get { throw null; } set { } }
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
public string TimestampFormat { get { throw null; } set { } }
public bool UseUtcTimestamp { get { throw null; } set { } }
}
public partial class CompactLogFormatterOptions : Microsoft.Extensions.Logging.Console.BaseOptions
{
public CompactLogFormatterOptions() { }
public bool DisableColors { get { throw null; } set { } }
}
public static partial class ConsoleLogFormatterNames
{
public const string Compact = "Compact";
Expand All @@ -42,15 +46,20 @@ public enum ConsoleLoggerFormat
Default = 0,
Systemd = 1,
}
public partial class ConsoleLoggerOptions
public partial class ConsoleLoggerOptions : Microsoft.Extensions.Logging.Console.BaseOptions
{
public ConsoleLoggerOptions() { }
[System.ObsoleteAttribute("ConsoleLoggerOptions.DisableColors has been deprecated. Please use DefaultConsoleLogFormatterOptions.DisableColors instead.", false)]
public bool DisableColors { get { throw null; } set { } }
[System.ObsoleteAttribute("ConsoleLoggerOptions.Format has been deprecated. Please use ConsoleLoggerOptions.FormatterName instead.", false)]
public Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat Format { get { throw null; } set { } }
public string Formatter { get { throw null; } set { } }
public string FormatterName { get { throw null; } set { } }
}
[Microsoft.Extensions.Logging.ProviderAliasAttribute("Console")]
public partial class ConsoleLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider, Microsoft.Extensions.Logging.ISupportExternalScope, System.IDisposable
{
[System.ObsoleteAttribute("ConsoleLoggerProvider.ctor has been deprecated.", false)]
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options) { }
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.Console.IConsoleLogFormatter> formatters) { }
public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { throw null; }
public void Dispose() { }
Expand All @@ -63,43 +72,31 @@ public readonly partial struct ConsoleMessage
public readonly System.ConsoleColor? Foreground;
public ConsoleMessage(string message, System.ConsoleColor? background = default(System.ConsoleColor?), System.ConsoleColor? foreground = default(System.ConsoleColor?)) { throw null; }
}
public partial class DefaultConsoleLogFormatterOptions
public partial class DefaultConsoleLogFormatterOptions : Microsoft.Extensions.Logging.Console.BaseOptions
{
public DefaultConsoleLogFormatterOptions() { }
public bool DisableColors { get { throw null; } set { } }
public bool IncludeScopes { get { throw null; } set { } }
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
public string TimestampFormat { get { throw null; } set { } }
public bool UseUtcTimestamp { get { throw null; } set { } }
}
public partial interface IConsoleLogFormatter
{
string Name { get; }
Microsoft.Extensions.Logging.Console.LogMessageEntry Format(Microsoft.Extensions.Logging.LogLevel logLevel, string logName, int eventId, string message, System.Exception exception, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider);
BaseOptions Options { get; }
Microsoft.Extensions.Logging.Console.LogMessageEntry Format<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, string logName, int eventId, TState state, System.Exception exception, System.Func<TState, System.Exception, string> formatter, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider);
}
public partial class JsonConsoleLogFormatterOptions
public partial class JsonConsoleLogFormatterOptions : Microsoft.Extensions.Logging.Console.BaseOptions
{
public JsonConsoleLogFormatterOptions() { }
public bool IncludeScopes { get { throw null; } set { } }
public System.Text.Json.JsonSerializerOptions JsonSerializerOptions { get { throw null; } set { } }
public System.Text.Json.JsonWriterOptions JsonWriterOptions { get { throw null; } set { } }
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
public string TimestampFormat { get { throw null; } set { } }
public bool UseUtcTimestamp { get { throw null; } set { } }
}
public readonly partial struct LogMessageEntry
{
public readonly bool LogAsError;
public readonly Microsoft.Extensions.Logging.Console.ConsoleMessage[] Messages;
public LogMessageEntry(Microsoft.Extensions.Logging.Console.ConsoleMessage[] messages, bool logAsError = false) { throw null; }
}
public partial class SystemdConsoleLogFormatterOptions
public partial class SystemdConsoleLogFormatterOptions : Microsoft.Extensions.Logging.Console.BaseOptions
{
public SystemdConsoleLogFormatterOptions() { }
public bool IncludeScopes { get { throw null; } set { } }
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
public string TimestampFormat { get { throw null; } set { } }
public bool UseUtcTimestamp { get { throw null; } set { } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}
}

public virtual void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception exception)
{
var entry = Formatter.Format(logLevel, logName, eventId, message, exception, ScopeProvider);
_queueProcessor.EnqueueMessage(entry);
}


public bool IsEnabled(LogLevel logLevel)
{
return logLevel != LogLevel.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,50 @@ namespace Microsoft.Extensions.Logging.Console
/// <summary>
/// Options for a <see cref="ConsoleLogger"/>.
/// </summary>
public class ConsoleLoggerOptions
public class ConsoleLoggerOptions : BaseOptions
{
[System.ObsoleteAttribute("ConsoleLoggerOptions.DisableColors has been deprecated. Please use DefaultConsoleLogFormatterOptions.DisableColors instead.", false)]
public bool DisableColors { get; set; }

/// <summary>
/// Gets or sets log message format. Defaults to <see cref="ConsoleLoggerFormat.Default" />.
/// </summary>
[System.ObsoleteAttribute("ConsoleLoggerOptions.Format has been deprecated. Please use ConsoleLoggerOptions.FormatterName instead.", false)]
public ConsoleLoggerFormat Format
{
get
{
try {
return (ConsoleLoggerFormat) Enum.Parse(typeof(ConsoleLoggerFormat), Formatter);
}
catch (ArgumentException) {
return ConsoleLoggerFormat.Default;
}
if (FormatterName != null && FormatterName.Equals(ConsoleLogFormatterNames.Systemd, StringComparison.OrdinalIgnoreCase))
return ConsoleLoggerFormat.Systemd;
return ConsoleLoggerFormat.Default;
}
set
{
if (value == ConsoleLoggerFormat.Systemd)
{
Formatter = Enum.GetName(typeof(ConsoleLoggerFormat), ConsoleLoggerFormat.Systemd);
FormatterName = ConsoleLogFormatterNames.Systemd;
}
else
{
Formatter = Enum.GetName(typeof(ConsoleLoggerFormat), ConsoleLoggerFormat.Default);
FormatterName = ConsoleLogFormatterNames.Default;
}
}
}

/// <summary>
///
/// </summary>
public string Formatter { get; set; }
public string FormatterName { get; set; }
}

public abstract class BaseOptions
{
public bool IncludeScopes { get; set; }

public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get; set; }

public string TimestampFormat { get; set; }

public bool UseUtcTimestamp { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class ConsoleLoggerProvider : ILoggerProvider, ISupportExternalScope
private IDisposable _optionsReloadToken;
private IExternalScopeProvider _scopeProvider = NullExternalScopeProvider.Instance;

[System.ObsoleteAttribute("ConsoleLoggerProvider.ctor has been deprecated.", false)]
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options)
: this(options, Enumerable.Empty<IConsoleLogFormatter>())
{
; // todo: check workflow. maybe we should always have 4 formatters prepped instead?
}

/// <summary>
/// Creates an instance of <see cref="ConsoleLoggerProvider"/>.
/// </summary>
Expand Down Expand Up @@ -56,27 +63,40 @@ public ConsoleLoggerProvider(IOptionsMonitor<ConsoleLoggerOptions> options, IEnu
private void ReloadLoggerOptions(ConsoleLoggerOptions options)
{
string nameFromFormat = Enum.GetName(typeof(ConsoleLoggerFormat), options?.Format);
_formatters.TryGetValue(options?.Formatter ?? nameFromFormat, out IConsoleLogFormatter logFormatter);
_formatters.TryGetValue(options?.FormatterName ?? nameFromFormat, out IConsoleLogFormatter logFormatter);
if (logFormatter == null)
{
logFormatter = _formatters[nameFromFormat];
}
UpdateFormatterOptions(logFormatter, options);

foreach (var logger in _loggers)
{
logger.Value.Formatter = logFormatter;
}
}

private void UpdateFormatterOptions(IConsoleLogFormatter formatter, ConsoleLoggerOptions fromOptions)
{
formatter.Options.IncludeScopes = fromOptions.IncludeScopes;
formatter.Options.LogToStandardErrorThreshold = fromOptions.LogToStandardErrorThreshold;
formatter.Options.TimestampFormat = fromOptions.TimestampFormat;
formatter.Options.UseUtcTimestamp = fromOptions.UseUtcTimestamp;
// kept for deprecated api:
if (formatter is DefaultConsoleLogFormatter dFormatter)
dFormatter.FormatterOptions.DisableColors = fromOptions.DisableColors;
}

/// <inheritdoc />
public ILogger CreateLogger(string name)
{
string nameFromFormat = Enum.GetName(typeof(ConsoleLoggerFormat), _options.CurrentValue.Format);
_formatters.TryGetValue(_options.CurrentValue.Formatter ?? nameFromFormat, out IConsoleLogFormatter logFormatter);
_formatters.TryGetValue(_options.CurrentValue.FormatterName ?? nameFromFormat, out IConsoleLogFormatter logFormatter);
if (logFormatter == null)
{
logFormatter = _formatters[nameFromFormat];
}
UpdateFormatterOptions(logFormatter, _options.CurrentValue);

return _loggers.GetOrAdd(name, loggerName => new ConsoleLogger(name, _messageQueue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void Dispose()
_optionsReloadToken?.Dispose();
}

public BaseOptions Options
{
get
{
return FormatterOptions as BaseOptions;
}
}

public CompactLogFormatterOptions FormatterOptions { get; set; }

public string Name => ConsoleLogFormatterNames.Compact;
Expand All @@ -56,14 +64,10 @@ public LogMessageEntry Format<TState>(LogLevel logLevel, string logName, int eve
{
return FormatHelper(logLevel, logName, eventId, message, exception, scopeProvider, state);
}
// TODO: test use case:
return default;
}

public LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider)
{
return FormatHelper<object>(logLevel, logName, eventId, message, exception, scopeProvider, null);
}

private LogMessageEntry FormatHelper<TState>(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider, TState scope)
{
var messages = new List<ConsoleMessage>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@

namespace Microsoft.Extensions.Logging.Console
{
public class CompactLogFormatterOptions
public class CompactLogFormatterOptions : BaseOptions
{
public CompactLogFormatterOptions() { }

/// <summary>
/// Includes scopes when <see langword="true" />.
/// </summary>
public bool IncludeScopes { get; set; }

/// <summary>
/// Disables colors when <see langword="true" />.
/// </summary>
public bool DisableColors { get; set; }

/// <summary>
/// Gets or sets value indicating the minimum level of messaged that would get written to <c>Console.Error</c>.
/// </summary>
public LogLevel LogToStandardErrorThreshold { get; set; } = LogLevel.None;

/// <summary>
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.
/// </summary>
public string TimestampFormat { get; set; }

/// <summary>
/// Gets or sets indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to <c>false</c>.
/// </summary>
public bool UseUtcTimestamp { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public void Dispose()

public string Name => ConsoleLogFormatterNames.Default;

public BaseOptions Options
{
get
{
return FormatterOptions as BaseOptions;
}
}

public DefaultConsoleLogFormatterOptions FormatterOptions { get; set; }

public LogMessageEntry Format<TState>(LogLevel logLevel, string logName, int eventId, TState state, Exception exception, Func<TState, Exception, string> formatter, IExternalScopeProvider scopeProvider)
Expand All @@ -60,7 +68,7 @@ public LogMessageEntry Format<TState>(LogLevel logLevel, string logName, int eve
return default;
}

public LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider)
private LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider)
{
var logBuilder = _logBuilder;
_logBuilder = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@

namespace Microsoft.Extensions.Logging.Console
{
public class DefaultConsoleLogFormatterOptions
public class DefaultConsoleLogFormatterOptions : BaseOptions
{
public DefaultConsoleLogFormatterOptions() { }

/// <summary>
/// Includes scopes when <see langword="true" />.
/// </summary>
public bool IncludeScopes { get; set; }

/// <summary>
/// Disables colors when <see langword="true" />.
/// </summary>
public bool DisableColors { get; set; }

/// <summary>
/// Gets or sets value indicating the minimum level of messaged that would get written to <c>Console.Error</c>.
/// </summary>
public LogLevel LogToStandardErrorThreshold { get; set; } = LogLevel.None;

/// <summary>
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.
/// </summary>
public string TimestampFormat { get; set; }

/// <summary>
/// Gets or sets indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to <c>false</c>.
/// </summary>
public bool UseUtcTimestamp { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.Logging.Console
public interface IConsoleLogFormatter
{
string Name { get; }
LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider);
BaseOptions Options { get; }
LogMessageEntry Format<TState>(LogLevel logLevel, string logName, int eventId, TState state, Exception exception, Func<TState, Exception, string> formatter, IExternalScopeProvider scopeProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public LogMessageEntry Format<TState>(LogLevel logLevel, string logName, int eve
return default;
}

public LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider)
private LogMessageEntry Format(LogLevel logLevel, string logName, int eventId, string message, Exception exception, IExternalScopeProvider scopeProvider)
{
var logBuilder = _logBuilder;
_logBuilder = null;
Expand Down Expand Up @@ -190,6 +190,14 @@ private void GetScopeInformation(Utf8JsonWriter writer, IExternalScopeProvider s
}
}

public BaseOptions Options
{
get
{
return FormatterOptions as BaseOptions;
}
}

public JsonConsoleLogFormatterOptions FormatterOptions { get; set; }

private void ReloadLoggerOptions(JsonConsoleLogFormatterOptions options)
Expand Down
Loading