Added logging

pull/13/head
tidusjar 9 years ago
parent 73635e06c3
commit 3dcf55ce7a

@ -33,6 +33,8 @@ using System.Xml.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NLog;
using PlexRequests.Api.Interfaces; using PlexRequests.Api.Interfaces;
using PlexRequests.Api.Models; using PlexRequests.Api.Models;
@ -42,7 +44,7 @@ namespace PlexRequests.Api
{ {
public class ApiRequest : IApiRequest public class ApiRequest : IApiRequest
{ {
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary> /// <summary>
/// An API request handler /// An API request handler
/// </summary> /// </summary>

@ -28,7 +28,7 @@ using System;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using PlexRequests.Api.Models.Movie; using NLog;
using RestSharp; using RestSharp;
@ -41,6 +41,7 @@ namespace PlexRequests.Api
Api = new ApiRequest(); Api = new ApiRequest();
} }
private ApiRequest Api { get; set; } private ApiRequest Api { get; set; }
private static Logger Log = LogManager.GetCurrentClassLogger();
public bool AddMovie(string imdbid, string apiKey, string title, string baseUrl) public bool AddMovie(string imdbid, string apiKey, string title, string baseUrl)
{ {
@ -51,9 +52,13 @@ namespace PlexRequests.Api
request.AddUrlSegment("title", title); request.AddUrlSegment("title", title);
var obj = Api.ExecuteJson<JObject>(request, new Uri(baseUrl)); var obj = Api.ExecuteJson<JObject>(request, new Uri(baseUrl));
Log.Trace("CP movie Add result count {0}", obj.Count);
if (obj.Count > 0) if (obj.Count > 0)
{ {
Log.Trace("CP movie obj[\"success\"] = {0}", obj["success"]);
var result = (bool)obj["success"]; var result = (bool)obj["success"];
Log.Trace("CP movie Add result {0}", result);
return result; return result;
} }
return false; return false;

@ -42,6 +42,10 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.2.3\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll</HintPath> <HintPath>..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll</HintPath>
<Private>True</Private> <Private>True</Private>

@ -3,6 +3,7 @@
<package id="Dapper" version="1.42" targetFramework="net452" /> <package id="Dapper" version="1.42" targetFramework="net452" />
<package id="Nancy" version="1.4.3" targetFramework="net452" /> <package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" /> <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" /> <package id="RestSharp" version="105.2.3" targetFramework="net452" />
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" /> <package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" />
</packages> </packages>

@ -62,6 +62,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

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

@ -47,6 +47,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyHelper.cs" /> <Compile Include="AssemblyHelper.cs" />
<Compile Include="ICacheProvider.cs" /> <Compile Include="ICacheProvider.cs" />
<Compile Include="LoggingHelper.cs" />
<Compile Include="MemoryCacheProvider.cs" /> <Compile Include="MemoryCacheProvider.cs" />
<Compile Include="ObjectCopier.cs" /> <Compile Include="ObjectCopier.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

@ -30,10 +30,13 @@ using System.IO;
using Mono.Data.Sqlite; using Mono.Data.Sqlite;
using NLog;
namespace PlexRequests.Store namespace PlexRequests.Store
{ {
public class DbConfiguration : ISqliteConfiguration public class DbConfiguration : ISqliteConfiguration
{ {
private static Logger Log = LogManager.GetCurrentClassLogger();
public DbConfiguration(SqliteFactory provider) public DbConfiguration(SqliteFactory provider)
{ {
Factory = provider; Factory = provider;
@ -43,8 +46,10 @@ namespace PlexRequests.Store
public virtual void CheckDb() public virtual void CheckDb()
{ {
Log.Trace("Checking DB");
if (!File.Exists(DbFile)) if (!File.Exists(DbFile))
{ {
Log.Trace("DB doesn't exist, creating a new one");
CreateDatabase(); CreateDatabase();
} }
} }

@ -30,6 +30,8 @@ using System.Linq;
using Nancy; using Nancy;
using Nancy.Responses.Negotiation; using Nancy.Responses.Negotiation;
using NLog;
using PlexRequests.Api; using PlexRequests.Api;
using PlexRequests.Core; using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
@ -63,15 +65,18 @@ namespace PlexRequests.UI.Modules
private TheTvDbApi TvApi { get; } private TheTvDbApi TvApi { get; }
private ICacheProvider Cache { get; } private ICacheProvider Cache { get; }
private ISettingsService<CouchPotatoSettings> CpService { get; set; } private ISettingsService<CouchPotatoSettings> CpService { get; set; }
private static Logger Log = LogManager.GetCurrentClassLogger();
private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50); private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50);
private Negotiator RequestLoad() private Negotiator RequestLoad()
{ {
Log.Trace("Loading Index");
return View["Search/Index"]; return View["Search/Index"];
} }
private Response SearchMovie(string searchTerm) private Response SearchMovie(string searchTerm)
{ {
Log.Trace("Searching for Movie {0}", searchTerm);
var movies = MovieApi.SearchMovie(searchTerm); var movies = MovieApi.SearchMovie(searchTerm);
var result = movies.Result; var result = movies.Result;
return Response.AsJson(result); return Response.AsJson(result);
@ -79,6 +84,7 @@ namespace PlexRequests.UI.Modules
private Response SearchTvShow(string searchTerm) private Response SearchTvShow(string searchTerm)
{ {
Log.Trace("Searching for TV Show {0}", searchTerm);
var tvShow = TvApi.SearchTv(searchTerm, AuthToken); var tvShow = TvApi.SearchTv(searchTerm, AuthToken);
if (tvShow?.data == null) if (tvShow?.data == null)
@ -134,14 +140,22 @@ namespace PlexRequests.UI.Modules
private Response RequestMovie(int movieId) private Response RequestMovie(int movieId)
{ {
Log.Trace("Requesting movie with id {0}", movieId);
var s = new SettingsService(Cache); var s = new SettingsService(Cache);
if (s.CheckRequest(movieId)) if (s.CheckRequest(movieId))
{ {
Log.Trace("movie with id {0} exists", movieId);
return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" }); 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(); var settings = CpService.GetSettings();
Log.Trace("Settings: ");
Log.Trace(settings.DumpJson);
var movieApi = new TheMovieDbApi(); var movieApi = new TheMovieDbApi();
var movieInfo = movieApi.GetMovieInformation(movieId).Result; var movieInfo = movieApi.GetMovieInformation(movieId).Result;
Log.Trace("Getting movie info from TheMovieDb");
Log.Trace(movieInfo.DumpJson);
var model = new RequestedModel var model = new RequestedModel
{ {
@ -158,10 +172,12 @@ namespace PlexRequests.UI.Modules
}; };
var cp = new CouchPotatoApi(); var cp = new CouchPotatoApi();
Log.Trace("Adding movie to CP");
var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.Ip); var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.Ip);
Log.Trace("Adding movie to CP result {0}", result);
if (result) if (result)
{ {
Log.Trace("Adding movie to database requests");
s.AddRequest(movieId, model); s.AddRequest(movieId, model);
return Response.AsJson(new { Result = true }); return Response.AsJson(new { Result = true });

@ -16,17 +16,14 @@
See https://github.com/nlog/nlog/wiki/Configuration-file See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs. for information on customizing logging rules and outputs.
--> -->
<targets> <targets async="true">
<target name="console" xsi:type="Console" layout="${message}"/>
<target name="filelog" type="File" fileName="${shortdate}.log"
layout="${date} ${logger} ${level}: ${message}" />
</targets> </targets>
<rules> <rules>
<logger name="*" minlevel="Trace" writeTo="filelog" />
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="console" />
</rules> </rules>
</nlog> </nlog>

@ -45,8 +45,10 @@ namespace PlexRequests.UI
{ {
class Program class Program
{ {
private static Logger Log = LogManager.GetCurrentClassLogger();
static void Main(string[] args) static void Main(string[] args)
{ {
Log.Trace("Getting assembly version");
WriteOutVersion(); WriteOutVersion();
var s = new Setup(); var s = new Setup();
@ -68,15 +70,17 @@ namespace PlexRequests.UI
private static void WriteOutVersion() private static void WriteOutVersion()
{ {
var assemblyVer = AssemblyHelper.GetAssemblyVersion(); var assemblyVer = AssemblyHelper.GetAssemblyVersion();
Log.Info($"Version: {assemblyVer}");
Console.WriteLine($"Version: {assemblyVer}"); Console.WriteLine($"Version: {assemblyVer}");
} }
private static string GetStartupUri() private static string GetStartupUri()
{ {
Log.Trace("Getting startup URI");
var uri = "http://localhost:3579/"; var uri = "http://localhost:3579/";
var service = new SettingsServiceV2<PlexRequestSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); var service = new SettingsServiceV2<PlexRequestSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var settings = service.GetSettings(); var settings = service.GetSettings();
Log.Trace("Port: {0}", settings.Port);
if (settings.Port != 0) if (settings.Port != 0)
{ {
uri = $"http://localhost:{settings.Port}"; uri = $"http://localhost:{settings.Port}";

Loading…
Cancel
Save