26 ArgumentException.ThrowIfNullOrWhiteSpace(categoryName);
27 return new BrowserConsoleLogger(categoryName);
35 private sealed class BrowserConsoleLogger(string categoryName) : ILogger
38 public IDisposable? BeginScope<TState>(TState state) where TState : notnull =>
null;
41 public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
44 public void Log<TState>(
49 Func<TState, Exception?, string> formatter)
51 ArgumentNullException.ThrowIfNull(formatter);
53 if (!IsEnabled(logLevel))
56 string message = formatter(state, exception);
58 if (
string.IsNullOrEmpty(message) && exception is
null)
61 string eventSuffix = eventId.Id != 0 || !
string.IsNullOrWhiteSpace(eventId.Name)
62 ? $
" [{eventId.Id}:{eventId.Name ?? string.Empty}]"
65 string line = $
"[{FormatLevel(logLevel)}] {categoryName}{eventSuffix}: {message}";
67 if (exception is not
null)
68 line += Environment.NewLine + exception;
70 TextWriter writer = logLevel >= LogLevel.Warning
74 writer.WriteLine(line);
77 private static string FormatLevel(LogLevel level) => level
switch
79 LogLevel.Trace =>
"trace",
80 LogLevel.Debug =>
"debug",
81 LogLevel.Information =>
"info",
82 LogLevel.Warning =>
"warn",
83 LogLevel.Error =>
"error",
84 LogLevel.Critical =>
"critical",
85 _ => level.ToString().ToLowerInvariant()