added Logger injection module for Autofac, API boots up.

pull/23/head
kay.one 12 years ago
parent 64a3e1caf0
commit 87f3c6a6c9

@ -18,7 +18,10 @@ namespace NzbDrone.Api
{
private readonly Logger _logger;
public Bootstrapper()
{
_logger = LogManager.GetCurrentClassLogger();
}
public static void Initialize()
{

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

@ -92,6 +92,7 @@
<Compile Include="ErrorManagment\ErrorPipeline.cs" />
<Compile Include="Exceptions\InvalidApiKeyException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QualityProfiles\NzbDroneApiModule.cs" />
<Compile Include="QualityProfiles\QualityProfileModel.cs" />
<Compile Include="QualityProfiles\QualityProfileModule.cs" />
<Compile Include="QualityType\QualityTypeModel.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('/'))
{
}
}
}

@ -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<QualityProfileModel, QualityProfile>(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<QualityProfileModel, QualityProfile>(request);
_qualityProvider.Update(profile);
return request.AsResponse();
return request.AsResponse();
}
private Response OnDelete(int id)

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

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

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

@ -265,6 +265,7 @@
<Compile Include="Helpers\SortHelper.cs" />
<Compile Include="Helpers\SabnzbdPriorityTypeConverter.cs" />
<Compile Include="Helpers\XElementHelper.cs" />
<Compile Include="Instrumentation\LogInjectionModule.cs" />
<Compile Include="Jobs\CleanupRecycleBinJob.cs" />
<Compile Include="Jobs\AppShutdownJob.cs" />
<Compile Include="Jobs\AppRestartJob.cs" />

@ -1,6 +1,6 @@
<SolutionConfiguration>
<FileVersion>1</FileVersion>
<AutoEnableOnStartup>True</AutoEnableOnStartup>
<AutoEnableOnStartup>False</AutoEnableOnStartup>
<AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>

Loading…
Cancel
Save