From f6c46878c651c23eee6529434695a36dd8c83e38 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 30 Aug 2021 08:46:53 -0400 Subject: [PATCH] add new overloads to client-side logging methods to include LogFunction enum parameter so that it can be specified explicitly rather than only being inferred from the page action --- Oqtane.Client/Modules/Admin/Login/Index.razor | 13 +-- Oqtane.Client/Modules/ModuleBase.cs | 90 +++++++++++++------ 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/Oqtane.Client/Modules/Admin/Login/Index.razor b/Oqtane.Client/Modules/Admin/Login/Index.razor index 70ff6b13..8399ae95 100644 --- a/Oqtane.Client/Modules/Admin/Login/Index.razor +++ b/Oqtane.Client/Modules/Admin/Login/Index.razor @@ -59,7 +59,7 @@ public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Anonymous; public override List Resources => new List() -{ + { new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" } }; @@ -84,10 +84,12 @@ if (user != null) { + await logger.LogInformation(LogFunction.Security, "Email Verified For For Username {Username}", _username); _message = Localizer["Success.Account.Verified"]; } else { + await logger.LogError(LogFunction.Security, "Email Verification Failed For Username {Username}", _username); _message = Localizer["Message.Account.NotVerfied"]; _type = MessageType.Warning; } @@ -121,7 +123,7 @@ if (user.IsAuthenticated) { - await logger.LogInformation("Login Successful For Username {Username}", _username); + await logger.LogInformation(LogFunction.Security, "Login Successful For Username {Username}", _username); // server-side Blazor needs to post to the Login page so that the cookies are set correctly var fields = new { __RequestVerificationToken = SiteState.AntiForgeryToken, username = _username, password = _password, remember = _remember, returnurl = _returnUrl }; string url = Utilities.TenantUrl(PageState.Alias, "/pages/login/"); @@ -129,7 +131,7 @@ } else { - await logger.LogInformation("Login Failed For Username {Username}", _username); + await logger.LogError(LogFunction.Security, "Login Failed For Username {Username}", _username); AddModuleMessage(Localizer["Error.Login.Fail"], MessageType.Error); } } @@ -143,14 +145,14 @@ user = await UserService.LoginUserAsync(user, true, _remember); if (user.IsAuthenticated) { - await logger.LogInformation("Login Successful For Username {Username}", _username); + await logger.LogInformation(LogFunction.Security, "Login Successful For Username {Username}", _username); var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider)); authstateprovider.NotifyAuthenticationChanged(); NavigationManager.NavigateTo(NavigateUrl(_returnUrl, true)); } else { - await logger.LogInformation("Login Failed For Username {Username}", _username); + await logger.LogError(LogFunction.Security, "Login Failed For Username {Username}", _username); AddModuleMessage(Localizer["Error.Login.Fail"], MessageType.Error); } } @@ -174,6 +176,7 @@ if (user != null) { await UserService.ForgotPasswordAsync(user); + await logger.LogInformation(LogFunction.Security, "Password Reset Notification Sent For Username {Username}", _username); _message = "Please Check The Email Address Associated To Your User Account For A Password Reset Notification"; } else diff --git a/Oqtane.Client/Modules/ModuleBase.cs b/Oqtane.Client/Modules/ModuleBase.cs index e7c6658d..7a6aad03 100644 --- a/Oqtane.Client/Modules/ModuleBase.cs +++ b/Oqtane.Client/Modules/ModuleBase.cs @@ -205,6 +205,38 @@ namespace Oqtane.Modules // 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 = ModuleState.PageId; int moduleId = ModuleState.ModuleId; @@ -215,34 +247,8 @@ namespace Oqtane.Modules } string category = GetType().AssemblyQualifiedName; string feature = Utilities.GetTypeNameLastSegment(category, 1); - LogFunction logFunction; - if (string.IsNullOrEmpty(function)) - { - function = PageState.Action; - } - switch (function.ToLower()) - { - case "add": - logFunction = LogFunction.Create; - break; - case "edit": - logFunction = LogFunction.Update; - break; - - case "delete": - logFunction = LogFunction.Delete; - break; - - default: - logFunction = LogFunction.Read; - break; - } - if (feature == "Login") - { - logFunction = LogFunction.Security; - } - await LoggingService.Log(alias, pageId, moduleId, userId, category, feature, logFunction, level, exception, message, args); + await LoggingService.Log(alias, pageId, moduleId, userId, category, feature, function, level, exception, message, args); } public class Logger @@ -259,6 +265,11 @@ namespace Oqtane.Modules 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); @@ -269,6 +280,11 @@ namespace Oqtane.Modules 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); @@ -279,6 +295,11 @@ namespace Oqtane.Modules 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); @@ -289,6 +310,11 @@ namespace Oqtane.Modules 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); @@ -299,6 +325,11 @@ namespace Oqtane.Modules 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); @@ -309,6 +340,11 @@ namespace Oqtane.Modules 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);