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);
57 if (
string.IsNullOrEmpty(message) && exception is
null)
60 string eventSuffix = eventId.Id != 0 || !
string.IsNullOrWhiteSpace(eventId.Name)
61 ? $
" [{eventId.Id}:{eventId.Name ?? string.Empty}]"
64 string line = $
"[{FormatLevel(logLevel)}] {categoryName}{eventSuffix}: {message}";
65 if (exception is not
null)
66 line += Environment.NewLine + exception;
68 TextWriter writer = logLevel >= LogLevel.Warning
71 writer.WriteLine(line);
74 private static string FormatLevel(LogLevel level) => level
switch
76 LogLevel.Trace =>
"trace",
77 LogLevel.Debug =>
"debug",
78 LogLevel.Information =>
"info",
79 LogLevel.Warning =>
"warn",
80 LogLevel.Error =>
"error",
81 LogLevel.Critical =>
"critical",
82 _ => level.ToString().ToLowerInvariant()