diff --git a/PlexRequests.Api/ApiRequest.cs b/PlexRequests.Api/ApiRequest.cs index dd1f1c6d0..205089a34 100644 --- a/PlexRequests.Api/ApiRequest.cs +++ b/PlexRequests.Api/ApiRequest.cs @@ -33,6 +33,8 @@ using System.Xml.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NLog; + using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models; @@ -42,7 +44,7 @@ namespace PlexRequests.Api { public class ApiRequest : IApiRequest { - + private static Logger Log = LogManager.GetCurrentClassLogger(); /// /// An API request handler /// diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs index aef772c01..c4e0f5213 100644 --- a/PlexRequests.Api/CouchPotatoApi.cs +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -28,7 +28,7 @@ using System; using Newtonsoft.Json.Linq; -using PlexRequests.Api.Models.Movie; +using NLog; using RestSharp; @@ -41,6 +41,7 @@ namespace PlexRequests.Api Api = new ApiRequest(); } private ApiRequest Api { get; set; } + private static Logger Log = LogManager.GetCurrentClassLogger(); public bool AddMovie(string imdbid, string apiKey, string title, string baseUrl) { @@ -51,9 +52,13 @@ namespace PlexRequests.Api request.AddUrlSegment("title", title); var obj = Api.ExecuteJson(request, new Uri(baseUrl)); + Log.Trace("CP movie Add result count {0}", obj.Count); + if (obj.Count > 0) { + Log.Trace("CP movie obj[\"success\"] = {0}", obj["success"]); var result = (bool)obj["success"]; + Log.Trace("CP movie Add result {0}", result); return result; } return false; diff --git a/PlexRequests.Api/PlexRequests.Api.csproj b/PlexRequests.Api/PlexRequests.Api.csproj index 4de1b21c1..cd4c87c53 100644 --- a/PlexRequests.Api/PlexRequests.Api.csproj +++ b/PlexRequests.Api/PlexRequests.Api.csproj @@ -42,6 +42,10 @@ ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True + + ..\packages\NLog.4.2.3\lib\net45\NLog.dll + True + ..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll True diff --git a/PlexRequests.Api/packages.config b/PlexRequests.Api/packages.config index 62b36e32c..82a3f269f 100644 --- a/PlexRequests.Api/packages.config +++ b/PlexRequests.Api/packages.config @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj b/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj index 0d255c9af..0319f7604 100644 --- a/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj +++ b/PlexRequests.Core.Tests/PlexRequests.Core.Tests.csproj @@ -62,6 +62,7 @@ + diff --git a/PlexRequests.Core.Tests/app.config b/PlexRequests.Core.Tests/app.config new file mode 100644 index 000000000..ac2586ac9 --- /dev/null +++ b/PlexRequests.Core.Tests/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/PlexRequests.Helpers/LoggingHelper.cs b/PlexRequests.Helpers/LoggingHelper.cs new file mode 100644 index 000000000..b3ee3dc1b --- /dev/null +++ b/PlexRequests.Helpers/LoggingHelper.cs @@ -0,0 +1,59 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: LoggingHelper.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 System; + +using Newtonsoft.Json; + +namespace PlexRequests.Helpers +{ + public static class LoggingHelper + { + public static string DumpJson(this object value) + { + var dumpTarget = value; + //if this is a string that contains a JSON object, do a round-trip serialization to format it: + var stringValue = value as string; + if (stringValue != null) + { + if (stringValue.Trim().StartsWith("{", StringComparison.Ordinal)) + { + var obj = JsonConvert.DeserializeObject(stringValue); + dumpTarget = JsonConvert.SerializeObject(obj, Formatting.Indented); + } + else + { + dumpTarget = stringValue; + } + } + else + { + dumpTarget = JsonConvert.SerializeObject(value, Formatting.Indented); + } + return dumpTarget.ToString(); + } + } +} diff --git a/PlexRequests.Helpers/PlexRequests.Helpers.csproj b/PlexRequests.Helpers/PlexRequests.Helpers.csproj index 5c665f1c5..f19ba2b09 100644 --- a/PlexRequests.Helpers/PlexRequests.Helpers.csproj +++ b/PlexRequests.Helpers/PlexRequests.Helpers.csproj @@ -47,6 +47,7 @@ + diff --git a/PlexRequests.Store/DbConfiguration.cs b/PlexRequests.Store/DbConfiguration.cs index d345616da..bccd679c0 100644 --- a/PlexRequests.Store/DbConfiguration.cs +++ b/PlexRequests.Store/DbConfiguration.cs @@ -30,10 +30,13 @@ using System.IO; using Mono.Data.Sqlite; +using NLog; + namespace PlexRequests.Store { public class DbConfiguration : ISqliteConfiguration { + private static Logger Log = LogManager.GetCurrentClassLogger(); public DbConfiguration(SqliteFactory provider) { Factory = provider; @@ -43,8 +46,10 @@ namespace PlexRequests.Store public virtual void CheckDb() { + Log.Trace("Checking DB"); if (!File.Exists(DbFile)) { + Log.Trace("DB doesn't exist, creating a new one"); CreateDatabase(); } } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 526e992d4..38bc1d338 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -30,6 +30,8 @@ using System.Linq; using Nancy; using Nancy.Responses.Negotiation; +using NLog; + using PlexRequests.Api; using PlexRequests.Core; using PlexRequests.Core.SettingModels; @@ -63,15 +65,18 @@ namespace PlexRequests.UI.Modules private TheTvDbApi TvApi { get; } private ICacheProvider Cache { get; } private ISettingsService CpService { get; set; } + private static Logger Log = LogManager.GetCurrentClassLogger(); private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50); private Negotiator RequestLoad() { + Log.Trace("Loading Index"); return View["Search/Index"]; } private Response SearchMovie(string searchTerm) { + Log.Trace("Searching for Movie {0}", searchTerm); var movies = MovieApi.SearchMovie(searchTerm); var result = movies.Result; return Response.AsJson(result); @@ -79,6 +84,7 @@ namespace PlexRequests.UI.Modules private Response SearchTvShow(string searchTerm) { + Log.Trace("Searching for TV Show {0}", searchTerm); var tvShow = TvApi.SearchTv(searchTerm, AuthToken); if (tvShow?.data == null) @@ -134,14 +140,22 @@ namespace PlexRequests.UI.Modules private Response RequestMovie(int movieId) { + Log.Trace("Requesting movie with id {0}", movieId); var s = new SettingsService(Cache); if (s.CheckRequest(movieId)) { + Log.Trace("movie with id {0} exists", movieId); return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" }); } + Log.Trace("movie with id {0} doesnt exists", movieId); var settings = CpService.GetSettings(); + Log.Trace("Settings: "); + Log.Trace(settings.DumpJson); + var movieApi = new TheMovieDbApi(); var movieInfo = movieApi.GetMovieInformation(movieId).Result; + Log.Trace("Getting movie info from TheMovieDb"); + Log.Trace(movieInfo.DumpJson); var model = new RequestedModel { @@ -158,10 +172,12 @@ namespace PlexRequests.UI.Modules }; var cp = new CouchPotatoApi(); - + Log.Trace("Adding movie to CP"); var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.Ip); + Log.Trace("Adding movie to CP result {0}", result); if (result) { + Log.Trace("Adding movie to database requests"); s.AddRequest(movieId, model); return Response.AsJson(new { Result = true }); diff --git a/PlexRequests.UI/NLog.config b/PlexRequests.UI/NLog.config index 1a855bd56..4fb8be6cb 100644 --- a/PlexRequests.UI/NLog.config +++ b/PlexRequests.UI/NLog.config @@ -16,17 +16,14 @@ See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> - - - - + + + + - - - - - + \ No newline at end of file diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index 48fde4c94..25acf00a0 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -45,10 +45,12 @@ namespace PlexRequests.UI { class Program { + private static Logger Log = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { + Log.Trace("Getting assembly version"); WriteOutVersion(); - + var s = new Setup(); var connection = s.SetupDb(); @@ -68,15 +70,17 @@ namespace PlexRequests.UI private static void WriteOutVersion() { var assemblyVer = AssemblyHelper.GetAssemblyVersion(); + Log.Info($"Version: {assemblyVer}"); Console.WriteLine($"Version: {assemblyVer}"); } private static string GetStartupUri() { + Log.Trace("Getting startup URI"); var uri = "http://localhost:3579/"; var service = new SettingsServiceV2(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); var settings = service.GetSettings(); - + Log.Trace("Port: {0}", settings.Port); if (settings.Port != 0) { uri = $"http://localhost:{settings.Port}";