sonarr-repo-only/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs

121 lines
4.6 KiB
C#
Raw Normal View History

2021-08-03 04:43:28 +00:00
using System.Data;
2014-03-05 07:11:38 +00:00
using System.Data.SQLite;
2021-08-03 04:43:28 +00:00
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Targets;
using NzbDrone.Common.Instrumentation;
2014-10-16 02:10:25 +00:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Instrumentation
{
public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownRequested>
{
2021-08-03 04:43:28 +00:00
private const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
private readonly IConnectionStringFactory _connectionStringFactory;
2014-10-16 02:10:25 +00:00
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
{
_connectionStringFactory = connectionStringFactory;
}
public void Register()
{
var target = new SlowRunningAsyncTargetWrapper(this) { TimeToSleepBetweenBatches = 500 };
2013-05-21 03:20:29 +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);
LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded;
2013-05-21 03:20:29 +00:00
LogManager.ReconfigExistingLoggers();
}
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; }
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."))
{
2014-10-16 02:10:25 +00:00
log.Logger = log.Logger.Remove(0, 9);
}
2014-10-16 02:10:25 +00:00
if (logEvent.Exception != null)
{
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-24 05:48:12 +00:00
2014-10-16 02:10:25 +00:00
log.Level = logEvent.Level.Name;
using (var connection =
SQLiteFactory.Instance.CreateConnection())
{
connection.ConnectionString = _connectionStringFactory.LogDbConnectionString;
connection.Open();
using (var sqlCommand = connection.CreateCommand())
{
sqlCommand.CommandText = INSERT_COMMAND;
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });
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 07:11:38 +00:00
catch (SQLiteException ex)
{
InternalLogger.Error("Unable to save log event to database: {0}", ex);
throw;
}
}
public void Handle(ApplicationShutdownRequested message)
{
if (LogManager.Configuration?.LoggingRules?.Contains(Rule) == true)
{
UnRegister();
}
}
}
2021-08-03 04:43:28 +00:00
}