1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01:00

Merge pull request #124 from NzbDrone/raw-dblog

Use raw sql to write logs to db.
This commit is contained in:
Keivan Beigi 2014-10-16 15:11:47 -07:00
commit 6c3a5b336d
6 changed files with 78 additions and 40 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using FluentAssertions;
using Marr.Data;
using NLog;
using NUnit.Framework;
using NzbDrone.Common.Instrumentation;
@ -65,6 +66,19 @@ public void write_long_log()
VerifyLog(StoredModel, LogLevel.Info);
}
[Test]
[Explicit]
public void perf_test()
{
MapRepository.Instance.EnableTraceLogging = false;
for (int i = 0; i < 1000; i++)
{
_logger.Info(Guid.NewGuid());
}
MapRepository.Instance.EnableTraceLogging = true;
}
[Test]
public void write_log_exception()
{
@ -119,7 +133,6 @@ private void VerifyLog(Log logItem, LogLevel level)
logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2));
logItem.Logger.Should().Be(this.GetType().Name);
logItem.Level.Should().Be(level.Name);
logItem.Method.Should().Be(new StackTrace().GetFrame(1).GetMethod().Name);
_logger.Name.Should().EndWith(logItem.Logger);
}
}

View File

@ -30,6 +30,7 @@ private static string GetConnectionString(string dbPath)
connectionBuilder.CacheSize = (int)-10.Megabytes();
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal;
connectionBuilder.Pooling = true;
return connectionBuilder.ConnectionString;
}

View File

@ -0,0 +1,14 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(64)]
public class remove_method_from_logs : NzbDroneMigrationBase
{
protected override void LogDbUpgrade()
{
Delete.Column("Method").FromTable("Logs");
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Data;
using System.Data.SQLite;
using NLog.Common;
using NLog.Config;
@ -6,6 +7,7 @@
using NLog.Layouts;
using NLog.Targets;
using NzbDrone.Common.Instrumentation;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
@ -14,17 +16,18 @@ namespace NzbDrone.Core.Instrumentation
public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownRequested>
{
private readonly ILogRepository _repository;
private readonly SQLiteConnection _connection;
public DatabaseTarget(ILogRepository repository)
const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
{
_repository = repository;
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString).OpenAndReturn();
}
public void Register()
{
Layout = new SimpleLayout("${callsite:className=false:fileName=false:includeSourcePath=false:methodName=true}");
Rule = new LoggingRule("*", LogLevel.Info, this);
LogManager.Configuration.AddTarget("DbLogger", this);
@ -51,40 +54,48 @@ private void OnLogManagerOnConfigurationReloaded(object sender, LoggingConfigura
protected override void Write(LogEventInfo logEvent)
{
var log = new Log();
log.Time = logEvent.TimeStamp;
log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage);
log.Method = Layout.Render(logEvent);
log.Logger = logEvent.LoggerName;
if (log.Logger.StartsWith("NzbDrone."))
{
log.Logger = log.Logger.Remove(0, 9);
}
if (logEvent.Exception != null)
{
if (String.IsNullOrWhiteSpace(log.Message))
{
log.Message = logEvent.Exception.Message;
}
else
{
log.Message += ": " + logEvent.Exception.Message;
}
log.Exception = logEvent.Exception.ToString();
log.ExceptionType = logEvent.Exception.GetType().ToString();
}
log.Level = logEvent.Level.Name;
try
{
_repository.Insert(log);
var log = new Log();
log.Time = logEvent.TimeStamp;
log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage);
log.Logger = logEvent.LoggerName;
if (log.Logger.StartsWith("NzbDrone."))
{
log.Logger = log.Logger.Remove(0, 9);
}
if (logEvent.Exception != null)
{
if (String.IsNullOrWhiteSpace(log.Message))
{
log.Message = logEvent.Exception.Message;
}
else
{
log.Message += ": " + logEvent.Exception.Message;
}
log.Exception = logEvent.Exception.ToString();
log.ExceptionType = logEvent.Exception.GetType().ToString();
}
log.Level = logEvent.Level.Name;
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection);
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time });
sqlCommand.Parameters.Add(new SQLiteParameter("Logger", DbType.String) { Value = log.Logger });
sqlCommand.Parameters.Add(new SQLiteParameter("Exception", DbType.String) { Value = log.Exception });
sqlCommand.Parameters.Add(new SQLiteParameter("ExceptionType", DbType.String) { Value = log.ExceptionType });
sqlCommand.Parameters.Add(new SQLiteParameter("Level", DbType.String) { Value = log.Level });
sqlCommand.ExecuteNonQuery();
}
catch (SQLiteException ex)
{

View File

@ -13,8 +13,6 @@ public class Log : ModelBase
public string Logger { get; set; }
public string Method { get; set; }
public string Exception { get; set; }
public string ExceptionType { get; set; }

View File

@ -225,6 +225,7 @@
<Compile Include="Datastore\Migration\058_drop_epsiode_file_path.cs" />
<Compile Include="Datastore\Migration\059_add_enable_options_to_indexers.cs" />
<Compile Include="Datastore\Migration\063_add_remotepathmappings.cs" />
<Compile Include="Datastore\Migration\064_add_remove_method_from_logs.cs" />
<Compile Include="Datastore\Migration\061_clear_bad_scene_names.cs" />
<Compile Include="Datastore\Migration\060_remove_enable_from_indexers.cs" />
<Compile Include="Datastore\Migration\062_convert_quality_models.cs" />