mirror of
https://github.com/Radarr/Radarr
synced 2025-02-22 14:21:14 +00:00
Fixed: Leaking of objects when logging something to the database.
This commit is contained in:
parent
09899fcf6c
commit
42015d5d95
1 changed files with 20 additions and 11 deletions
|
@ -15,13 +15,14 @@ public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownReque
|
||||||
{
|
{
|
||||||
private readonly SQLiteConnection _connection;
|
private readonly SQLiteConnection _connection;
|
||||||
|
|
||||||
|
private readonly IConnectionStringFactory _connectionStringFactory;
|
||||||
|
|
||||||
const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
|
const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
|
||||||
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
|
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
|
||||||
|
|
||||||
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
|
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
|
||||||
{
|
{
|
||||||
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString);
|
_connectionStringFactory = connectionStringFactory;
|
||||||
_connection.Open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Register()
|
public void Register()
|
||||||
|
@ -84,8 +85,13 @@ protected override void Write(LogEventInfo logEvent)
|
||||||
|
|
||||||
log.Level = logEvent.Level.Name;
|
log.Level = logEvent.Level.Name;
|
||||||
|
|
||||||
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection);
|
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("Message", DbType.String) { Value = log.Message });
|
||||||
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });
|
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("Logger", DbType.String) { Value = log.Logger });
|
||||||
|
@ -95,6 +101,9 @@ protected override void Write(LogEventInfo logEvent)
|
||||||
|
|
||||||
sqlCommand.ExecuteNonQuery();
|
sqlCommand.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
catch (SQLiteException ex)
|
catch (SQLiteException ex)
|
||||||
{
|
{
|
||||||
InternalLogger.Error("Unable to save log event to database: {0}", ex);
|
InternalLogger.Error("Unable to save log event to database: {0}", ex);
|
||||||
|
@ -104,7 +113,7 @@ protected override void Write(LogEventInfo logEvent)
|
||||||
|
|
||||||
public void Handle(ApplicationShutdownRequested message)
|
public void Handle(ApplicationShutdownRequested message)
|
||||||
{
|
{
|
||||||
if (LogManager.Configuration.LoggingRules.Contains(Rule))
|
if (LogManager.Configuration?.LoggingRules?.Contains(Rule) == true)
|
||||||
{
|
{
|
||||||
UnRegister();
|
UnRegister();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue