| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using Serilog.Core; |
| | 3 | | using Serilog.Events; |
| | 4 | | using TELBlazor.Components.Core.Services.HelperServices; |
| | 5 | | using Blazored.LocalStorage; |
| | 6 | | using TELBlazor.Components.Core.Models.Logging; |
| | 7 | |
|
| | 8 | | namespace TELBlazor.Components.OptionalImplementations.Core.Services.HelperServices |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The TELBlazor Component system uses serilog in testing etc, but you can use your own logging system instead |
| | 12 | | /// </summary> |
| | 13 | | public class SerilogLogLevelSwitcherService : ILogLevelSwitcherService |
| | 14 | | { |
| | 15 | | private readonly LoggingLevelSwitch _loggingLevelSwitch; |
| | 16 | | private readonly Microsoft.Extensions.Logging.ILogger _logger; |
| | 17 | | private readonly ILocalStorageService _localStorage; |
| | 18 | | private const string LogLevelKey = "logLevel"; |
| | 19 | |
|
| | 20 | |
|
| 0 | 21 | | public bool IsInitialized { get; set; } = false; |
| | 22 | |
|
| 0 | 23 | | public SerilogLogLevelSwitcherService( |
| 0 | 24 | | LoggingLevelSwitch loggingLevelSwitch, |
| 0 | 25 | | Microsoft.Extensions.Logging.ILogger<SerilogLogLevelSwitcherService> logger, |
| 0 | 26 | | ILocalStorageService localStorage) |
| 0 | 27 | | { |
| 0 | 28 | | _loggingLevelSwitch = loggingLevelSwitch; |
| 0 | 29 | | _logger = logger; |
| 0 | 30 | | _localStorage = localStorage; |
| | 31 | | //InitializeLogLevelFromAsyncSourceIfAvailable(); Must be called from the component but the component is sup |
| | 32 | |
|
| 0 | 33 | | } |
| | 34 | | public async Task InitializeLogLevelFromAsyncSourceIfAvailable() |
| 0 | 35 | | { |
| 0 | 36 | | if (IsInitialized) return; |
| | 37 | |
|
| | 38 | | try |
| 0 | 39 | | { |
| 0 | 40 | | string storedLevel = await GetStoredLogLevelWithExpiration(); |
| 0 | 41 | | if (!string.IsNullOrEmpty(storedLevel)) |
| 0 | 42 | | { |
| 0 | 43 | | if (Enum.TryParse(storedLevel, true, out LogEventLevel logLevel) && logLevel > _loggingLevelSwitch.M |
| 0 | 44 | | { |
| 0 | 45 | | SetLogLevel(logLevel.ToString()); |
| 0 | 46 | | _logger.LogInformation("Log level initialized from local storage: {Level}", logLevel); |
| 0 | 47 | | } |
| 0 | 48 | | } |
| 0 | 49 | | IsInitialized = true; |
| 0 | 50 | | } |
| 0 | 51 | | catch (Exception ex) |
| 0 | 52 | | { |
| | 53 | | //We would do prerender check here if it is serve prerender there is no storage |
| 0 | 54 | | _logger.LogError(ex, "Error initializing log level from local storage."); |
| 0 | 55 | | } |
| 0 | 56 | | } |
| | 57 | |
|
| 0 | 58 | | public List<string> GetAvailableLogLevels() => Enum.GetNames(typeof(LogEventLevel)).ToList(); |
| | 59 | |
|
| | 60 | | public string GetCurrentLogLevel() |
| 0 | 61 | | { |
| 0 | 62 | | string logLevel = _loggingLevelSwitch.MinimumLevel.ToString(); |
| 0 | 63 | | _logger.LogInformation("Fetching current log level: {Level}", logLevel); |
| | 64 | |
|
| 0 | 65 | | return logLevel; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public string SetLogLevel(string level) |
| 0 | 69 | | { |
| 0 | 70 | | LogAllLevels("Before Change"); |
| 0 | 71 | | if (string.IsNullOrWhiteSpace(level)) |
| 0 | 72 | | { |
| 0 | 73 | | _logger.LogWarning("Attempted to set log level with an empty value."); |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | if (!Enum.TryParse(level, true, out LogEventLevel logLevel)) |
| 0 | 77 | | { |
| 0 | 78 | | _logger.LogWarning("Invalid log level received: {Level}", level); |
| | 79 | |
|
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | _logger.LogInformation("Changing log level from {OldLevel} to {NewLevel}", |
| 0 | 83 | | _loggingLevelSwitch.MinimumLevel.ToString(), logLevel.ToString()); |
| | 84 | |
|
| 0 | 85 | | _loggingLevelSwitch.MinimumLevel = logLevel; |
| 0 | 86 | | StoreLogLevelWithTimestamp(logLevel.ToString()); |
| 0 | 87 | | LogAllLevels("After Change"); |
| 0 | 88 | | return GetCurrentLogLevel(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | // Logs a message at all log levels to test visibility |
| | 92 | | private void LogAllLevels(string phase) |
| 0 | 93 | | { |
| 0 | 94 | | _logger.LogTrace("[{Phase}] TRACE level log", phase); |
| 0 | 95 | | _logger.LogDebug("[{Phase}] DEBUG level log", phase); |
| 0 | 96 | | _logger.LogInformation("[{Phase}] INFORMATION level log", phase); |
| 0 | 97 | | _logger.LogWarning("[{Phase}] WARNING level log", phase); |
| 0 | 98 | | _logger.LogError("[{Phase}] ERROR level log", phase); |
| 0 | 99 | | _logger.LogCritical("[{Phase}] CRITICAL level log", phase); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | |
|
| | 103 | | private async Task<string> GetStoredLogLevelWithExpiration() |
| 0 | 104 | | { |
| | 105 | | try |
| 0 | 106 | | { |
| 0 | 107 | | LocalStorageLogLevel? storedItem = await _localStorage.GetItemAsync<LocalStorageLogLevel>(LogLevelKey); |
| | 108 | |
|
| | 109 | |
|
| 0 | 110 | | if (storedItem == null) |
| 0 | 111 | | { |
| 0 | 112 | | return null; |
| | 113 | | } |
| | 114 | |
|
| | 115 | |
|
| 0 | 116 | | if (DateTime.UtcNow > storedItem.Expires) |
| 0 | 117 | | { |
| 0 | 118 | | await _localStorage.RemoveItemAsync(LogLevelKey); |
| | 119 | |
|
| 0 | 120 | | return null; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | return storedItem.Level; |
| | 124 | | } |
| 0 | 125 | | catch (Exception) |
| 0 | 126 | | { |
| 0 | 127 | | return null; // Return null if local storage access fails |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private async Task StoreLogLevelWithTimestamp(string level) |
| 0 | 132 | | { |
| | 133 | | try |
| 0 | 134 | | { |
| 0 | 135 | | LocalStorageLogLevel? storedItem = await _localStorage.GetItemAsync<LocalStorageLogLevel>(LogLevelKey); |
| 0 | 136 | | if (storedItem != null && (DateTime.UtcNow < storedItem.Expires || storedItem.Level == level)) |
| 0 | 137 | | { |
| 0 | 138 | | if (storedItem != null && (DateTime.UtcNow < storedItem.Expires)) |
| 0 | 139 | | { |
| | 140 | | // Expired or different level: delete |
| 0 | 141 | | await _localStorage.RemoveItemAsync(LogLevelKey); // Remove the old item |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | //its already set and we dont want to extend expiry |
| 0 | 145 | | return; |
| | 146 | | } |
| | 147 | | else |
| 0 | 148 | | { |
| 0 | 149 | | var newItem = new LocalStorageLogLevel |
| 0 | 150 | | { |
| 0 | 151 | | Level = level, |
| 0 | 152 | | Expires = DateTime.UtcNow.AddHours(24) |
| 0 | 153 | | }; |
| | 154 | |
|
| 0 | 155 | | await _localStorage.SetItemAsync(LogLevelKey, newItem); |
| 0 | 156 | | } |
| | 157 | |
|
| 0 | 158 | | } |
| 0 | 159 | | catch (Exception ex) |
| 0 | 160 | | { |
| 0 | 161 | | _logger.LogError(ex, "Error storing log level to local storage."); |
| 0 | 162 | | } |
| 0 | 163 | | } |
| | 164 | | } |
| | 165 | | } |