diff --git a/Src/CrispyWaffle/Log/Adapters/ILogAdapter.cs b/Src/CrispyWaffle/Log/Adapters/ILogAdapter.cs index ab42fab1..9d7f1b37 100644 --- a/Src/CrispyWaffle/Log/Adapters/ILogAdapter.cs +++ b/Src/CrispyWaffle/Log/Adapters/ILogAdapter.cs @@ -85,7 +85,11 @@ void Debug( /// /// The message to be logged. /// Requires LogLevel.ERROR flag. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "Design decision.")] + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Naming", + "CA1716:Identifiers should not match keywords", + Justification = "Design decision." + )] void Error(string message); /// diff --git a/Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs b/Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs index 4b847e15..83a28a15 100644 --- a/Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs +++ b/Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs @@ -48,19 +48,27 @@ public RollingTextFileLogAdapter( { checked { - _maxFileSize = maxFileSize.size * (long)maxFileSize.unit > MaxFileSizeAllowed || maxFileSize.size * (long)maxFileSize.unit < MinFileSizeAllowed - ? throw new ArgumentOutOfRangeException($"Max file size cannot be greater than {MaxFileSizeAllowed}!") - : maxFileSize.size * (long)maxFileSize.unit; + _maxFileSize = + maxFileSize.size * (long)maxFileSize.unit > MaxFileSizeAllowed + || maxFileSize.size * (long)maxFileSize.unit < MinFileSizeAllowed + ? throw new ArgumentOutOfRangeException( + $"Max file size cannot be greater than {MaxFileSizeAllowed}!" + ) + : maxFileSize.size * (long)maxFileSize.unit; } } catch { - throw new ArgumentOutOfRangeException($"Max file size cannot be greater than {MaxFileSizeAllowed}!"); + throw new ArgumentOutOfRangeException( + $"Max file size cannot be greater than {MaxFileSizeAllowed}!" + ); } if (maxMessageCount < 1) { - throw new ArgumentOutOfRangeException($"Max message count cannot be less than 1. Current value: ${maxMessageCount}!"); + throw new ArgumentOutOfRangeException( + $"Max message count cannot be less than 1. Current value: ${maxMessageCount}!" + ); } _maxMessageCount = maxMessageCount; @@ -92,7 +100,9 @@ public RollingTextFileLogAdapter( public void SetLevel(LogLevel level) { _level = level; - Warning($"Level updated from {_level.GetHumanReadableValue()} to {level.GetHumanReadableValue()}."); + Warning( + $"Level updated from {_level.GetHumanReadableValue()} to {level.GetHumanReadableValue()}." + ); } /// @@ -110,7 +120,11 @@ public void Debug( return; } - WriteToFile(LogLevel.Debug, (string)content.GetCustomSerializer(customFormat), fileName: identifier); + WriteToFile( + LogLevel.Debug, + (string)content.GetCustomSerializer(customFormat), + fileName: identifier + ); } /// @@ -234,12 +248,22 @@ public void CategorizedDebug( { if (customFormat == SerializerFormat.None) { - WriteToFile(LogLevel.Debug, (string)content.GetSerializer(), fileName: identifier, category: category); + WriteToFile( + LogLevel.Debug, + (string)content.GetSerializer(), + fileName: identifier, + category: category + ); return; } - WriteToFile(LogLevel.Debug, (string)content.GetCustomSerializer(customFormat), fileName: identifier, category: category); + WriteToFile( + LogLevel.Debug, + (string)content.GetCustomSerializer(customFormat), + fileName: identifier, + category: category + ); } /// @@ -273,31 +297,37 @@ private void WriteToFile(LogLevel level, Exception exception, string category = WriteToFile(level, exMessage.ToString(), category); } - private void WriteToFile(LogLevel level, string content, string fileName = default, string category = default) + private void WriteToFile( + LogLevel level, + string content, + string fileName = default, + string category = default + ) { if (!_level.HasFlag(level)) { return; } - var message = (string)new LogMessage() - { - Application = EnvironmentHelper.ApplicationName, - Category = category == default ? _defaultCategory : category, - Date = DateTime.UtcNow, - Hostname = EnvironmentHelper.Host, - Id = Guid.NewGuid().ToString(), - IpAddress = EnvironmentHelper.IpAddress, - IpAddressRemote = EnvironmentHelper.IpAddressExternal, - Level = level.GetHumanReadableValue(), - Message = content, - MessageIdentifier = fileName == default ? currentFileName : fileName, - Operation = EnvironmentHelper.Operation, - ProcessId = EnvironmentHelper.ProcessId, - UserAgent = EnvironmentHelper.UserAgent, - ThreadId = Environment.CurrentManagedThreadId, - ThreadName = Thread.CurrentThread.Name - }.GetSerializer(); + var message = (string) + new LogMessage() + { + Application = EnvironmentHelper.ApplicationName, + Category = category == default ? _defaultCategory : category, + Date = DateTime.UtcNow, + Hostname = EnvironmentHelper.Host, + Id = Guid.NewGuid().ToString(), + IpAddress = EnvironmentHelper.IpAddress, + IpAddressRemote = EnvironmentHelper.IpAddressExternal, + Level = level.GetHumanReadableValue(), + Message = content, + MessageIdentifier = fileName == default ? currentFileName : fileName, + Operation = EnvironmentHelper.Operation, + ProcessId = EnvironmentHelper.ProcessId, + UserAgent = EnvironmentHelper.UserAgent, + ThreadId = Environment.CurrentManagedThreadId, + ThreadName = Thread.CurrentThread.Name + }.GetSerializer(); lock (_syncRoot) { @@ -310,12 +340,18 @@ private void WriteToFile(LogLevel level, string content, string fileName = defau if (fileMessageCount > 0) { - currentFileStream.SetLength(currentFileStream.Length - 1 - Environment.NewLine.Length); - messageBytes = UTF8Encoding.UTF8.GetBytes($",{Environment.NewLine}{message}{Environment.NewLine}]"); + currentFileStream.SetLength( + currentFileStream.Length - 1 - Environment.NewLine.Length + ); + messageBytes = UTF8Encoding.UTF8.GetBytes( + $",{Environment.NewLine}{message}{Environment.NewLine}]" + ); } else { - messageBytes = UTF8Encoding.UTF8.GetBytes($"[{Environment.NewLine}{message}{Environment.NewLine}]"); + messageBytes = UTF8Encoding.UTF8.GetBytes( + $"[{Environment.NewLine}{message}{Environment.NewLine}]" + ); } currentFileStream.Write(messageBytes, 0, messageBytes.Length); diff --git a/Src/CrispyWaffle/Log/Providers/TextFileLogProvider.cs b/Src/CrispyWaffle/Log/Providers/TextFileLogProvider.cs index e8afffb4..96f233ef 100644 --- a/Src/CrispyWaffle/Log/Providers/TextFileLogProvider.cs +++ b/Src/CrispyWaffle/Log/Providers/TextFileLogProvider.cs @@ -132,7 +132,11 @@ public void Debug(string category, string content, string identifier) /// The object to be serialized. /// The filename/attachment identifier (file name or key). /// (Optional) the custom serializer format. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Code Smell", "S1006:Method overrides should not change parameter defaults", Justification = "Needed here.")] + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Critical Code Smell", + "S1006:Method overrides should not change parameter defaults", + Justification = "Needed here." + )] public void Debug( string category, T content, diff --git a/Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs b/Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs index e2970c0e..67d26d4e 100644 --- a/Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs +++ b/Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs @@ -17,7 +17,12 @@ public class RollingTextFileLogAdapterTests public void BasicSaveLogsToFileTest() { var fileNameSeed = "basicLogs"; - var adapter = new RollingTextFileLogAdapter(AppDomain.CurrentDomain.BaseDirectory, fileNameSeed, 100, (Unit.KByte, 10)); + var adapter = new RollingTextFileLogAdapter( + AppDomain.CurrentDomain.BaseDirectory, + fileNameSeed, + 100, + (Unit.KByte, 10) + ); var message = new string(Enumerable.Repeat('0', 1000).ToArray()); for (int i = 0; i < 100; i++) @@ -28,8 +33,10 @@ public void BasicSaveLogsToFileTest() adapter.Dispose(); var regexFileName = new Regex(GetFileNameRegex(fileNameSeed)); - var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") - .Where(regexFileName.IsMatch).ToList(); + var files = Directory + .GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") + .Where(regexFileName.IsMatch) + .ToList(); foreach (var file in files) { @@ -44,7 +51,12 @@ public void BasicSaveLogsToFileTest() public void MaxMessageConstraintTest() { var fileNameSeed = "maxMessageLogs"; - var adapter = new RollingTextFileLogAdapter(AppDomain.CurrentDomain.BaseDirectory, fileNameSeed, 100, (Unit.MByte, 1)); + var adapter = new RollingTextFileLogAdapter( + AppDomain.CurrentDomain.BaseDirectory, + fileNameSeed, + 100, + (Unit.MByte, 1) + ); adapter.SetLevel(Log.LogLevel.Debug); var message = "Message"; @@ -56,8 +68,10 @@ public void MaxMessageConstraintTest() adapter.Dispose(); var regexFileName = new Regex(GetFileNameRegex(fileNameSeed)); - var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") - .Where(regexFileName.IsMatch).ToList(); + var files = Directory + .GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") + .Where(regexFileName.IsMatch) + .ToList(); Assert.True(files.Count == 10); @@ -74,7 +88,12 @@ public void MaxMessageConstraintTest() public void MaxSizeConstraintTest() { var fileNameSeed = "maxSizeLogs"; - var adapter = new RollingTextFileLogAdapter(AppDomain.CurrentDomain.BaseDirectory, fileNameSeed, 100000, (Unit.KByte, 1)); + var adapter = new RollingTextFileLogAdapter( + AppDomain.CurrentDomain.BaseDirectory, + fileNameSeed, + 100000, + (Unit.KByte, 1) + ); var message = new string(Enumerable.Repeat('0', 990).ToArray()); for (int i = 0; i < 50; i++) @@ -85,8 +104,10 @@ public void MaxSizeConstraintTest() adapter.Dispose(); var regexFileName = new Regex(GetFileNameRegex(fileNameSeed)); - var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") - .Where(regexFileName.IsMatch).ToList(); + var files = Directory + .GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") + .Where(regexFileName.IsMatch) + .ToList(); Assert.True(files.Count == 50); @@ -100,11 +121,20 @@ public void MaxSizeConstraintTest() } [Fact] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "xUnit1031:Do not use blocking task operations in test method", Justification = "Testing.")] + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "Usage", + "xUnit1031:Do not use blocking task operations in test method", + Justification = "Testing." + )] public void MultiThreadedTest() { var fileNameSeed = "multiThreadedLogs"; - var adapter = new RollingTextFileLogAdapter(AppDomain.CurrentDomain.BaseDirectory, fileNameSeed, 10, (Unit.MByte, 100)); + var adapter = new RollingTextFileLogAdapter( + AppDomain.CurrentDomain.BaseDirectory, + fileNameSeed, + 10, + (Unit.MByte, 100) + ); var tasks = new Task[40]; for (int i = 0; i < 40; i += 4) @@ -121,8 +151,10 @@ public void MultiThreadedTest() var messageSet = new HashSet(); var regexFileName = new Regex(GetFileNameRegex(fileNameSeed)); - var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") - .Where(regexFileName.IsMatch).ToList(); + var files = Directory + .GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.json") + .Where(regexFileName.IsMatch) + .ToList(); Assert.True(files.Count == 4);