From 44d6bdb2ba76530114182bc213e2dc421d24bc28 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Mon, 23 Sep 2024 15:05:30 +0300 Subject: [PATCH] Added logging --- src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs | 3 +++ src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs | 3 +++ src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs | 3 +++ src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs | 3 +++ .../Controllers/CrashUploadController.cs | 13 ++++++++++++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs b/src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs index c722d86..806516c 100644 --- a/src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs +++ b/src/BUTR.CrashReport.Server.v13/HtmlHandlerV13.cs @@ -65,7 +65,10 @@ public async Task UploadHtmlAsync(ControllerBase controller, Canc var html = await streamReader.ReadToEndAsync(ct); var (valid, version, crashReportModel) = ParseHtml(html); if (!valid) + { + _logger.LogWarning("Invalid HTML"); return controller.StatusCode(StatusCodes.Status500InternalServerError); + } if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReportModel!.Id, ct) is { } idEntity) return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}"); diff --git a/src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs b/src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs index 97182e7..036ee21 100644 --- a/src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs +++ b/src/BUTR.CrashReport.Server.v13/JsonHandlerV13.cs @@ -67,7 +67,10 @@ public async Task UploadJsonAsync(ControllerBase controller, Canc controller.Request.Body = await _gZipCompressor.DecompressAsync(controller.Request.Body, ct); if (await controller.HttpContext.Request.ReadFromJsonAsync(_jsonSerializerOptions, ct) is not { CrashReport: { } crashReport, LogSources: { } logSources }) + { + _logger.LogWarning("Failed to read JSON body"); return controller.StatusCode(StatusCodes.Status500InternalServerError); + } if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReport.Id, ct) is { } idEntity) return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}"); diff --git a/src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs b/src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs index acc9678..5c34d92 100644 --- a/src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs +++ b/src/BUTR.CrashReport.Server.v14/HtmlHandlerV14.cs @@ -65,7 +65,10 @@ public async Task UploadHtmlAsync(ControllerBase controller, Canc var html = await streamReader.ReadToEndAsync(ct); var (valid, version, crashReportModel) = ParseHtml(html); if (!valid) + { + _logger.LogWarning("Invalid HTML"); return controller.StatusCode(StatusCodes.Status500InternalServerError); + } if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReportModel!.Id, ct) is { } idEntity) return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}"); diff --git a/src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs b/src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs index a9b06d2..4e5be06 100644 --- a/src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs +++ b/src/BUTR.CrashReport.Server.v14/JsonHandlerV14.cs @@ -67,7 +67,10 @@ public async Task UploadJsonAsync(ControllerBase controller, Canc controller.Request.Body = await _gZipCompressor.DecompressAsync(controller.Request.Body, ct); if (await controller.HttpContext.Request.ReadFromJsonAsync(_jsonSerializerOptions, ct) is not { CrashReport: { } crashReport, LogSources: { } logSources }) + { + _logger.LogWarning("Failed to read JSON body"); return controller.StatusCode(StatusCodes.Status500InternalServerError); + } if (await _dbContext.IdEntities.FirstOrDefaultAsync(x => x.CrashReportId == crashReport.Id, ct) is { } idEntity) return controller.Ok($"{_options.BaseUri}/{idEntity.FileId}"); diff --git a/src/BUTR.CrashReport.Server/Controllers/CrashUploadController.cs b/src/BUTR.CrashReport.Server/Controllers/CrashUploadController.cs index 6344361..74f8042 100644 --- a/src/BUTR.CrashReport.Server/Controllers/CrashUploadController.cs +++ b/src/BUTR.CrashReport.Server/Controllers/CrashUploadController.cs @@ -53,7 +53,10 @@ public CrashUploadController( public Task CrashUploadAsync(CancellationToken ct) { if (Request.ContentLength is not { } contentLength || contentLength < _options.MinContentLength || contentLength > _options.MaxContentLength) + { + _logger.LogWarning("Content length is invalid: {ContentLength}", Request.ContentLength); return Task.FromResult(StatusCode(StatusCodes.Status500InternalServerError)); + } switch (Request.ContentType) { @@ -67,14 +70,18 @@ public Task CrashUploadAsync(CancellationToken ct) private async Task UploadJsonAsync(CancellationToken ct) { - switch (Request.Headers["CrashReportVersion"]) + var version = Request.Headers["CrashReportVersion"].ToString(); + switch (version) { case "13": return await _jsonHandlerV13.UploadJsonAsync(this, ct); case "14": return await _jsonHandlerV14.UploadJsonAsync(this, ct); default: + { + _logger.LogWarning("Crash report version is invalid: {CrashReportVersion}", version); return StatusCode(StatusCodes.Status500InternalServerError); + } } } @@ -109,13 +116,17 @@ static bool TryParseVersion(string content, out byte version) using var streamReader = new StreamReader(Request.Body); var html = await streamReader.ReadToEndAsync(ct); if (!TryParseVersion(html, out var version)) + { + _logger.LogWarning("Failed to parse html crash report version"); return StatusCode(StatusCodes.Status500InternalServerError); + } if (version <= 13) return await _htmlHandlerV13.UploadHtmlAsync(this, ct); if (version == 14) return await _htmlHandlerV14.UploadHtmlAsync(this, ct); + _logger.LogWarning("Crash report version is invalid: {CrashReportVersion}", version); return StatusCode(StatusCodes.Status500InternalServerError); } } \ No newline at end of file