Updated DBSyncWatcher
Added new read code for reading data in from a database file. Added Logger to log when a file is created, or the file has been changed. Changed Error to log as error, instead of Console writting.
This commit is contained in:
parent
823bb11b83
commit
725a37d9cf
1 changed files with 31 additions and 7 deletions
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using Serilog;
|
||||
|
||||
namespace FreeTubeSyncer.Library;
|
||||
|
||||
|
|
@ -44,8 +45,15 @@ public class DBSyncWatcher
|
|||
|
||||
if (!WatchFiles.Keys.Contains(dbName)) return;
|
||||
|
||||
var data = File.ReadAllText(e.FullPath);
|
||||
foreach (var line in data.Split('\n'))
|
||||
Log.Information("Database File Changed: {DbName}", dbName);
|
||||
|
||||
var data = new List<string>();
|
||||
|
||||
using var fh = File.OpenText(e.FullPath);
|
||||
while (!fh.EndOfStream)
|
||||
data.Add(fh.ReadLine() ?? string.Empty);
|
||||
|
||||
foreach (var line in data)
|
||||
{
|
||||
if (line == "") continue;
|
||||
var type = WatchFiles[dbName];
|
||||
|
|
@ -56,11 +64,27 @@ public class DBSyncWatcher
|
|||
private void HandleCreated(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
if (e.ChangeType != WatcherChangeTypes.Created) return;
|
||||
var dbName = Path.GetFileName(e.FullPath);
|
||||
if (!WatchFiles.Keys.Contains(dbName)) return;
|
||||
var data = File.ReadAllText(e.FullPath);
|
||||
foreach (var line in data.Split('\n'))
|
||||
|
||||
while (Locked)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
|
||||
var dbName = Path.GetFileName(e.FullPath);
|
||||
|
||||
if (!WatchFiles.Keys.Contains(dbName)) return;
|
||||
|
||||
Log.Information("Database File Created: {DbName}", dbName);
|
||||
|
||||
var data = new List<string>();
|
||||
using var fh = File.OpenText(e.FullPath);
|
||||
|
||||
while (!fh.EndOfStream)
|
||||
data.Add(fh.ReadLine() ?? string.Empty);
|
||||
|
||||
foreach (var line in data)
|
||||
{
|
||||
if (line == "") continue;
|
||||
var type = WatchFiles[dbName];
|
||||
OnDatabaseChange?.Invoke(dbName, line);
|
||||
}
|
||||
|
|
@ -68,6 +92,6 @@ public class DBSyncWatcher
|
|||
|
||||
private void HandleError(object sender, ErrorEventArgs e)
|
||||
{
|
||||
Console.WriteLine("Error: {0}\n{1}", e.GetException().Message, e.GetException().StackTrace);
|
||||
Log.Error("Error: {Message}\n{StackTrace}", e.GetException().Message, e.GetException().StackTrace);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue