Use raw sql to write logs to db.

pull/4/head
Keivan Beigi 10 years ago
parent 28f7e256b2
commit cb00644a09

@ -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 @@ namespace NzbDrone.Core.Test.InstrumentationTests
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()
{

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

@ -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;
using NLog.Layouts;
using NLog.Targets;
using NzbDrone.Common.Instrumentation;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
@ -14,11 +16,14 @@ 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],[Method],[Exception],[ExceptionType],[Level]) " +
"VALUES(@Message,@Time,@Logger,@Method,@Exception,@ExceptionType,@Level)";
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
{
_repository = repository;
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString).OpenAndReturn();
}
public void Register()
@ -50,6 +55,8 @@ namespace NzbDrone.Core.Instrumentation
public LoggingRule Rule { get; set; }
protected override void Write(LogEventInfo logEvent)
{
try
{
var log = new Log();
log.Time = logEvent.TimeStamp;
@ -82,9 +89,17 @@ namespace NzbDrone.Core.Instrumentation
log.Level = logEvent.Level.Name;
try
{
_repository.Insert(log);
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("Method", DbType.String) { Value = log.Method });
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)
{

Loading…
Cancel
Save