mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-10-30 15:32:31 +01:00
LogProvider now usues petapoco
This commit is contained in:
parent
335639fabc
commit
f4801901a7
154
NzbDrone.Core.Test/LogProviderTest.cs
Normal file
154
NzbDrone.Core.Test/LogProviderTest.cs
Normal file
@ -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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="LogProviderTest.cs" />
|
||||
<Compile Include="UpcomingEpisodesProviderTest.cs" />
|
||||
<Compile Include="MediaFileProvider_ImportNewDownloadTest.cs" />
|
||||
<Compile Include="MediaFileProvider_GetNewFilenameTest.cs" />
|
||||
|
@ -105,104 +105,5 @@ namespace NzbDrone.Core.Test
|
||||
Console.WriteLine(new Series().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.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.LogConnectionString)).WhenInjectedInto<SubsonicTarget>().InSingletonScope();
|
||||
_kernel.Bind<IRepository>().ToConstant(Connection.CreateSimpleRepository(Connection.LogConnectionString)).WhenInjectedInto<LogProvider>().InSingletonScope();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,12 +41,12 @@ namespace NzbDrone.Core.Datastore.Migrations
|
||||
{
|
||||
new Column("SeriesId", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Title", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("CleanTitle", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("CleanTitle", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("Status", DbType.String, ColumnProperty.Null),
|
||||
new Column("Overview", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("Overview", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("AirsDayOfWeek", DbType.Int16, ColumnProperty.Null),
|
||||
new Column("AirTimes", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("Language", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("AirTimes", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("Language", DbType.String, ColumnProperty.NotNull, String.Empty),
|
||||
new Column("Path", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Monitored", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("QualityProfileId", DbType.Int16, ColumnProperty.NotNull),
|
||||
@ -92,51 +92,67 @@ namespace NzbDrone.Core.Datastore.Migrations
|
||||
});
|
||||
|
||||
Database.AddTable("History", "SQLite", new[]
|
||||
{
|
||||
new Column("HistoryId", DbType.Int64, ColumnProperty.PrimaryKey),
|
||||
new Column("EpisodeId", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("SeriesId", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("NzbTitle", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Date", DbType.DateTime, ColumnProperty.NotNull),
|
||||
new Column("Quality", DbType.Int16, ColumnProperty.NotNull),
|
||||
new Column("IsProper", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("Indexer", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
{
|
||||
new Column("HistoryId", DbType.Int64, ColumnProperty.PrimaryKey),
|
||||
new Column("EpisodeId", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("SeriesId", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("NzbTitle", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Date", DbType.DateTime, ColumnProperty.NotNull),
|
||||
new Column("Quality", DbType.Int16, ColumnProperty.NotNull),
|
||||
new Column("IsProper", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("Indexer", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
|
||||
Database.AddTable("RootDirs", "SQLite", new[]
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Path", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Path", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
|
||||
Database.AddTable("ExternalNotificationSettings", "SQLite", new[]
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Enabled", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("NotifierName", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Enabled", DbType.Boolean, ColumnProperty.NotNull)
|
||||
,
|
||||
new Column("NotifierName", DbType.String,
|
||||
ColumnProperty.NotNull),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull)
|
||||
});
|
||||
|
||||
Database.AddTable("JobSettings", "SQLite", new[]
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Enable", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("TypeName", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Interval", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("LastExecution", DbType.DateTime, ColumnProperty.NotNull),
|
||||
new Column("Success", DbType.Boolean, ColumnProperty.NotNull)
|
||||
});
|
||||
{
|
||||
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Enable", DbType.Boolean, ColumnProperty.NotNull),
|
||||
new Column("TypeName", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Interval", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("LastExecution", DbType.DateTime, ColumnProperty.NotNull),
|
||||
new Column("Success", DbType.Boolean, ColumnProperty.NotNull)
|
||||
});
|
||||
|
||||
Database.AddTable("QualityProfiles", "SQLite", new[]
|
||||
{
|
||||
new Column("QualityProfileId", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Cutoff", DbType.Int32, ColumnProperty.NotNull),
|
||||
new Column("SonicAllowed", DbType.String, ColumnProperty.NotNull),
|
||||
});
|
||||
{
|
||||
new Column("QualityProfileId", DbType.Int32, ColumnProperty.PrimaryKey),
|
||||
new Column("Name", DbType.String, ColumnProperty.NotNull),
|
||||
new Column("Cutoff", DbType.Int32, 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()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
@ -1,14 +1,16 @@
|
||||
using System;
|
||||
using PetaPoco;
|
||||
using SubSonic.SqlGeneration.Schema;
|
||||
|
||||
namespace NzbDrone.Core.Instrumentation
|
||||
{
|
||||
[TableName("Logs")]
|
||||
[PrimaryKey("LogId", autoIncrement = true)]
|
||||
public class Log
|
||||
{
|
||||
[SubSonicPrimaryKey]
|
||||
public int LogId { get; protected set; }
|
||||
|
||||
[SubSonicLongString]
|
||||
public Int64 LogId { get; protected set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public DateTime Time { get; set; }
|
||||
@ -17,11 +19,8 @@ namespace NzbDrone.Core.Instrumentation
|
||||
|
||||
public string Method { get; set; }
|
||||
|
||||
[SubSonicNullString]
|
||||
[SubSonicLongString]
|
||||
public string Exception { get; set; }
|
||||
|
||||
[SubSonicNullString]
|
||||
public string ExceptionType { get; set; }
|
||||
|
||||
public String Level { get; set; }
|
||||
|
@ -1,27 +1,31 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using PetaPoco;
|
||||
using SubSonic.Repository;
|
||||
|
||||
namespace NzbDrone.Core.Instrumentation
|
||||
{
|
||||
public class LogProvider
|
||||
{
|
||||
private readonly IDatabase _database;
|
||||
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()
|
||||
{
|
||||
_repository.DeleteMany(GetAllLogs());
|
||||
_database.Delete<Log>("");
|
||||
Logger.Info("Cleared Log History");
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
using System;
|
||||
using NLog;
|
||||
using NLog.Targets;
|
||||
using PetaPoco;
|
||||
using SubSonic.Repository;
|
||||
|
||||
namespace NzbDrone.Core.Instrumentation
|
||||
{
|
||||
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)
|
||||
@ -47,7 +48,7 @@ namespace NzbDrone.Core.Instrumentation
|
||||
log.Level = logEvent.Level.Name;
|
||||
|
||||
|
||||
_repository.Add(log);
|
||||
_database.Insert(log);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user