|
|
|
@ -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,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 @@ namespace NzbDrone.Core.Instrumentation
|
|
|
|
|
|
|
|
|
|
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."))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrWhiteSpace(log.Message))
|
|
|
|
|
log.Logger = logEvent.LoggerName;
|
|
|
|
|
|
|
|
|
|
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.ExceptionType = logEvent.Exception.GetType().ToString();
|
|
|
|
|
}
|
|
|
|
|
log.Level = logEvent.Level.Name;
|
|
|
|
|
|
|
|
|
|
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection);
|
|
|
|
|
|
|
|
|
|
log.Level = logEvent.Level.Name;
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_repository.Insert(log);
|
|
|
|
|
sqlCommand.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (SQLiteException ex)
|
|
|
|
|
{
|
|
|
|
|