diff --git a/src/Umbraco.Core/Logging/Viewer/JsonLogViewer.cs b/src/Umbraco.Core/Logging/Viewer/JsonLogViewer.cs index ea2c8aa49f..f7010c8815 100644 --- a/src/Umbraco.Core/Logging/Viewer/JsonLogViewer.cs +++ b/src/Umbraco.Core/Logging/Viewer/JsonLogViewer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using Newtonsoft.Json; using Serilog.Events; using Serilog.Formatting.Compact.Reader; @@ -10,13 +11,15 @@ namespace Umbraco.Core.Logging.Viewer internal class JsonLogViewer : LogViewerSourceBase { private readonly string _logsPath; + private readonly ILogger _logger; - public JsonLogViewer(string logsPath = "", string searchPath = "") : base(searchPath) + public JsonLogViewer(ILogger logger, string logsPath = "", string searchPath = "") : base(searchPath) { if (string.IsNullOrEmpty(logsPath)) logsPath = $@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\"; _logsPath = logsPath; + _logger = logger; } private const int FileSizeCap = 100; @@ -77,8 +80,14 @@ namespace Umbraco.Core.Logging.Viewer using (var stream = new StreamReader(fs)) { var reader = new LogEventReader(stream); - while (reader.TryRead(out var evt)) + while (TryRead(reader, out var evt)) { + //We may get a null if log line is malformed + if (evt == null) + { + continue; + } + if (count > skip + take) { break; @@ -105,5 +114,21 @@ namespace Umbraco.Core.Logging.Viewer return logs; } + private bool TryRead(LogEventReader reader, out LogEvent evt) + { + try + { + return reader.TryRead(out evt); + } + catch (JsonReaderException ex) + { + // As we are reading/streaming one line at a time in the JSON file + // Thus we can not rpeort the line number, as it will always be 1 + _logger.Error(ex, "Unable to parse a line in the JSON log file"); + + evt = null; + return true; + } + } } } diff --git a/src/Umbraco.Core/Logging/Viewer/LogViewerComposer.cs b/src/Umbraco.Core/Logging/Viewer/LogViewerComposer.cs index 651e3de06e..8eb835b4d9 100644 --- a/src/Umbraco.Core/Logging/Viewer/LogViewerComposer.cs +++ b/src/Umbraco.Core/Logging/Viewer/LogViewerComposer.cs @@ -9,7 +9,7 @@ namespace Umbraco.Core.Logging.Viewer { public void Compose(Composition composition) { - composition.SetLogViewer(_ => new JsonLogViewer()); + composition.SetLogViewer(_ => new JsonLogViewer(composition.Logger)); } } } diff --git a/src/Umbraco.Tests/Logging/LogviewerTests.cs b/src/Umbraco.Tests/Logging/LogviewerTests.cs index 75e2c66a61..35981f5368 100644 --- a/src/Umbraco.Tests/Logging/LogviewerTests.cs +++ b/src/Umbraco.Tests/Logging/LogviewerTests.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using Moq; +using NUnit.Framework; using System; using System.IO; using System.Linq; @@ -47,7 +48,8 @@ namespace Umbraco.Tests.Logging File.Copy(exampleLogfilePath, _newLogfilePath, true); File.Copy(exampleSearchfilePath, _newSearchfilePath, true); - _logViewer = new JsonLogViewer(logsPath: _newLogfileDirPath, searchPath: _newSearchfilePath); + var logger = Mock.Of(); + _logViewer = new JsonLogViewer(logger, logsPath: _newLogfileDirPath, searchPath: _newSearchfilePath); } [OneTimeTearDown]