Skip to content

Commit

Permalink
CSharpier format
Browse files Browse the repository at this point in the history
  • Loading branch information
gstraccini[bot] committed Aug 8, 2024
1 parent e1cf3df commit 8116437
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 46 deletions.
6 changes: 5 additions & 1 deletion Src/CrispyWaffle/Log/Adapters/ILogAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ void Debug<T>(
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <remarks>Requires LogLevel.ERROR flag.</remarks>
[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);

/// <summary>
Expand Down
98 changes: 67 additions & 31 deletions Src/CrispyWaffle/Log/Adapters/RollingTextFileLogAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()}."
);
}

/// <inheritdoc />
Expand All @@ -110,7 +120,11 @@ public void Debug<T>(
return;
}

WriteToFile(LogLevel.Debug, (string)content.GetCustomSerializer(customFormat), fileName: identifier);
WriteToFile(
LogLevel.Debug,
(string)content.GetCustomSerializer(customFormat),
fileName: identifier
);
}

/// <inheritdoc />
Expand Down Expand Up @@ -234,12 +248,22 @@ public void CategorizedDebug<T>(
{
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
);
}

/// <inheritdoc />
Expand Down Expand Up @@ -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()

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove these redundant parentheses. Warning

Remove these redundant parentheses.
{
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)
{
Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion Src/CrispyWaffle/Log/Providers/TextFileLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public void Debug(string category, string content, string identifier)
/// <param name="content">The object to be serialized.</param>
/// <param name="identifier">The filename/attachment identifier (file name or key).</param>
/// <param name="customFormat">(Optional) the custom serializer format.</param>
[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<T>(
string category,
T content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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)

Check failure on line 38 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'

Check failure on line 38 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'
.ToList();

foreach (var file in files)
{
Expand All @@ -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";

Expand All @@ -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)

Check failure on line 73 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'

Check failure on line 73 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'
.ToList();

Assert.True(files.Count == 10);

Check failure on line 76 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Operator '==' cannot be applied to operands of type 'method group' and 'int'

Check failure on line 76 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Operator '==' cannot be applied to operands of type 'method group' and 'int'

Expand All @@ -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++)
Expand All @@ -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)

Check failure on line 109 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'
.ToList();

Assert.True(files.Count == 50);

Check failure on line 112 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Operator '==' cannot be applied to operands of type 'method group' and 'int'

Expand All @@ -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)
Expand All @@ -121,8 +151,10 @@ public void MultiThreadedTest()

var messageSet = new HashSet<string>();
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)

Check failure on line 156 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The call is ambiguous between the following methods or properties: 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>)' and 'Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, int, bool>)'
.ToList();

Assert.True(files.Count == 4);

Check failure on line 159 in Tests/CrispyWaffle.Tests/Serialization/RollingTextFileLogAdapterTests.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Operator '==' cannot be applied to operands of type 'method group' and 'int'

Expand Down

0 comments on commit 8116437

Please sign in to comment.