|
4 | 4 | using System.Threading;
|
5 | 5 | using System.Threading.Tasks;
|
6 | 6 |
|
7 |
| -namespace SqliteLogger |
| 7 | +namespace SqliteLogger; |
| 8 | + |
| 9 | +internal sealed class ConnectionManager : IDisposable |
8 | 10 | {
|
9 |
| - internal sealed class ConnectionManager : IDisposable |
| 11 | + private readonly string _connectionString; |
| 12 | + private readonly CancellationTokenSource _stoppingTokenSource = new(); |
| 13 | + |
| 14 | + private readonly SqliteConnection? _queueConnection; |
| 15 | + private readonly Task? _queueTask; |
| 16 | + |
| 17 | + public ConnectionManager(SqliteLoggerConfiguration _config) |
10 | 18 | {
|
11 |
| - private readonly string _connectionString; |
12 |
| - private readonly CancellationTokenSource _stoppingTokenSource = new(); |
| 19 | + string fullFilePath = Path.GetFullPath(_config.FilePath); |
| 20 | + SqliteConnection connection; |
13 | 21 |
|
14 |
| - private readonly SqliteConnection? _queueConnection; |
15 |
| - private readonly Task? _queueTask; |
| 22 | + SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new() |
| 23 | + { |
| 24 | + DataSource = fullFilePath |
| 25 | + }; |
| 26 | + _connectionString = sqliteConnectionStringBuilder.ToString(); |
16 | 27 |
|
17 |
| - public ConnectionManager(SqliteLoggerConfiguration _config) |
| 28 | + // Ensure the file is created |
| 29 | + using (connection = new SqliteConnection(_connectionString)) |
18 | 30 | {
|
19 |
| - string fullFilePath = Path.GetFullPath(_config.FilePath); |
20 |
| - SqliteConnection connection; |
| 31 | + connection.Open(); |
| 32 | + CreateTables(connection, "main", true); |
| 33 | + } |
21 | 34 |
|
22 |
| - SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new() |
23 |
| - { |
24 |
| - DataSource = fullFilePath |
25 |
| - }; |
26 |
| - _connectionString = sqliteConnectionStringBuilder.ToString(); |
| 35 | + if (_config.UseQueue) |
| 36 | + { |
| 37 | + _connectionString = "Data Source=file::memory:?cache=shared"; |
| 38 | + connection = new SqliteConnection(_connectionString); |
| 39 | + connection.Open(); |
27 | 40 |
|
28 |
| - // Ensure the file is created |
29 |
| - using (connection = new SqliteConnection(_connectionString)) |
| 41 | + using (SqliteCommand command = connection.CreateCommand()) |
30 | 42 | {
|
31 |
| - connection.Open(); |
32 |
| - CreateTables(connection, "main", true); |
| 43 | + command.CommandText = $"ATTACH DATABASE '{fullFilePath}' AS 'file'"; |
| 44 | + command.ExecuteNonQuery(); |
33 | 45 | }
|
34 | 46 |
|
35 |
| - if (_config.UseQueue) |
36 |
| - { |
37 |
| - _connectionString = "Data Source=file::memory:?cache=shared"; |
38 |
| - connection = new SqliteConnection(_connectionString); |
39 |
| - connection.Open(); |
| 47 | + CreateTables(connection); |
| 48 | + CreateTables(connection, "file", true); |
40 | 49 |
|
41 |
| - using (SqliteCommand command = connection.CreateCommand()) |
42 |
| - { |
43 |
| - command.CommandText = $"ATTACH DATABASE '{fullFilePath}' AS 'file'"; |
44 |
| - command.ExecuteNonQuery(); |
45 |
| - } |
| 50 | + _queueConnection = connection; |
| 51 | + var task = new LogQueueTask(_queueConnection); |
| 52 | + TimeSpan minDelay = TimeSpan.FromMilliseconds(1); |
| 53 | + task.Delay = _config.DelayBetweenQueueDrain < minDelay ? minDelay : _config.DelayBetweenQueueDrain; |
46 | 54 |
|
47 |
| - CreateTables(connection); |
48 |
| - CreateTables(connection, "file", true); |
49 |
| - |
50 |
| - _queueConnection = connection; |
51 |
| - var task = new LogQueueTask(_queueConnection); |
52 |
| - TimeSpan minDelay = TimeSpan.FromMilliseconds(1); |
53 |
| - task.Delay = _config.DelayBetweenQueueDrain < minDelay ? minDelay : _config.DelayBetweenQueueDrain; |
54 |
| - |
55 |
| - _queueTask = task.RunAsync(_stoppingTokenSource.Token); |
56 |
| - } |
| 55 | + _queueTask = task.RunAsync(_stoppingTokenSource.Token); |
57 | 56 | }
|
| 57 | + } |
58 | 58 |
|
59 |
| - private static void CreateTables(SqliteConnection connection, string schema = "main", bool createIndexes = false) |
| 59 | + private static void CreateTables(SqliteConnection connection, string schema = "main", bool createIndexes = false) |
| 60 | + { |
| 61 | + using (var transaction = connection.BeginTransaction()) |
60 | 62 | {
|
61 |
| - using (var transaction = connection.BeginTransaction()) |
| 63 | + using (SqliteCommand command = connection.CreateCommand()) |
62 | 64 | {
|
63 |
| - using (SqliteCommand command = connection.CreateCommand()) |
| 65 | + command.Transaction = transaction; |
| 66 | + command.CommandText = |
| 67 | + $"CREATE TABLE IF NOT EXISTS {schema}.traces (" + |
| 68 | + "timestamp TEXT NOT NULL, " + |
| 69 | + "name TEXT NOT NULL, " + |
| 70 | + "level TEXT NOT NULL, " + |
| 71 | + "state TEXT, " + |
| 72 | + "exception_id TEXT , " + |
| 73 | + "message TEXT" + |
| 74 | + ");"; |
| 75 | + |
| 76 | + command.ExecuteNonQuery(); |
| 77 | + |
| 78 | + command.CommandText = |
| 79 | + $"CREATE TABLE IF NOT EXISTS {schema}.exceptions (" + |
| 80 | + "timestamp TEXT NOT NULL, " + // denorming |
| 81 | + "sequence INTEGER NOT NULL, " + // denorming |
| 82 | + "id TEXT PRIMARY KEY, " + // A primary key |
| 83 | + "data TEXT, " + // JSON IDictionary |
| 84 | + "hresult INTEGER, " + |
| 85 | + "inner_exception_id TEXT REFERENCES exceptions(id) ON DELETE CASCADE ON UPDATE CASCADE, " + // primary key of another exception, creating a hierarchy |
| 86 | + "message TEXT NOT NULL, " + |
| 87 | + "source TEXT, " + |
| 88 | + "stacktrace TEXT, " + // This can get big |
| 89 | + "targetsite TEXT" + |
| 90 | + ");"; |
| 91 | + |
| 92 | + command.ExecuteNonQuery(); |
| 93 | + |
| 94 | + if (createIndexes) |
64 | 95 | {
|
65 |
| - command.Transaction = transaction; |
66 | 96 | command.CommandText =
|
67 |
| - $"CREATE TABLE IF NOT EXISTS {schema}.traces (" + |
68 |
| - "timestamp TEXT NOT NULL, " + |
69 |
| - "name TEXT NOT NULL, " + |
70 |
| - "level TEXT NOT NULL, " + |
71 |
| - "state TEXT, " + |
72 |
| - "exception_id TEXT , " + |
73 |
| - "message TEXT" + |
74 |
| - ");"; |
75 |
| - |
| 97 | + $"CREATE INDEX IF NOT EXISTS {schema}.idx_traces_timespamp_desc ON traces (" + |
| 98 | + "timestamp DESC" + |
| 99 | + ")"; |
76 | 100 | command.ExecuteNonQuery();
|
77 | 101 |
|
78 | 102 | command.CommandText =
|
79 |
| - $"CREATE TABLE IF NOT EXISTS {schema}.exceptions (" + |
80 |
| - "timestamp TEXT NOT NULL, " + // denorming |
81 |
| - "sequence INTEGER NOT NULL, " + // denorming |
82 |
| - "id TEXT PRIMARY KEY, " + // A primary key |
83 |
| - "data TEXT, " + // JSON IDictionary |
84 |
| - "hresult INTEGER, " + |
85 |
| - "inner_exception_id TEXT REFERENCES exceptions(id) ON DELETE CASCADE ON UPDATE CASCADE, " + // primary key of another exception, creating a hierarchy |
86 |
| - "message TEXT NOT NULL, " + |
87 |
| - "source TEXT, " + |
88 |
| - "stacktrace TEXT, " + // This can get big |
89 |
| - "targetsite TEXT" + |
90 |
| - ");"; |
91 |
| - |
| 103 | + $"CREATE INDEX IF NOT EXISTS {schema}.idx_exceptions_timespamp_desc ON exceptions (" + |
| 104 | + "timestamp DESC" + |
| 105 | + ")"; |
92 | 106 | command.ExecuteNonQuery();
|
93 |
| - |
94 |
| - if (createIndexes) |
95 |
| - { |
96 |
| - command.CommandText = |
97 |
| - $"CREATE INDEX IF NOT EXISTS {schema}.idx_traces_timespamp_desc ON traces (" + |
98 |
| - "timestamp DESC" + |
99 |
| - ")"; |
100 |
| - command.ExecuteNonQuery(); |
101 |
| - |
102 |
| - command.CommandText = |
103 |
| - $"CREATE INDEX IF NOT EXISTS {schema}.idx_exceptions_timespamp_desc ON exceptions (" + |
104 |
| - "timestamp DESC" + |
105 |
| - ")"; |
106 |
| - command.ExecuteNonQuery(); |
107 |
| - } |
108 | 107 | }
|
109 |
| - transaction.Commit(); |
110 | 108 | }
|
| 109 | + transaction.Commit(); |
111 | 110 | }
|
| 111 | + } |
112 | 112 |
|
113 |
| - public SqliteLoggerConnection CreateConnection() |
114 |
| - { |
115 |
| - return new SqliteLoggerConnection(new SqliteConnection(_connectionString)); |
116 |
| - } |
| 113 | + public SqliteLoggerConnection CreateConnection() |
| 114 | + { |
| 115 | + return new SqliteLoggerConnection(new SqliteConnection(_connectionString)); |
| 116 | + } |
117 | 117 |
|
118 |
| - public void Dispose() |
119 |
| - { |
120 |
| - _stoppingTokenSource?.Cancel(); |
121 |
| - _queueTask?.Wait(); |
122 |
| - _queueConnection?.Close(); |
123 |
| - } |
| 118 | + public void Dispose() |
| 119 | + { |
| 120 | + _stoppingTokenSource?.Cancel(); |
| 121 | + _queueTask?.Wait(); |
| 122 | + _queueConnection?.Close(); |
124 | 123 | }
|
125 | 124 | }
|
0 commit comments