1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

added Logger injection module for Autofac, API boots up.

This commit is contained in:
kay.one 2013-01-19 16:19:27 -08:00
parent 64a3e1caf0
commit 87f3c6a6c9
10 changed files with 78 additions and 23 deletions

View File

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

View File

@ -16,11 +16,14 @@ public ErrorPipeline(Logger logger)
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;
}

View File

@ -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" />

View File

@ -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('/'))
{
}
}
}

View File

@ -8,18 +8,14 @@
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 @@ private Response OnPost()
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(request);
request.Id = _qualityProvider.Add(profile);
return request.AsResponse();
return request.AsResponse();
}
//Update
@ -54,7 +50,7 @@ private Response OnPut()
var profile = Mapper.Map<QualityProfileModel, QualityProfile>(request);
_qualityProvider.Update(profile);
return request.AsResponse();
return request.AsResponse();
}
private Response OnDelete(int id)

View File

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

View File

@ -20,15 +20,17 @@ public static class ContainerExtentions
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>();
}

View File

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

View File

@ -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" />

View File

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