From 84edd6636d7860a81ec740a1ea1637ad6287158c Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Sun, 17 Apr 2016 21:58:34 -0500 Subject: [PATCH] fix saving the log level --- PlexRequests.Core/PlexRequests.Core.csproj | 1 + .../SettingModels/LogSettings.cs | 36 +++++++++++++++++++ PlexRequests.UI/Bootstrapper.cs | 1 + PlexRequests.UI/Modules/AdminModule.cs | 11 ++++++ PlexRequests.UI/Program.cs | 15 ++++++-- PlexRequests.UI/Views/Admin/Logs.cshtml | 13 ++++--- 6 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 PlexRequests.Core/SettingModels/LogSettings.cs diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 773d17e88..453ea859e 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -83,6 +83,7 @@ + diff --git a/PlexRequests.Core/SettingModels/LogSettings.cs b/PlexRequests.Core/SettingModels/LogSettings.cs new file mode 100644 index 000000000..bcea3127d --- /dev/null +++ b/PlexRequests.Core/SettingModels/LogSettings.cs @@ -0,0 +1,36 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SickRageSettings.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using NLog; + +namespace PlexRequests.Core.SettingModels +{ + public class LogSettings : Settings + { + public int Level { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index e39720c26..48aeb8290 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -78,6 +78,7 @@ namespace PlexRequests.UI container.Register, SettingsServiceV2>(); container.Register, SettingsServiceV2>(); container.Register, SettingsServiceV2>(); + container.Register, SettingsServiceV2>(); // Repo's container.Register, GenericRepository>(); diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 1650c4ed4..f30d0cee3 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -68,6 +68,7 @@ namespace PlexRequests.UI.Modules private ISettingsService PushbulletService { get; } private ISettingsService PushoverService { get; } private ISettingsService HeadphonesService { get; } + private ISettingsService LogService { get; } private IPlexApi PlexApi { get; } private ISonarrApi SonarrApi { get; } private IPushbulletApi PushbulletApi { get; } @@ -95,6 +96,7 @@ namespace PlexRequests.UI.Modules IRepository logsRepo, INotificationService notify, ISettingsService headphones, + ISettingsService logs, ICacheProvider cache) : base("admin") { PrService = prService; @@ -114,6 +116,7 @@ namespace PlexRequests.UI.Modules PushoverApi = pushoverApi; NotificationService = notify; HeadphonesService = headphones; + LogService = logs; Cache = cache; #if !DEBUG @@ -637,8 +640,16 @@ namespace PlexRequests.UI.Modules private Response UpdateLogLevels(int level) { + var settings = LogService.GetSettings(); + + // apply the level var newLevel = LogLevel.FromOrdinal(level); LoggingHelper.ReconfigureLogLevel(newLevel); + + //save the log settings + settings.Level = level; + LogService.SaveSettings(settings); + return Response.AsJson(new JsonResponseModel { Result = true, Message = $"The new log level is now {newLevel}"}); } diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index c16a08c08..a23309721 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -25,7 +25,6 @@ // ************************************************************************/ #endregion using System; -using System.Collections.Generic; using Microsoft.Owin.Hosting; @@ -42,8 +41,6 @@ using PlexRequests.Store.Repository; using System.Diagnostics; using FluentScheduler; - -using PlexRequests.Services; using PlexRequests.UI.Jobs; namespace PlexRequests.UI @@ -91,6 +88,7 @@ namespace PlexRequests.UI var cn = s.SetupDb(baseUrl); s.CacheQualityProfiles(); ConfigureTargets(cn); + SetupLogging(); if (port == -1) port = GetStartupPort(); @@ -157,6 +155,17 @@ namespace PlexRequests.UI LoggingHelper.ConfigureLogging(connectionString); } + private static void SetupLogging() + { + var settingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var logSettings = settingsService.GetSettings(); + + if (logSettings != null) + { + LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level)); + } + } + private static void SetupSchedulers() { TaskManager.TaskFactory = new PlexTaskFactory(); diff --git a/PlexRequests.UI/Views/Admin/Logs.cshtml b/PlexRequests.UI/Views/Admin/Logs.cshtml index 50a689d89..ed32d6552 100644 --- a/PlexRequests.UI/Views/Admin/Logs.cshtml +++ b/PlexRequests.UI/Views/Admin/Logs.cshtml @@ -76,11 +76,14 @@ url: logUrl, dataType: "json", success: function (response) { - $("#select > option").each(function (level) { - if (response[0] == level.value) { - $('#' + level.target.id).prop("selected", "selected"); - } - }); + if (response && response.length > 0) { + $("#selected > option").each(function (level) { + var $opt = $(this); + if (response[0].ordinal == level) { + $opt.prop("selected", "selected"); + } + }); + } }, error: function (e) { console.log(e);