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

@ -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);
}
}
}
}
}
}