From 87f3c6a6c9419d4d9d8f4c5bcf3bf8cc2d420289 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 19 Jan 2013 16:19:27 -0800 Subject: [PATCH] added Logger injection module for Autofac, API boots up. --- NzbDrone.Api/Bootstrapper.cs | 5 ++- NzbDrone.Api/ErrorManagment/ErrorPipeline.cs | 9 +++-- NzbDrone.Api/NzbDrone.Api.csproj | 1 + .../QualityProfiles/NzbDroneApiModule.cs | 16 +++++++++ .../QualityProfiles/QualityProfileModule.cs | 12 +++---- NzbDrone.Api/QualityType/QualityTypeModule.cs | 9 ++--- NzbDrone.Core/ContainerExtentions.cs | 10 +++--- .../Instrumentation/LogInjectionModule.cs | 36 +++++++++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.ncrunchsolution | 2 +- 10 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 NzbDrone.Api/QualityProfiles/NzbDroneApiModule.cs create mode 100644 NzbDrone.Core/Instrumentation/LogInjectionModule.cs diff --git a/NzbDrone.Api/Bootstrapper.cs b/NzbDrone.Api/Bootstrapper.cs index 818341466..c4fda1b98 100644 --- a/NzbDrone.Api/Bootstrapper.cs +++ b/NzbDrone.Api/Bootstrapper.cs @@ -18,7 +18,10 @@ namespace NzbDrone.Api { private readonly Logger _logger; - + public Bootstrapper() + { + _logger = LogManager.GetCurrentClassLogger(); + } public static void Initialize() { diff --git a/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs index 528ce5792..7e8d234df 100644 --- a/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs +++ b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs @@ -16,11 +16,14 @@ namespace NzbDrone.Api.ErrorManagment public Response HandleException(NancyContext context, Exception exception) { - if (exception is ApiException) + var apiException = exception as ApiException; + + if (apiException != null) { - _logger.WarnException("API Error", exception); - return ((ApiException)exception).ToErrorResponse(); + _logger.WarnException("API Error", apiException); + return apiException.ToErrorResponse(); } + _logger.ErrorException("Unexpected error", exception); return null; } diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index b4ea6f09a..cb3bbe33a 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -92,6 +92,7 @@ + diff --git a/NzbDrone.Api/QualityProfiles/NzbDroneApiModule.cs b/NzbDrone.Api/QualityProfiles/NzbDroneApiModule.cs new file mode 100644 index 000000000..4383ca355 --- /dev/null +++ b/NzbDrone.Api/QualityProfiles/NzbDroneApiModule.cs @@ -0,0 +1,16 @@ +using System.Linq; +using Nancy; + +namespace NzbDrone.Api.QualityProfiles +{ + public abstract class NzbDroneApiModule : NancyModule + { + protected NzbDroneApiModule(string resource) + : base("/api/" + resource.Trim('/')) + { + } + + + + } +} \ No newline at end of file diff --git a/NzbDrone.Api/QualityProfiles/QualityProfileModule.cs b/NzbDrone.Api/QualityProfiles/QualityProfileModule.cs index 7e6065ee1..f0aba2e84 100644 --- a/NzbDrone.Api/QualityProfiles/QualityProfileModule.cs +++ b/NzbDrone.Api/QualityProfiles/QualityProfileModule.cs @@ -8,18 +8,14 @@ using NzbDrone.Api.QualityType; namespace NzbDrone.Api.QualityProfiles { - public class QualityProfileModule : NancyModule + public class QualityProfileModule : NzbDroneApiModule { private readonly QualityProvider _qualityProvider; public QualityProfileModule(QualityProvider qualityProvider) - { - _qualityProvider = qualityProvider; - } - - public QualityProfileModule() : base("/QualityProfile") { + _qualityProvider = qualityProvider; Get["/"] = x => OnGet(); Get["/{Id}"] = x => OnGet((int)x.Id); Put["/"] = x => OnPut(); @@ -44,7 +40,7 @@ namespace NzbDrone.Api.QualityProfiles var profile = Mapper.Map(request); request.Id = _qualityProvider.Add(profile); - return request.AsResponse(); + return request.AsResponse(); } //Update @@ -54,7 +50,7 @@ namespace NzbDrone.Api.QualityProfiles var profile = Mapper.Map(request); _qualityProvider.Update(profile); - return request.AsResponse(); + return request.AsResponse(); } private Response OnDelete(int id) diff --git a/NzbDrone.Api/QualityType/QualityTypeModule.cs b/NzbDrone.Api/QualityType/QualityTypeModule.cs index c49e31f2a..b038aa018 100644 --- a/NzbDrone.Api/QualityType/QualityTypeModule.cs +++ b/NzbDrone.Api/QualityType/QualityTypeModule.cs @@ -2,22 +2,19 @@ using System.Linq; using AutoMapper; using Nancy; +using NzbDrone.Api.QualityProfiles; using NzbDrone.Core.Providers; namespace NzbDrone.Api.QualityType { - public class QualityTypeModule : NancyModule + public class QualityTypeModule : NzbDroneApiModule { private readonly QualityTypeProvider _qualityTypeProvider; public QualityTypeModule(QualityTypeProvider qualityTypeProvider) - { - _qualityTypeProvider = qualityTypeProvider; - } - - public QualityTypeModule() : base("/QualityTypes") { + _qualityTypeProvider = qualityTypeProvider; Get["/"] = x => GetQualityType(); Get["/{id}"] = x => GetQualityType(x.Id); diff --git a/NzbDrone.Core/ContainerExtentions.cs b/NzbDrone.Core/ContainerExtentions.cs index cc24f1e12..b305f3e28 100644 --- a/NzbDrone.Core/ContainerExtentions.cs +++ b/NzbDrone.Core/ContainerExtentions.cs @@ -20,15 +20,17 @@ namespace NzbDrone.Core private static readonly Logger logger = LogManager.GetLogger("ServiceRegistration"); - public static void RegisterCoreServices(this ContainerBuilder container) + public static void RegisterCoreServices(this ContainerBuilder containerBuilder) { var core = Assembly.Load("NzbDrone.Core"); var common = Assembly.Load("NzbDrone.Common"); - container.RegisterAssembly(core); - container.RegisterAssembly(common); + containerBuilder.RegisterAssembly(core); + containerBuilder.RegisterAssembly(common); - container.InitDatabase(); + containerBuilder.InitDatabase(); + + containerBuilder.RegisterModule(); } diff --git a/NzbDrone.Core/Instrumentation/LogInjectionModule.cs b/NzbDrone.Core/Instrumentation/LogInjectionModule.cs new file mode 100644 index 000000000..24d9cf51d --- /dev/null +++ b/NzbDrone.Core/Instrumentation/LogInjectionModule.cs @@ -0,0 +1,36 @@ +using System; +using System.Linq; +using Autofac; +using Autofac.Core; +using NLog; + +namespace NzbDrone.Core.Instrumentation +{ + public class LogInjectionModule : Module + { + protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) + { + registration.Preparing += OnComponentPreparing; + } + static void OnComponentPreparing(object sender, PreparingEventArgs e) + { + e.Parameters = e.Parameters.Union(new[] + { + new ResolvedParameter((p, i) => p.ParameterType == typeof(Logger), (p,i)=> GetLogger(p.Member.DeclaringType)) + }); + } + + private static object GetLogger(Type type) + { + const string STRING_TO_REMOVE = "SyntikX"; + + var loggerName = type.FullName; + if (loggerName.StartsWith(STRING_TO_REMOVE)) + { + loggerName = loggerName.Substring(STRING_TO_REMOVE.Length + 1); + } + + return LogManager.GetLogger(loggerName); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index ba8ae9328..5d882ae01 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -265,6 +265,7 @@ + diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution index 13efc6321..e88d63cdb 100644 --- a/NzbDrone.ncrunchsolution +++ b/NzbDrone.ncrunchsolution @@ -1,6 +1,6 @@ 1 - True + False true true UseDynamicAnalysis