Improved logging system.
This commit is contained in:
parent
d94b1cb813
commit
98d13ebbbb
24 changed files with 202 additions and 142 deletions
|
@ -1,4 +1,3 @@
|
|||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SQLite;
|
||||
using NativeSQLiteConnection = System.Data.SQLite.SQLiteConnection;
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using SharpChat.Messages;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SQLite;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using ZLogger;
|
||||
|
||||
namespace SharpChat.SQLite;
|
||||
|
||||
public class SQLiteMessageStorage(SQLiteConnection conn) : MessageStorage {
|
||||
public class SQLiteMessageStorage(ILogger logger, SQLiteConnection conn) : MessageStorage {
|
||||
public async Task LogMessage(
|
||||
long id,
|
||||
string type,
|
||||
|
@ -37,7 +39,7 @@ public class SQLiteMessageStorage(SQLiteConnection conn) : MessageStorage {
|
|||
new SQLiteParameter("data", data == null ? "{}" : JsonSerializer.SerializeToUtf8Bytes(data))
|
||||
);
|
||||
} catch(SQLiteException ex) {
|
||||
Logger.Write(ex);
|
||||
logger.ZLogError($"Error in LogMessage(): {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +50,7 @@ public class SQLiteMessageStorage(SQLiteConnection conn) : MessageStorage {
|
|||
new SQLiteParameter("id", msg.Id)
|
||||
);
|
||||
} catch(SQLiteException ex) {
|
||||
Logger.Write(ex);
|
||||
logger.ZLogError($"Error in DeleteMessage(): {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,7 +88,7 @@ public class SQLiteMessageStorage(SQLiteConnection conn) : MessageStorage {
|
|||
return evt;
|
||||
}
|
||||
} catch(SQLiteException ex) {
|
||||
Logger.Write(ex);
|
||||
logger.ZLogError($"Error in GetMessage(): {ex}");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -116,7 +118,7 @@ public class SQLiteMessageStorage(SQLiteConnection conn) : MessageStorage {
|
|||
msgs.Add(evt);
|
||||
}
|
||||
} catch(SQLiteException ex) {
|
||||
Logger.Write(ex);
|
||||
logger.ZLogError($"Error in GetMessages(): {ex}");
|
||||
}
|
||||
|
||||
msgs.Reverse();
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using ZLogger;
|
||||
|
||||
namespace SharpChat.SQLite;
|
||||
|
||||
public class SQLiteMigrations(SQLiteConnection conn) {
|
||||
public class SQLiteMigrations(ILogger logger, SQLiteConnection conn) {
|
||||
public async Task RunMigrations() {
|
||||
long currentVersion = await conn.RunQueryValue<long>("PRAGMA user_version");
|
||||
long version = currentVersion;
|
||||
|
||||
async Task doMigration(int expect, Func<Task> action) {
|
||||
if(version < expect) {
|
||||
logger.ZLogInformation($"Upgrading to version {version}...");
|
||||
await action();
|
||||
++version;
|
||||
}
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using SharpChat.Configuration;
|
||||
using SharpChat.Messages;
|
||||
using System.Data.SQLite;
|
||||
using ZLogger;
|
||||
using NativeSQLiteConnection = System.Data.SQLite.SQLiteConnection;
|
||||
|
||||
namespace SharpChat.SQLite;
|
||||
|
||||
public class SQLiteStorage(string connString) : Storage, IDisposable {
|
||||
public class SQLiteStorage(ILogger logger, string connString) : Storage, IDisposable {
|
||||
public const string MEMORY = "file::memory:?cache=shared";
|
||||
public const string DEFAULT = "sharpchat.db";
|
||||
|
||||
public SQLiteConnection Connection { get; } = new SQLiteConnection(new NativeSQLiteConnection(connString).OpenAndReturn());
|
||||
|
||||
public MessageStorage CreateMessageStorage() {
|
||||
return new SQLiteMessageStorage(Connection);
|
||||
return new SQLiteMessageStorage(logger, Connection);
|
||||
}
|
||||
|
||||
public async Task UpgradeStorage() {
|
||||
await new SQLiteMigrations(Connection).RunMigrations();
|
||||
logger.ZLogInformation($"Upgrading storage...");
|
||||
await new SQLiteMigrations(logger, Connection).RunMigrations();
|
||||
}
|
||||
|
||||
public static string BuildConnectionString(Config config) {
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||
<PackageReference Include="ZLogger" Version="2.5.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue