add logging stuff

This commit is contained in:
Stanley Dimant
2022-07-03 02:24:32 +02:00
parent d56923d553
commit 15b220dc8b
9 changed files with 81 additions and 49 deletions

View File

@@ -21,7 +21,7 @@ namespace MareSynchronosServer.Hubs
if (userId != null)
{
Logger.LogInformation("Heartbeat from " + userId);
Logger.LogInformation("Connection from " + userId);
var user = (await DbContext.Users.SingleAsync(u => u.UID == userId));
return new LoggedInUserDto
{

View File

@@ -42,6 +42,7 @@ namespace MareSynchronosServer.Hubs
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
{
fileListHashes = fileListHashes.Distinct().ToList();
Logger.LogInformation("User " + AuthenticatedUserId + " sending files");
var forbiddenFiles = DbContext.ForbiddenUploadEntries.Where(f => fileListHashes.Contains(f.Hash));
var filesToUpload = new List<UploadFileDto>();
@@ -51,10 +52,11 @@ namespace MareSynchronosServer.Hubs
Hash = f.Hash,
IsForbidden = true
}));
var existingFiles = DbContext.Files.Where(f => fileListHashes.Contains(f.Hash)).ToList();
fileListHashes.RemoveAll(f => filesToUpload.Any(u => u.Hash == f));
var existingFiles = DbContext.Files.Where(f => fileListHashes.Contains(f.Hash));
foreach (var file in fileListHashes.Where(f => existingFiles.All(e => e.Hash != f) && filesToUpload.All(u => u.Hash != f)))
{
Logger.LogInformation("Needs upload: " + file);
Logger.LogInformation("User " + AuthenticatedUserId + " needs upload: " + file);
var userId = AuthenticatedUserId;
await DbContext.Files.AddAsync(new FileCache()
{
@@ -90,9 +92,26 @@ namespace MareSynchronosServer.Hubs
var forbiddenFile = DbContext.ForbiddenUploadEntries.SingleOrDefault(f => f.Hash == hash);
if (forbiddenFile != null) return;
var uploadedFile = new List<byte>();
await foreach (var chunk in fileContent)
try
{
uploadedFile.AddRange(chunk);
await foreach (var chunk in fileContent)
{
uploadedFile.AddRange(chunk);
}
}
catch
{
DbContext.Files.Remove(relatedFile);
try
{
await DbContext.SaveChangesAsync();
}
catch
{
// already removed
}
return;
}
Logger.LogInformation("User " + AuthenticatedUserId + " upload finished: " + hash + ", size: " + uploadedFile.Count);
@@ -115,6 +134,7 @@ namespace MareSynchronosServer.Hubs
relatedFile.Uploaded = true;
relatedFile.LastAccessTime = DateTime.Now;
await DbContext.SaveChangesAsync();
Logger.LogInformation("File " + hash + " added to DB");
return;
}
catch (Exception ex)

View File

@@ -69,37 +69,11 @@ namespace MareSynchronosServer.Hubs
return AuthenticatedUserId;
}
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task GetCharacterData(Dictionary<string, int> visibleCharacterWithJobs)
{
var uid = AuthenticatedUserId;
Dictionary<string, CharacterCacheDto> ret = new();
var entriesHavingThisUser = DbContext.ClientPairs
.Include(w => w.User)
.Include(w => w.OtherUser)
.Where(w => w.OtherUser.UID == uid && !w.IsPaused && visibleCharacterWithJobs.Keys.Contains(w.User.CharacterIdentification))
.ToList();
foreach (var pair in entriesHavingThisUser)
{
bool isNotPaused = await DbContext.ClientPairs.AnyAsync(w =>
!w.IsPaused && w.User.UID == uid && w.OtherUser.UID == pair.User.UID);
if (!isNotPaused) continue;
var dictEntry = visibleCharacterWithJobs[pair.User.CharacterIdentification];
var cachedChar = await
DbContext.CharacterData.SingleOrDefaultAsync(c => c.UserId == pair.User.UID && c.JobId == dictEntry);
if (cachedChar != null)
{
await Clients.User(uid).SendAsync("ReceiveCharacterData", cachedChar.CharacterCache,
pair.User.CharacterIdentification);
}
}
}
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterCache, List<string> visibleCharacterIds)
{
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to visible clients");
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients");
var uid = AuthenticatedUserId;
var entriesHavingThisUser = DbContext.ClientPairs
@@ -166,7 +140,7 @@ namespace MareSynchronosServer.Hubs
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AuthScheme)]
public async Task<List<string>> GetOnlineCharacters()
{
Logger.LogInformation("User " + AuthenticatedUserId + " sent character hash");
Logger.LogInformation("User " + AuthenticatedUserId + " requested online characters");
var ownUser = DbContext.Users.Single(u => u.UID == AuthenticatedUserId);
var otherUsers = await DbContext.ClientPairs