|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Text; |
5 | 6 | using Microsoft.Extensions.Logging; |
| 7 | +using Microsoft.JSInterop; |
6 | 8 |
|
7 | 9 | namespace Microsoft.AspNetCore.Components.WebAssembly.Services |
8 | 10 | { |
9 | 11 | internal class WebAssemblyConsoleLogger<T> : ILogger<T>, ILogger |
10 | 12 | { |
| 13 | + private static readonly string _loglevelPadding = ": "; |
| 14 | + private static readonly string _messagePadding; |
| 15 | + private static readonly string _newLineWithMessagePadding; |
| 16 | + private static readonly StringBuilder _logBuilder = new StringBuilder(); |
| 17 | + |
| 18 | + private readonly string _name; |
| 19 | + private readonly IJSInProcessRuntime _jsRuntime; |
| 20 | + |
| 21 | + static WebAssemblyConsoleLogger() |
| 22 | + { |
| 23 | + var logLevelString = GetLogLevelString(LogLevel.Information); |
| 24 | + _messagePadding = new string(' ', logLevelString.Length + _loglevelPadding.Length); |
| 25 | + _newLineWithMessagePadding = Environment.NewLine + _messagePadding; |
| 26 | + } |
| 27 | + |
| 28 | + public WebAssemblyConsoleLogger(IJSRuntime jsRuntime) |
| 29 | + : this(string.Empty, (IJSInProcessRuntime)jsRuntime) // Cast for DI |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + public WebAssemblyConsoleLogger(string name, IJSInProcessRuntime jsRuntime) |
| 34 | + { |
| 35 | + _name = name ?? throw new ArgumentNullException(nameof(name)); |
| 36 | + _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); |
| 37 | + } |
| 38 | + |
11 | 39 | public IDisposable BeginScope<TState>(TState state) |
12 | 40 | { |
13 | 41 | return NoOpDisposable.Instance; |
14 | 42 | } |
15 | 43 |
|
16 | 44 | public bool IsEnabled(LogLevel logLevel) |
17 | 45 | { |
18 | | - return logLevel >= LogLevel.Warning; |
| 46 | + return logLevel >= LogLevel.Warning && logLevel != LogLevel.None; |
19 | 47 | } |
20 | 48 |
|
21 | 49 | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
22 | 50 | { |
23 | | - var formattedMessage = formatter(state, exception); |
24 | | - Console.WriteLine($"[{logLevel}] {formattedMessage}"); |
| 51 | + if (!IsEnabled(logLevel)) |
| 52 | + { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (formatter == null) |
| 57 | + { |
| 58 | + throw new ArgumentNullException(nameof(formatter)); |
| 59 | + } |
| 60 | + |
| 61 | + var message = formatter(state, exception); |
| 62 | + |
| 63 | + if (!string.IsNullOrEmpty(message) || exception != null) |
| 64 | + { |
| 65 | + WriteMessage(logLevel, _name, eventId.Id, message, exception); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception exception) |
| 70 | + { |
| 71 | + lock (_logBuilder) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + CreateDefaultLogMessage(_logBuilder, logLevel, logName, eventId, message, exception); |
| 76 | + var formattedMessage = _logBuilder.ToString(); |
| 77 | + |
| 78 | + switch (logLevel) |
| 79 | + { |
| 80 | + case LogLevel.Trace: |
| 81 | + case LogLevel.Debug: |
| 82 | + // Although https://console.spec.whatwg.org/#loglevel-severity claims that |
| 83 | + // "console.debug" and "console.log" are synonyms, that doesn't match the |
| 84 | + // behavior of browsers in the real world. Chromium only displays "debug" |
| 85 | + // messages if you enable "Verbose" in the filter dropdown (which is off |
| 86 | + // by default). As such "console.debug" is the best choice for messages |
| 87 | + // with a lower severity level than "Information". |
| 88 | + _jsRuntime.InvokeVoid("console.debug", formattedMessage); |
| 89 | + break; |
| 90 | + case LogLevel.Information: |
| 91 | + _jsRuntime.InvokeVoid("console.info", formattedMessage); |
| 92 | + break; |
| 93 | + case LogLevel.Warning: |
| 94 | + _jsRuntime.InvokeVoid("console.warn", formattedMessage); |
| 95 | + break; |
| 96 | + case LogLevel.Error: |
| 97 | + _jsRuntime.InvokeVoid("console.error", formattedMessage); |
| 98 | + break; |
| 99 | + case LogLevel.Critical: |
| 100 | + // Writing to Console.Error is even more severe than calling console.error, |
| 101 | + // because it also causes the error UI (gold bar) to appear. We use Console.Error |
| 102 | + // as the signal for triggering that because it's what the underlying dotnet.wasm |
| 103 | + // runtime will do if it encounters a truly severe error outside the Blazor |
| 104 | + // code paths. |
| 105 | + Console.Error.WriteLine(formattedMessage); |
| 106 | + break; |
| 107 | + default: // LogLevel.None or invalid enum values |
| 108 | + Console.WriteLine(formattedMessage); |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + finally |
| 113 | + { |
| 114 | + _logBuilder.Clear(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void CreateDefaultLogMessage(StringBuilder logBuilder, LogLevel logLevel, string logName, int eventId, string message, Exception exception) |
| 120 | + { |
| 121 | + logBuilder.Append(GetLogLevelString(logLevel)); |
| 122 | + logBuilder.Append(_loglevelPadding); |
| 123 | + logBuilder.Append(logName); |
| 124 | + logBuilder.Append("["); |
| 125 | + logBuilder.Append(eventId); |
| 126 | + logBuilder.Append("]"); |
| 127 | + |
| 128 | + if (!string.IsNullOrEmpty(message)) |
| 129 | + { |
| 130 | + // message |
| 131 | + logBuilder.AppendLine(); |
| 132 | + logBuilder.Append(_messagePadding); |
| 133 | + |
| 134 | + var len = logBuilder.Length; |
| 135 | + logBuilder.Append(message); |
| 136 | + logBuilder.Replace(Environment.NewLine, _newLineWithMessagePadding, len, message.Length); |
| 137 | + } |
| 138 | + |
| 139 | + // Example: |
| 140 | + // System.InvalidOperationException |
| 141 | + // at Namespace.Class.Function() in File:line X |
| 142 | + if (exception != null) |
| 143 | + { |
| 144 | + // exception message |
| 145 | + logBuilder.AppendLine(); |
| 146 | + logBuilder.Append(exception.ToString()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static string GetLogLevelString(LogLevel logLevel) |
| 151 | + { |
| 152 | + switch (logLevel) |
| 153 | + { |
| 154 | + case LogLevel.Trace: |
| 155 | + return "trce"; |
| 156 | + case LogLevel.Debug: |
| 157 | + return "dbug"; |
| 158 | + case LogLevel.Information: |
| 159 | + return "info"; |
| 160 | + case LogLevel.Warning: |
| 161 | + return "warn"; |
| 162 | + case LogLevel.Error: |
| 163 | + return "fail"; |
| 164 | + case LogLevel.Critical: |
| 165 | + return "crit"; |
| 166 | + default: |
| 167 | + throw new ArgumentOutOfRangeException(nameof(logLevel)); |
| 168 | + } |
25 | 169 | } |
26 | 170 |
|
27 | 171 | private class NoOpDisposable : IDisposable |
|
0 commit comments