From 42015d5d9511f95f5d8ae59e7d319cbf84b285f9 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Sat, 1 Dec 2018 15:00:09 +0100 Subject: [PATCH] Fixed: Leaking of objects when logging something to the database. --- .../Instrumentation/DatabaseTarget.cs | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index f69de8224..13c351694 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -15,13 +15,14 @@ namespace NzbDrone.Core.Instrumentation { private readonly SQLiteConnection _connection; + private readonly IConnectionStringFactory _connectionStringFactory; + const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " + "VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)"; public DatabaseTarget(IConnectionStringFactory connectionStringFactory) { - _connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString); - _connection.Open(); + _connectionStringFactory = connectionStringFactory; } public void Register() @@ -84,16 +85,24 @@ namespace NzbDrone.Core.Instrumentation log.Level = logEvent.Level.Name; - 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.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 }); + using (var connection = + SQLiteFactory.Instance.CreateConnection()) + { + connection.ConnectionString = _connectionStringFactory.LogDbConnectionString; + 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(); + } + } - sqlCommand.ExecuteNonQuery(); } catch (SQLiteException ex) { @@ -104,7 +113,7 @@ namespace NzbDrone.Core.Instrumentation public void Handle(ApplicationShutdownRequested message) { - if (LogManager.Configuration.LoggingRules.Contains(Rule)) + if (LogManager.Configuration?.LoggingRules?.Contains(Rule) == true) { UnRegister(); }