Skip to content

Commit 47fe874

Browse files
committed
Target net8 and update dependencies
1 parent fb9e3f2 commit 47fe874

16 files changed

+391
-403
lines changed

Benchmarks/Benchmarks.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
11-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
10+
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
11+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

Console/Console.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0" />
11-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
11+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

ConsoleStress/ConsoleStress.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
11-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

SqliteLogger/ConnectionManager.cs

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,121 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7-
namespace SqliteLogger
7+
namespace SqliteLogger;
8+
9+
internal sealed class ConnectionManager : IDisposable
810
{
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)
1018
{
11-
private readonly string _connectionString;
12-
private readonly CancellationTokenSource _stoppingTokenSource = new();
19+
string fullFilePath = Path.GetFullPath(_config.FilePath);
20+
SqliteConnection connection;
1321

14-
private readonly SqliteConnection? _queueConnection;
15-
private readonly Task? _queueTask;
22+
SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new()
23+
{
24+
DataSource = fullFilePath
25+
};
26+
_connectionString = sqliteConnectionStringBuilder.ToString();
1627

17-
public ConnectionManager(SqliteLoggerConfiguration _config)
28+
// Ensure the file is created
29+
using (connection = new SqliteConnection(_connectionString))
1830
{
19-
string fullFilePath = Path.GetFullPath(_config.FilePath);
20-
SqliteConnection connection;
31+
connection.Open();
32+
CreateTables(connection, "main", true);
33+
}
2134

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();
2740

28-
// Ensure the file is created
29-
using (connection = new SqliteConnection(_connectionString))
41+
using (SqliteCommand command = connection.CreateCommand())
3042
{
31-
connection.Open();
32-
CreateTables(connection, "main", true);
43+
command.CommandText = $"ATTACH DATABASE '{fullFilePath}' AS 'file'";
44+
command.ExecuteNonQuery();
3345
}
3446

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);
4049

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;
4654

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);
5756
}
57+
}
5858

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())
6062
{
61-
using (var transaction = connection.BeginTransaction())
63+
using (SqliteCommand command = connection.CreateCommand())
6264
{
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)
6495
{
65-
command.Transaction = transaction;
6696
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+
")";
76100
command.ExecuteNonQuery();
77101

78102
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+
")";
92106
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-
}
108107
}
109-
transaction.Commit();
110108
}
109+
transaction.Commit();
111110
}
111+
}
112112

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+
}
117117

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();
124123
}
125124
}

SqliteLogger/ILoggerConnection.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22

3-
namespace SqliteLogger
3+
namespace SqliteLogger;
4+
5+
internal interface ILoggerConnection : IDisposable
46
{
5-
internal interface ILoggerConnection : IDisposable
6-
{
7-
public IDisposable BeginScope();
7+
public IDisposable BeginScope();
88

9-
public void Log(DateTimeOffset timestamp, string name, string level, string state, string? exceptionId, string message);
9+
public void Log(DateTimeOffset timestamp, string name, string level, string state, string? exceptionId, string message);
1010

11-
public void LogException(
12-
string timestamp, int sequence, string id, string? data, int? hResult, string? innerExceptionId, string message,
13-
string? source, string? stackTrace, string? targetSite);
14-
}
11+
public void LogException(
12+
string timestamp, int sequence, string id, string? data, int? hResult, string? innerExceptionId, string message,
13+
string? source, string? stackTrace, string? targetSite);
1514
}

SqliteLogger/LogQueueTask.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,34 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55

6-
namespace SqliteLogger
6+
namespace SqliteLogger;
7+
8+
internal class LogQueueTask
79
{
8-
internal class LogQueueTask
9-
{
10-
private readonly SqliteConnection _source;
10+
private readonly SqliteConnection _source;
1111

12-
public LogQueueTask(SqliteConnection source)
13-
{
14-
_source = source;
15-
}
12+
public LogQueueTask(SqliteConnection source)
13+
{
14+
_source = source;
15+
}
1616

17-
public TimeSpan Delay { get; set; }
17+
public TimeSpan Delay { get; set; }
1818

19-
public async Task RunAsync(CancellationToken stoppingToken)
19+
public async Task RunAsync(CancellationToken stoppingToken)
20+
{
21+
while (!stoppingToken.IsCancellationRequested)
2022
{
21-
while (!stoppingToken.IsCancellationRequested)
22-
{
23-
await Task.Delay(Delay, CancellationToken.None).ConfigureAwait(false);
23+
await Task.Delay(Delay, CancellationToken.None).ConfigureAwait(false);
2424

25-
SqliteCommand command = _source.CreateCommand();
26-
command.CommandText =
27-
"BEGIN IMMEDIATE TRANSACTION; " +
28-
"INSERT INTO file.exceptions SELECT * FROM main.exceptions; " +
29-
"DELETE FROM main.exceptions; " +
30-
"INSERT INTO file.traces SELECT * FROM main.traces; " +
31-
"DELETE FROM main.traces; " +
32-
"COMMIT;";
33-
command.ExecuteNonQuery();
34-
}
25+
SqliteCommand command = _source.CreateCommand();
26+
command.CommandText =
27+
"BEGIN IMMEDIATE TRANSACTION; " +
28+
"INSERT INTO file.exceptions SELECT * FROM main.exceptions; " +
29+
"DELETE FROM main.exceptions; " +
30+
"INSERT INTO file.traces SELECT * FROM main.traces; " +
31+
"DELETE FROM main.traces; " +
32+
"COMMIT;";
33+
command.ExecuteNonQuery();
3534
}
3635
}
3736
}

SqliteLogger/NullDisposable.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using System;
22

3-
namespace SqliteLogger
3+
namespace SqliteLogger;
4+
5+
internal class NullDisposable : IDisposable
46
{
5-
internal class NullDisposable : IDisposable
6-
{
7-
public static readonly IDisposable Instance = new NullDisposable();
7+
public static readonly IDisposable Instance = new NullDisposable();
88

9-
private NullDisposable()
10-
{
9+
private NullDisposable()
10+
{
1111

12-
}
12+
}
1313

14-
public void Dispose()
15-
{
16-
return;
17-
}
14+
public void Dispose()
15+
{
16+
return;
1817
}
1918
}

0 commit comments

Comments
 (0)