LogProvider now usues petapoco

pull/2/head
kay.one 14 years ago
parent 335639fabc
commit f4801901a7

@ -0,0 +1,154 @@
// ReSharper disable RedundantUsingDirective
using System;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NLog;
using NLog.Config;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class LogProviderTest : TestBase
{
[Test]
public void write_log()
{
//setup
var message = Guid.NewGuid().ToString();
var db = MockLib.GetEmptyDatabase(true);
var sonicTarget = new SubsonicTarget(db);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
//Act
Logger.Info(message);
//Assert
db.Fetch<Log>().Should().HaveCount(1);
var logItem = db.Fetch<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Info.Name, logItem.Level);
Assert.AreEqual("write_log", logItem.Method);
}
[Test]
public void clearLog()
{
//setup
var db = MockLib.GetEmptyDatabase(true);
var sonicTarget = new SubsonicTarget(db);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
//Act
for (int i = 0; i < 10; i++)
{
Logger.Info("Test");
}
//Assert
var provider = new LogProvider(db);
provider.GetAllLogs().Should().HaveCount(10);
provider.DeleteAll();
provider.GetAllLogs().Should().HaveCount(1);
}
[Test]
public void write_log_exception()
{
//setup
var message = Guid.NewGuid().ToString();
var db = MockLib.GetEmptyDatabase(true);
var sonicTarget = new SubsonicTarget(db);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
var ex = new InvalidOperationException("Fake Exception");
//Act
Logger.ErrorException(message, ex);
//Assert
db.Fetch<Log>().Should().HaveCount(1);
var logItem = db.Fetch<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(message + ": " + ex.Message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Error.Name, logItem.Level);
Assert.AreEqual(ex.GetType().ToString(), logItem.ExceptionType);
Assert.AreEqual(ex.ToString(), logItem.Exception);
ExceptionVerification.ExcpectedErrors(1);
}
[Test]
public void write_log_exception_no_message_should_use_exception_message()
{
//setup
var message = String.Empty;
var db = MockLib.GetEmptyDatabase(true);
var sonicTarget = new SubsonicTarget(db);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
var ex = new InvalidOperationException("Fake Exception");
//Act
Logger.ErrorException(message, ex);
//Assert
db.Fetch<Log>().Should().HaveCount(1);
var logItem = db.Fetch<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(ex.Message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Error.Name, logItem.Level);
Assert.AreEqual(ex.GetType().ToString(), logItem.ExceptionType);
Assert.AreEqual(ex.ToString(), logItem.Exception);
ExceptionVerification.ExcpectedErrors(1);
}
}
}

@ -91,6 +91,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="LogProviderTest.cs" />
<Compile Include="UpcomingEpisodesProviderTest.cs" /> <Compile Include="UpcomingEpisodesProviderTest.cs" />
<Compile Include="MediaFileProvider_ImportNewDownloadTest.cs" /> <Compile Include="MediaFileProvider_ImportNewDownloadTest.cs" />
<Compile Include="MediaFileProvider_GetNewFilenameTest.cs" /> <Compile Include="MediaFileProvider_GetNewFilenameTest.cs" />

@ -105,104 +105,5 @@ namespace NzbDrone.Core.Test
Console.WriteLine(new Series().ToString()); Console.WriteLine(new Series().ToString());
Console.WriteLine(new EpisodeFile().ToString()); Console.WriteLine(new EpisodeFile().ToString());
} }
[Test]
public void write_log()
{
//setup
var message = Guid.NewGuid().ToString();
var sonicRepo = MockLib.GetEmptyRepository(true);
var sonicTarget = new SubsonicTarget(sonicRepo);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
//Act
Logger.Info(message);
//Assert
sonicRepo.All<Log>().Should().HaveCount(1);
var logItem = sonicRepo.All<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Info.Name, logItem.Level);
Assert.AreEqual("write_log", logItem.Method);
}
[Test]
public void write_log_exception()
{
//setup
var message = Guid.NewGuid().ToString();
var sonicRepo = MockLib.GetEmptyRepository(true);
var sonicTarget = new SubsonicTarget(sonicRepo);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
var ex = new InvalidOperationException("Fake Exception");
//Act
Logger.ErrorException(message, ex);
//Assert
sonicRepo.All<Log>().Should().HaveCount(1);
var logItem = sonicRepo.All<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(message + ": " + ex.Message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Error.Name, logItem.Level);
Assert.AreEqual(ex.GetType().ToString(), logItem.ExceptionType);
Assert.AreEqual(ex.ToString(), logItem.Exception);
ExceptionVerification.ExcpectedErrors(1);
}
[Test]
public void write_log_exception_no_message_should_use_exception_message()
{
//setup
var message = String.Empty;
var sonicRepo = MockLib.GetEmptyRepository(true);
var sonicTarget = new SubsonicTarget(sonicRepo);
LogManager.Configuration.AddTarget("DbLogger", sonicTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, sonicTarget));
LogManager.Configuration.Reload();
Logger Logger = LogManager.GetCurrentClassLogger();
var ex = new InvalidOperationException("Fake Exception");
//Act
Logger.ErrorException(message, ex);
//Assert
sonicRepo.All<Log>().Should().HaveCount(1);
var logItem = sonicRepo.All<Log>().First();
Assert.AreNotEqual(new DateTime(), logItem.Time);
Assert.AreEqual(ex.Message, logItem.Message);
Assert.AreEqual(Logger.Name, logItem.Logger);
Assert.AreEqual(LogLevel.Error.Name, logItem.Level);
Assert.AreEqual(ex.GetType().ToString(), logItem.ExceptionType);
Assert.AreEqual(ex.ToString(), logItem.Exception);
ExceptionVerification.ExcpectedErrors(1);
}
} }
} }

@ -69,9 +69,10 @@ namespace NzbDrone.Core
_kernel = new StandardKernel(); _kernel = new StandardKernel();
_kernel.Bind<IDatabase>().ToMethod(c => Connection.GetPetaPocoDb(Connection.MainConnectionString)).InRequestScope(); _kernel.Bind<IDatabase>().ToMethod(c => Connection.GetPetaPocoDb(Connection.MainConnectionString)).InRequestScope();
_kernel.Bind<IDatabase>().ToMethod(c => Connection.GetPetaPocoDb(Connection.LogConnectionString)).WhenInjectedInto<SubsonicTarget>().InSingletonScope();
_kernel.Bind<IDatabase>().ToMethod(c => Connection.GetPetaPocoDb(Connection.LogConnectionString)).WhenInjectedInto<LogProvider>().InRequestScope();
_kernel.Bind<IRepository>().ToConstant(Connection.CreateSimpleRepository(Connection.MainConnectionString)).InSingletonScope(); _kernel.Bind<IRepository>().ToConstant(Connection.CreateSimpleRepository(Connection.MainConnectionString)).InSingletonScope();
_kernel.Bind<IRepository>().ToConstant(Connection.CreateSimpleRepository(Connection.LogConnectionString)).WhenInjectedInto<SubsonicTarget>().InSingletonScope();
_kernel.Bind<IRepository>().ToConstant(Connection.CreateSimpleRepository(Connection.LogConnectionString)).WhenInjectedInto<LogProvider>().InSingletonScope();
} }
} }

