diff --git a/Oqtane.Client/Modules/ModuleBase.cs b/Oqtane.Client/Modules/ModuleBase.cs index 8023da67..f1a086e5 100644 --- a/Oqtane.Client/Modules/ModuleBase.cs +++ b/Oqtane.Client/Modules/ModuleBase.cs @@ -9,7 +9,6 @@ using Oqtane.UI; using System.Collections.Generic; using Microsoft.JSInterop; using System.Linq; -using Oqtane.Themes; namespace Oqtane.Modules { diff --git a/Oqtane.Client/Themes/Controls/Theme/LoginBase.cs b/Oqtane.Client/Themes/Controls/Theme/LoginBase.cs index 9f4f4071..468e398e 100644 --- a/Oqtane.Client/Themes/Controls/Theme/LoginBase.cs +++ b/Oqtane.Client/Themes/Controls/Theme/LoginBase.cs @@ -19,7 +19,6 @@ namespace Oqtane.Themes.Controls [Inject] public IUserService UserService { get; set; } [Inject] public IJSRuntime jsRuntime { get; set; } [Inject] public IServiceProvider ServiceProvider { get; set; } - [Inject] public ILogService LoggingService { get; set; } protected void LoginUser() { diff --git a/Oqtane.Client/Themes/ThemeBase.cs b/Oqtane.Client/Themes/ThemeBase.cs index 8fcb1a30..2de61b1c 100644 --- a/Oqtane.Client/Themes/ThemeBase.cs +++ b/Oqtane.Client/Themes/ThemeBase.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; +using Oqtane.Enums; using Oqtane.Models; +using Oqtane.Modules; +using Oqtane.Services; using Oqtane.Shared; using Oqtane.UI; using System; @@ -13,6 +16,9 @@ namespace Oqtane.Themes { public abstract class ThemeBase : ComponentBase, IThemeControl { + [Inject] + protected ILogService LoggingService { get; set; } + [Inject] protected IJSRuntime JSRuntime { get; set; } @@ -186,6 +192,148 @@ namespace Oqtane.Themes await interop.ScrollTo(0, 0, "smooth"); } + // logging methods + public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args) + { + LogFunction logFunction; + if (string.IsNullOrEmpty(function)) + { + // try to infer from page action + function = PageState.Action; + } + if (!Enum.TryParse(function, out logFunction)) + { + switch (function.ToLower()) + { + case "add": + logFunction = LogFunction.Create; + break; + case "edit": + logFunction = LogFunction.Update; + break; + case "delete": + logFunction = LogFunction.Delete; + break; + case "": + logFunction = LogFunction.Read; + break; + default: + logFunction = LogFunction.Other; + break; + } + } + await Log(alias, level, logFunction, exception, message, args); + } + + public async Task Log(Alias alias, LogLevel level, LogFunction function, Exception exception, string message, params object[] args) + { + int pageId = PageState.Page.PageId; + string category = GetType().AssemblyQualifiedName; + string feature = Utilities.GetTypeNameLastSegment(category, 1); + + await LoggingService.Log(alias, pageId, null, PageState.User?.UserId, category, feature, function, level, exception, message, args); + } + + public class Logger + { + private readonly ModuleBase _moduleBase; + + public Logger(ModuleBase moduleBase) + { + _moduleBase = moduleBase; + } + + public async Task LogTrace(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Trace, "", null, message, args); + } + + public async Task LogTrace(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Trace, function, null, message, args); + } + + public async Task LogTrace(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Trace, "", exception, message, args); + } + + public async Task LogDebug(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Debug, "", null, message, args); + } + + public async Task LogDebug(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Debug, function, null, message, args); + } + + public async Task LogDebug(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Debug, "", exception, message, args); + } + + public async Task LogInformation(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Information, "", null, message, args); + } + + public async Task LogInformation(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Information, function, null, message, args); + } + + public async Task LogInformation(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Information, "", exception, message, args); + } + + public async Task LogWarning(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Warning, "", null, message, args); + } + + public async Task LogWarning(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Warning, function, null, message, args); + } + + public async Task LogWarning(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Warning, "", exception, message, args); + } + + public async Task LogError(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Error, "", null, message, args); + } + + public async Task LogError(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Error, function, null, message, args); + } + + public async Task LogError(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Error, "", exception, message, args); + } + + public async Task LogCritical(string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Critical, "", null, message, args); + } + + public async Task LogCritical(LogFunction function, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Critical, function, null, message, args); + } + + public async Task LogCritical(Exception exception, string message, params object[] args) + { + await _moduleBase.Log(null, LogLevel.Critical, "", exception, message, args); + } + } + [Obsolete("ContentUrl(int fileId) is deprecated. Use FileUrl(int fileId) instead.", false)] public string ContentUrl(int fileid) {