|
|
@ -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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|