Merge pull request #124 from NzbDrone/raw-dblog

Use raw sql to write logs to db.
pull/4/head
Keivan Beigi 10 years ago
commit 6c3a5b336d

@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using FluentAssertions; using FluentAssertions;
using Marr.Data;
using NLog; using NLog;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
@ -65,6 +66,19 @@ namespace NzbDrone.Core.Test.InstrumentationTests
VerifyLog(StoredModel, LogLevel.Info); 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] [Test]
public void write_log_exception() public void write_log_exception()
{ {
@ -119,7 +133,6 @@ namespace NzbDrone.Core.Test.InstrumentationTests
logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2)); logItem.Time.Should().BeWithin(TimeSpan.FromSeconds(2));
logItem.Logger.Should().Be(this.GetType().Name); logItem.Logger.Should().Be(this.GetType().Name);
logItem.Level.Should().Be(level.Name); logItem.Level.Should().Be(level.Name);
logItem.Method.Should().Be(new StackTrace().GetFrame(1).GetMethod().Name);
_logger.Name.Should().EndWith(logItem.Logger); _logger.Name.Should().EndWith(logItem.Logger);
} }
} }

@ -30,6 +30,7 @@ namespace NzbDrone.Core.Datastore
connectionBuilder.CacheSize = (int)-10.Megabytes(); connectionBuilder.CacheSize = (int)-10.Megabytes();
connectionBuilder.DateTimeKind = DateTimeKind.Utc; connectionBuilder.DateTimeKind = DateTimeKind.Utc;
connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal; connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal;
connectionBuilder.Pooling = true;
return connectionBuilder.ConnectionString; return connectionBuilder.ConnectionString;
} }

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

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

@ -13,8 +13,6 @@ namespace NzbDrone.Core.Instrumentation
public string Logger { get; set; } public string Logger { get; set; }
public string Method { get; set; }
public string Exception { get; set; } public string Exception { get; set; }
public string ExceptionType { get; set; } public string ExceptionType { get; set; }

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

Loading…
Cancel
Save