Merge pull request #259 from chlupac/fix

ModuleBase fix
This commit is contained in:
Shaun Walker 2020-03-06 10:58:28 -05:00 committed by GitHub
commit b09b75f919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,11 +10,12 @@ namespace Oqtane.Modules
{ {
public class ModuleBase : ComponentBase, IModuleControl public class ModuleBase : ComponentBase, IModuleControl
{ {
public Logger logger { get; set; } private Logger _logger;
protected Logger logger => _logger ?? (_logger = new Logger(this));
public ModuleBase() public ModuleBase()
{ {
logger = new Logger(this);
} }
[Inject] [Inject]
@ -87,14 +88,14 @@ namespace Oqtane.Modules
return EditUrl(ModuleState.ModuleId, action, parameters); return EditUrl(ModuleState.ModuleId, action, parameters);
} }
public string EditUrl(int moduleid, string action) public string EditUrl(int moduleId, string action)
{ {
return EditUrl(moduleid, action, ""); return EditUrl(moduleId, action, "");
} }
public string EditUrl(int moduleid, string action, string parameters) public string EditUrl(int moduleId, string action, string parameters)
{ {
return EditUrl(PageState.Page.Path, moduleid, action, parameters); return EditUrl(PageState.Page.Path, moduleId, action, parameters);
} }
public string EditUrl(string path, int moduleid, string action, string parameters) public string EditUrl(string path, int moduleid, string action, string parameters)
@ -128,16 +129,16 @@ namespace Oqtane.Modules
// logging methods // logging methods
public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args) public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args)
{ {
int PageId = PageState.Page.PageId; int pageId = PageState.Page.PageId;
int ModuleId = ModuleState.ModuleId; int moduleId = ModuleState.ModuleId;
int? UserId = null; int? userId = null;
if (PageState.User != null) if (PageState.User != null)
{ {
UserId = PageState.User.UserId; userId = PageState.User.UserId;
} }
string category = GetType().AssemblyQualifiedName; string category = GetType().AssemblyQualifiedName;
string feature = Utilities.GetTypeNameLastSegment(category, 1); string feature = Utilities.GetTypeNameLastSegment(category, 1);
LogFunction logfunction; LogFunction logFunction;
if (string.IsNullOrEmpty(function)) if (string.IsNullOrEmpty(function))
{ {
function = PageState.Action; function = PageState.Action;
@ -145,92 +146,92 @@ namespace Oqtane.Modules
switch (function.ToLower()) switch (function.ToLower())
{ {
case "add": case "add":
logfunction = LogFunction.Create; logFunction = LogFunction.Create;
break; break;
case "edit": case "edit":
logfunction = LogFunction.Update; logFunction = LogFunction.Update;
break; break;
case "delete": case "delete":
logfunction = LogFunction.Delete; logFunction = LogFunction.Delete;
break; break;
default: default:
logfunction = LogFunction.Read; logFunction = LogFunction.Read;
break; break;
} }
if (feature == "Login") if (feature == "Login")
{ {
logfunction = LogFunction.Security; 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, logFunction, level, exception, message, args);
} }
public class Logger public class Logger
{ {
private ModuleBase modulebase; private readonly ModuleBase _moduleBase;
public Logger(ModuleBase modulebase) public Logger(ModuleBase moduleBase)
{ {
modulebase = modulebase; _moduleBase = moduleBase;
} }
public async Task LogTrace(string message, params object[] args) public async Task LogTrace(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Trace, "", null, message, args); await _moduleBase.Log(null, LogLevel.Trace, "", null, message, args);
} }
public async Task LogTrace(Exception exception, string message, params object[] args) public async Task LogTrace(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Trace, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Trace, "", exception, message, args);
} }
public async Task LogDebug(string message, params object[] args) public async Task LogDebug(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Debug, "", null, message, args); await _moduleBase.Log(null, LogLevel.Debug, "", null, message, args);
} }
public async Task LogDebug(Exception exception, string message, params object[] args) public async Task LogDebug(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Debug, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Debug, "", exception, message, args);
} }
public async Task LogInformation(string message, params object[] args) public async Task LogInformation(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Information, "", null, message, args); await _moduleBase.Log(null, LogLevel.Information, "", null, message, args);
} }
public async Task LogInformation(Exception exception, string message, params object[] args) public async Task LogInformation(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Information, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Information, "", exception, message, args);
} }
public async Task LogWarning(string message, params object[] args) public async Task LogWarning(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Warning, "", null, message, args); await _moduleBase.Log(null, LogLevel.Warning, "", null, message, args);
} }
public async Task LogWarning(Exception exception, string message, params object[] args) public async Task LogWarning(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Warning, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Warning, "", exception, message, args);
} }
public async Task LogError(string message, params object[] args) public async Task LogError(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Error, "", null, message, args); await _moduleBase.Log(null, LogLevel.Error, "", null, message, args);
} }
public async Task LogError(Exception exception, string message, params object[] args) public async Task LogError(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Error, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Error, "", exception, message, args);
} }
public async Task LogCritical(string message, params object[] args) public async Task LogCritical(string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Critical, "", null, message, args); await _moduleBase.Log(null, LogLevel.Critical, "", null, message, args);
} }
public async Task LogCritical(Exception exception, string message, params object[] args) public async Task LogCritical(Exception exception, string message, params object[] args)
{ {
await modulebase.Log(null, LogLevel.Critical, "", exception, message, args); await _moduleBase.Log(null, LogLevel.Critical, "", exception, message, args);
} }
} }
} }