enhancement to send log notifications to host users

This commit is contained in:
Shaun Walker 2022-02-23 16:10:24 -05:00
parent 9ba356c47e
commit ac45f67a21
6 changed files with 66 additions and 7 deletions

View File

@ -131,7 +131,7 @@
}
else
{
await logger.LogError(LogFunction.Security, "Login Failed For Username {Username}", _username);
await logger.LogInformation(LogFunction.Security, "Login Failed For Username {Username}", _username);
AddModuleMessage(Localizer["Error.Login.Fail"], MessageType.Error);
}
}
@ -152,7 +152,7 @@
}
else
{
await logger.LogError(LogFunction.Security, "Login Failed For Username {Username}", _username);
await logger.LogInformation(LogFunction.Security, "Login Failed For Username {Username}", _username);
AddModuleMessage(Localizer["Error.Login.Fail"], MessageType.Error);
}
}

View File

@ -69,6 +69,21 @@
<option value="Warning">@Localizer["Warning"]</option>
<option value="Error">@Localizer["Error"]</option>
<option value="Critical">@Localizer["Critical"]</option>
<option value="None">@Localizer["None"]</option>
</select>
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="notificationlevel" HelpText="The Minimum Logging Level For Which Notifications Should Be Sent To Host Users." ResourceKey="NotificationLevel">Notification Level: </Label>
<div class="col-sm-9">
<select id="notificationlevel" class="form-select" @bind="@_notificationlevel">
<option value="Trace">@Localizer["Trace"]</option>
<option value="Debug">@Localizer["Debug"]</option>
<option value="Information">@Localizer["Information"]</option>
<option value="Warning">@Localizer["Warning"]</option>
<option value="Error">@Localizer["Error"]</option>
<option value="Critical">@Localizer["Critical"]</option>
<option value="None">@Localizer["None"]</option>
</select>
</div>
</div>
@ -110,6 +125,7 @@
private string _detailederrors = string.Empty;
private string _logginglevel = string.Empty;
private string _notificationlevel = string.Empty;
private string _swagger = string.Empty;
private string _packageservice = string.Empty;
@ -128,6 +144,7 @@
_detailederrors = systeminfo["detailederrors"];
_logginglevel = systeminfo["logginglevel"];
_notificationlevel = systeminfo["notificationlevel"];
_swagger = systeminfo["swagger"];
_packageservice = systeminfo["packageservice"];
}
@ -140,6 +157,7 @@
var settings = new Dictionary<string, string>();
settings.Add("detailederrors", _detailederrors);
settings.Add("logginglevel", _logginglevel);
settings.Add("notificationlevel", _notificationlevel);
settings.Add("swagger", _swagger);
settings.Add("packageservice", _packageservice);
await SystemService.UpdateSystemInfoAsync(settings);

View File

@ -231,4 +231,13 @@
<data name="RestartApplication.Text" xml:space="preserve">
<value>Restart Application</value>
</data>
<data name="None" xml:space="preserve">
<value>None</value>
</data>
<data name="NotificationLevel.HelpText" xml:space="preserve">
<value>The Minimum Logging Level For Which Notifications Should Be Sent To Host Users.</value>
</data>
<data name="NotificationLevel.Text" xml:space="preserve">
<value>Notification Level:</value>
</data>
</root>

View File

@ -38,6 +38,7 @@ namespace Oqtane.Controllers
systeminfo.Add("rendermode", _configManager.GetSetting("RenderMode", "ServerPrerendered"));
systeminfo.Add("detailederrors", _configManager.GetSetting("DetailedErrors", "false"));
systeminfo.Add("logginglevel", _configManager.GetSetting("Logging:LogLevel:Default", "Information"));
systeminfo.Add("notificationlevel", _configManager.GetSetting("Logging:LogLevel:Notify", "Error"));
systeminfo.Add("swagger", _configManager.GetSetting("UseSwagger", "true"));
systeminfo.Add("packageservice", _configManager.GetSetting("PackageService", "true"));
@ -64,6 +65,9 @@ namespace Oqtane.Controllers
case "logginglevel":
_configManager.AddOrUpdateSetting("Logging:LogLevel:Default", kvp.Value, false);
break;
case "notificationlevel":
_configManager.AddOrUpdateSetting("Logging:LogLevel:Notify", kvp.Value, false);
break;
case "swagger":
_configManager.AddOrUpdateSetting("UseSwagger", kvp.Value, false);
break;

View File

@ -18,14 +18,18 @@ namespace Oqtane.Infrastructure
private readonly IConfigManager _config;
private readonly IUserPermissions _userPermissions;
private readonly IHttpContextAccessor _accessor;
private readonly IUserRoleRepository _userRoles;
private readonly INotificationRepository _notifications;
public LogManager(ILogRepository logs, ITenantManager tenantManager, IConfigManager config, IUserPermissions userPermissions, IHttpContextAccessor accessor)
public LogManager(ILogRepository logs, ITenantManager tenantManager, IConfigManager config, IUserPermissions userPermissions, IHttpContextAccessor accessor, IUserRoleRepository userRoles, INotificationRepository notifications)
{
_logs = logs;
_tenantManager = tenantManager;
_config = config;
_userPermissions = userPermissions;
_accessor = accessor;
_userRoles = userRoles;
_notifications = notifications;
}
public void Log(LogLevel level, object @class, LogFunction function, string message, params object[] args)
@ -124,11 +128,11 @@ namespace Oqtane.Infrastructure
try
{
_logs.AddLog(log);
SendNotification(log);
}
catch (Exception ex)
catch
{
// an error occurred writing to the database
var x = ex.Message;
}
}
}
@ -188,5 +192,28 @@ namespace Oqtane.Infrastructure
}
return log;
}
private void SendNotification(Log log)
{
LogLevel notifylevel = LogLevel.Error;
var section = _config.GetSection("Logging:LogLevel:Notify");
if (section.Exists())
{
notifylevel = Enum.Parse<LogLevel>(section.Value);
}
if (Enum.Parse<LogLevel>(log.Level) >= notifylevel)
{
foreach (var userrole in _userRoles.GetUserRoles(log.SiteId.Value))
{
if (userrole.Role.Name == RoleNames.Host)
{
var url = _accessor.HttpContext.Request.Scheme + "://" + _tenantManager.GetAlias().Name + "/admin/log";
var notification = new Notification(log.SiteId.Value, null, userrole.User, "Site " + log.Level + " Notification", "Please visit " + url + " for more information", null);
_notifications.AddNotification(notification);
}
}
}
}
}
}

View File

@ -1,4 +1,4 @@
namespace Oqtane.Shared
namespace Oqtane.Shared
{
public enum LogLevel
{
@ -7,6 +7,7 @@
Information,
Warning,
Error,
Critical
Critical,
None
}
}