@ -112,8 +112,10 @@ namespace NzbDrone.Core.Datastore.Migrations
Database.AddTable("ExternalNotificationSettings", "SQLite", new[] Database.AddTable("ExternalNotificationSettings", "SQLite", new[]
{ {
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey), new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
new Column("Enabled", DbType.Boolean, ColumnProperty.NotNull), new Column("Enabled", DbType.Boolean, ColumnProperty.NotNull)
new Column("NotifierName", DbType.String, ColumnProperty.NotNull), ,
new Column("NotifierName", DbType.String,
ColumnProperty.NotNull),
new Column("Name", DbType.String, ColumnProperty.NotNull) new Column("Name", DbType.String, ColumnProperty.NotNull)
}); });
@ -135,8 +137,22 @@ namespace NzbDrone.Core.Datastore.Migrations
new Column("Cutoff", DbType.Int32, ColumnProperty.NotNull), new Column("Cutoff", DbType.Int32, ColumnProperty.NotNull),
new Column("SonicAllowed", DbType.String, ColumnProperty.NotNull), new Column("SonicAllowed", DbType.String, ColumnProperty.NotNull),
}); });
Database.AddTable("Logs", "SQLite", new[]
{
new Column("LogId", DbType.Int64, ColumnProperty.PrimaryKey),
new Column("Message", DbType.String, ColumnProperty.NotNull),
new Column("Time", DbType.DateTime, ColumnProperty.NotNull),
new Column("Logger", DbType.String, ColumnProperty.NotNull),
new Column("Method", DbType.String, ColumnProperty.NotNull),
new Column("Exception", DbType.String, ColumnProperty.Null),
new Column("ExceptionType", DbType.String, ColumnProperty.Null),
new Column("Level", DbType.String, ColumnProperty.NotNull)
});
} }
public override void Down() public override void Down()
{ {
throw new NotImplementedException(); throw new NotImplementedException();

@ -1,14 +1,16 @@
using System; using System;
using PetaPoco;
using SubSonic.SqlGeneration.Schema; using SubSonic.SqlGeneration.Schema;
namespace NzbDrone.Core.Instrumentation namespace NzbDrone.Core.Instrumentation
{ {
[TableName("Logs")]
[PrimaryKey("LogId", autoIncrement = true)]
public class Log public class Log
{ {
[SubSonicPrimaryKey]
public int LogId { get; protected set; }
[SubSonicLongString] public Int64 LogId { get; protected set; }
public string Message { get; set; } public string Message { get; set; }
public DateTime Time { get; set; } public DateTime Time { get; set; }
@ -17,11 +19,8 @@ namespace NzbDrone.Core.Instrumentation
public string Method { get; set; } public string Method { get; set; }
[SubSonicNullString]
[SubSonicLongString]
public string Exception { get; set; } public string Exception { get; set; }
[SubSonicNullString]
public string ExceptionType { get; set; } public string ExceptionType { get; set; }
public String Level { get; set; } public String Level { get; set; }

@ -1,27 +1,31 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using NLog; using NLog;
using PetaPoco;
using SubSonic.Repository; using SubSonic.Repository;
namespace NzbDrone.Core.Instrumentation namespace NzbDrone.Core.Instrumentation
{ {
public class LogProvider public class LogProvider
{ {
private readonly IDatabase _database;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IRepository _repository;
public LogProvider(IRepository repository)
public LogProvider(IDatabase database)
{ {
_repository = repository; _database = database;
} }
public IQueryable<Log> GetAllLogs() public IList<Log> GetAllLogs()
{ {
return _repository.All<Log>(); return _database.Fetch<Log>();
} }
public void DeleteAll() public void DeleteAll()
{ {
_repository.DeleteMany(GetAllLogs()); _database.Delete<Log>("");
Logger.Info("Cleared Log History"); Logger.Info("Cleared Log History");
} }
} }

@ -1,17 +1,18 @@
using System; using System;
using NLog; using NLog;
using NLog.Targets; using NLog.Targets;
using PetaPoco;
using SubSonic.Repository; using SubSonic.Repository;
namespace NzbDrone.Core.Instrumentation namespace NzbDrone.Core.Instrumentation
{ {
public class SubsonicTarget : Target public class SubsonicTarget : Target
{ {
private readonly IRepository _repository; private readonly IDatabase _database;
public SubsonicTarget(IRepository repository) public SubsonicTarget(IDatabase database)
{ {
_repository = repository; _database = database;
} }
protected override void Write(LogEventInfo logEvent) protected override void Write(LogEventInfo logEvent)
@ -47,7 +48,7 @@ namespace NzbDrone.Core.Instrumentation
log.Level = logEvent.Level.Name; log.Level = logEvent.Level.Name;
_repository.Add(log); _database.Insert(log);
} }
} }
} }
Loading…
Cancel
Save