2016-12-23 21:45:24 +00:00
|
|
|
|
using System.Data;
|
2014-03-05 07:11:38 +00:00
|
|
|
|
using System.Data.SQLite;
|
2014-03-05 01:02:36 +00:00
|
|
|
|
using NLog.Common;
|
2011-11-14 00:22:18 +00:00
|
|
|
|
using NLog.Config;
|
2010-10-24 07:46:58 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Targets;
|
2014-05-25 06:17:59 +00:00
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
2014-10-16 02:10:25 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-07-30 00:57:38 +00:00
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2010-10-24 07:46:58 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Instrumentation
|
|
|
|
|
{
|
2013-07-30 00:57:38 +00:00
|
|
|
|
public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownRequested>
|
2010-10-24 07:46:58 +00:00
|
|
|
|
{
|
2014-10-16 02:10:25 +00:00
|
|
|
|
private readonly SQLiteConnection _connection;
|
2010-10-24 07:46:58 +00:00
|
|
|
|
|
2014-10-16 02:23:10 +00:00
|
|
|
|
const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
|
|
|
|
|
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
|
2014-10-16 02:10:25 +00:00
|
|
|
|
|
|
|
|
|
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
|
2010-10-24 07:46:58 +00:00
|
|
|
|
{
|
2014-11-24 20:00:19 +00:00
|
|
|
|
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString);
|
|
|
|
|
_connection.Open();
|
2010-10-24 07:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
public void Register()
|
|
|
|
|
{
|
2016-06-10 22:25:59 +00:00
|
|
|
|
var target = new SlowRunningAsyncTargetWrapper(this) { TimeToSleepBetweenBatches = 500 };
|
2013-05-21 03:20:29 +00:00
|
|
|
|
|
2016-06-10 22:25:59 +00:00
|
|
|
|
Rule = new LoggingRule("*", LogLevel.Info, target);
|
|
|
|
|
|
|
|
|
|
LogManager.Configuration.AddTarget("DbLogger", target);
|
2013-05-21 03:20:29 +00:00
|
|
|
|
LogManager.Configuration.LoggingRules.Add(Rule);
|
2013-07-30 00:57:38 +00:00
|
|
|
|
LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded;
|
2013-05-21 03:20:29 +00:00
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
2011-11-14 00:22:18 +00:00
|
|
|
|
}
|
2013-04-17 03:20:11 +00:00
|
|
|
|
|
2013-07-30 00:57:38 +00:00
|
|
|
|
public void UnRegister()
|
|
|
|
|
{
|
|
|
|
|
LogManager.ConfigurationReloaded -= OnLogManagerOnConfigurationReloaded;
|
|
|
|
|
LogManager.Configuration.RemoveTarget("DbLogger");
|
|
|
|
|
LogManager.Configuration.LoggingRules.Remove(Rule);
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnLogManagerOnConfigurationReloaded(object sender, LoggingConfigurationReloadedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
Register();
|
|
|
|
|
}
|
2011-10-24 05:54:09 +00:00
|
|
|
|
|
2013-05-21 03:20:29 +00:00
|
|
|
|
public LoggingRule Rule { get; set; }
|
|
|
|
|
|
2010-10-24 07:46:58 +00:00
|
|
|
|
protected override void Write(LogEventInfo logEvent)
|
|
|
|
|
{
|
2014-10-16 02:10:25 +00:00
|
|
|
|
try
|
2011-07-04 05:23:38 +00:00
|
|
|
|
{
|
2014-10-16 02:10:25 +00:00
|
|
|
|
var log = new Log();
|
|
|
|
|
log.Time = logEvent.TimeStamp;
|
|
|
|
|
log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage);
|
2011-07-04 05:23:38 +00:00
|
|
|
|
|
2014-10-16 02:10:25 +00:00
|
|
|
|
log.Logger = logEvent.LoggerName;
|
|
|
|
|
|
|
|
|
|
if (log.Logger.StartsWith("NzbDrone."))
|
2011-04-25 17:48:16 +00:00
|
|
|
|
{
|
2014-10-16 02:10:25 +00:00
|
|
|
|
log.Logger = log.Logger.Remove(0, 9);
|
2011-04-25 17:48:16 +00:00
|
|
|
|
}
|
2014-10-16 02:10:25 +00:00
|
|
|
|
|
|
|
|
|
if (logEvent.Exception != null)
|
2011-04-25 17:48:16 +00:00
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(log.Message))
|
2014-10-16 02:10:25 +00:00
|
|
|
|
{
|
|
|
|
|
log.Message = logEvent.Exception.Message;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Message += ": " + logEvent.Exception.Message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Exception = logEvent.Exception.ToString();
|
|
|
|
|
log.ExceptionType = logEvent.Exception.GetType().ToString();
|
2011-04-25 17:48:16 +00:00
|
|
|
|
}
|
2011-04-24 05:48:12 +00:00
|
|
|
|
|
2014-10-16 02:10:25 +00:00
|
|
|
|
log.Level = logEvent.Level.Name;
|
2010-10-24 07:46:58 +00:00
|
|
|
|
|
2014-10-16 02:10:25 +00:00
|
|
|
|
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection);
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
2014-10-16 02:10:25 +00:00
|
|
|
|
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
|
2014-10-17 14:07:21 +00:00
|
|
|
|
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });
|
2014-10-16 02:10:25 +00:00
|
|
|
|
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();
|
2014-03-05 01:02:36 +00:00
|
|
|
|
}
|
2014-03-05 07:11:38 +00:00
|
|
|
|
catch (SQLiteException ex)
|
2014-03-05 01:02:36 +00:00
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
|
InternalLogger.Error(ex, "Unable to save log event to database");
|
2014-03-05 01:02:36 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2010-10-24 07:46:58 +00:00
|
|
|
|
}
|
2013-07-30 00:57:38 +00:00
|
|
|
|
|
|
|
|
|
public void Handle(ApplicationShutdownRequested message)
|
|
|
|
|
{
|
2020-09-22 20:32:57 +00:00
|
|
|
|
if (LogManager.Configuration != null && LogManager.Configuration.LoggingRules.Contains(Rule))
|
2013-07-30 00:57:38 +00:00
|
|
|
|
{
|
|
|
|
|
UnRegister();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-24 07:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|