Updated Syncer

Added Logging functionality to write to log files.
This commit is contained in:
Mario Steele 2025-07-31 16:14:21 -05:00
parent 725a37d9cf
commit c94b709145

View file

@ -10,6 +10,8 @@ using System.Threading.Tasks;
using FreeTubeSyncer.Library;
using FreeTubeSyncer.Models.DatabaseModels;
using RestSharp;
using Serilog;
using Serilog.Core;
namespace FreeTubeSyncer.REST;
@ -68,34 +70,29 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
public async Task<bool> PingApi()
{
// TODO: Replace with Logger
Console.WriteLine($"Pinging API at {_client.BuildUri(new RestRequest("/ping"))}...");
Log.Information("Pinging API at {BuildUri}...", _client.BuildUri(new RestRequest("/ping")));
try
{
var res = await _client.GetAsync<Ping>(new RestRequest("/ping"));
if (res == null)
{
// TODO: Replace with Logger
Console.WriteLine($"Ping returned null, not the server we are looking for!");
Log.Information("Ping returned null, not the server we are looking for!");
return false;
}
if (res.AppVersion == "0.1.3")
{
// TODO: Replace with Logger
Console.WriteLine($"Server Online! {res.AppVersion}");
Log.Information("Server Online! {AppVersion}", res.AppVersion);
return true;
}
}
catch (Exception ex)
{
// TODO: Replace with Logger
Console.WriteLine($"Network Error: {ex.Message}, API not alive.");
Log.Error("API not alive. Network Error: {Message}", ex.Message);
return false;
}
// TODO: Replace with Logger
Console.WriteLine("Responded with something other then 404, API not what we expected.");
Log.Error("Responded with something other then Ping. This is not the server we are looking for...");
return false;
}
@ -128,8 +125,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (_entries.Any(x => x.EqualId(item.Id())))
_entries.RemoveAll(x => x.EqualId(item.Id()));
_entries.Add(item);
// TODO: Replace with Logger
Console.WriteLine($"Posting to REST: {item.Id()}");
Log.Information("Posting to REST: {ItemId}", item.Id());
await _client.PostJsonAsync<T>(_restEndpoint, item);
}
}
@ -147,15 +143,11 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (data.Equals(entry)) continue;
// TODO: Replace with Logger
Console.WriteLine($"Updated Entry from REST for {_dbName} - {entry.Id()}");
Log.Information("Updated Entry from REST for {DbName} - {EntryId}", _dbName, entry.Id());
_entries.RemoveAll(x => x.EqualId(entry.Id()));
}
else
{
// TODO: Replace with Logger
Console.WriteLine($"New Entry from REST for {_dbName} - {entry.Id()}");
}
Log.Information("New Entry from REST for {DbName} - {EntryId}", _dbName, entry.Id());
_entries.Add(entry);
_isDirty = true;
@ -186,8 +178,8 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
}
catch (Exception iex)
{
// TODO: Replace with Logger
Console.WriteLine($"Failed to parse line: {entryObject}\nMessage: {iex.Message}");
Log.Error("Failed to parse line: {EntryLine}", entryObject);
Log.Error("Error Message: {Messsage}", iex.Message);
entry = null;
}
}
@ -199,15 +191,11 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
{
var data = _entries.First(x => x.EqualId(entry.Id()));
if (data.Equals(entry)) return;
// TODO: Replace with Logger
Console.WriteLine($"File Entry {entry.Id()} updated for {_dbName}");
Log.Information("Updated File Entry {EntryId} updated for {DbName}", entry.Id(), _dbName);
_entries.RemoveAll(x => x.EqualId(entry.Id()));
}
else
{
// TODO: Replace with Logger
Console.WriteLine($"New File Entry {entry.Id()} for {_dbName}");
}
Log.Information("New File Entry {EntryId} for {DbName}", entry.Id(), _dbName);
_entries.Add(entry);
await _client.PostJsonAsync<T>(_restEndpoint, entry);
@ -219,8 +207,8 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (!_isDirty)
return;
_syncing = true;
// TODO: Replace with Logger
Console.WriteLine($"Syncing {_dbPath}...");
Log.Information("Syncing {DbName}...", _dbPath);
var start = DateTime.Now;
var json = new List<string>();
foreach (var entry in _entries)
json.Add(entry.JsonData());
@ -233,8 +221,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
fh.Close();
}
_watcher.Locked = false;
// TODO: Replace with Logger
Console.WriteLine($"Updated {_dbPath}.");
Log.Information("Updated {DbName}, completed in {TimeSpan}", _dbName, DateTime.Now - start);
_isDirty = false;
_syncing = false;
}