You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "FreeAndNil (via GitHub)" <gi...@apache.org> on 2024/03/12 07:11:46 UTC

[PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests #119 (logging-log4net)

FreeAndNil opened a new pull request, #122:
URL: https://github.com/apache/logging-log4net/pull/122

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "fluffynuts (via GitHub)" <gi...@apache.org>.
fluffynuts commented on code in PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#discussion_r1521169980


##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -222,7 +220,7 @@ protected internal enum RollPoint
       TopOfMonth = 5
     }
 
-    #endregion Protected Enums
+    #endregion

Review Comment:
   You don't have to do this right now, but:
   I feel that regions are terrible for code quality and readability - they hide the code until the reader gets to it and give the false sense of "small code" - regions are a "code smell" that the code is potentially doing too much. At the very least, they're really annoying (for me at least) to work with - first thing I do when I come into a file with regions is remove them.



##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -705,7 +699,7 @@ private void DetermineCurSizeRollBackups()
       List<string> arrayFiles = GetExistingFiles(fullPath);
       InitializeRollBackups(fileName, arrayFiles);
 
-      LogLog.Debug(declaringType, "curSizeRollBackups starts at [" + m_curSizeRollBackups + "]");
+      LogLog.Debug(declaringType, "curSizeRollBackups starts at [" + CurrentSizeRollBackups + "]");

Review Comment:
   interpolated string please - if you're using Rider or ReSharper, you should be able to alt-enter on the `+` to convert to an interpolated string



##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -675,7 +669,7 @@ protected string GetNextOutputFileName(string fileName)
 
         if (CountDirection >= 0)
         {
-          fileName = CombinePath(fileName, "." + m_curSizeRollBackups);
+          fileName = CombinePath(fileName, "." + CurrentSizeRollBackups);

Review Comment:
   please use string interpolation rather (avoids a class of null-related bugs - even though I don't think a null-deref is a potential problem here, I feel like it's better to stick to a style so that the reader doesn't have to mentally check that the concatenation should always succeed), ie:
   ```csharp
     fileName = CombinePath(fileName, $".{CurrentSizeRollBackups}")
   ```



##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -715,21 +709,16 @@ private void DetermineCurSizeRollBackups()
     private string GetWildcardPatternForFile(string baseFileName)
     {
       if (PreserveLogFileNameExtension)
-      {
         return Path.GetFileNameWithoutExtension(baseFileName) + "*" + Path.GetExtension(baseFileName);
-      }
-      else
-      {
-        return baseFileName + '*';
-      }
+      return baseFileName + '*';
     }
 
     /// <summary>
     /// Builds a list of filenames for all files matching the base filename plus a file
     /// pattern.
     /// </summary>
-    // Internal for unit testing.
-    internal List<string> GetExistingFiles(string baseFilePath)
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public List<string> GetExistingFiles(string baseFilePath)

Review Comment:
   Just an idea (I don't have a problem here, just putting it out there)
   
   If we have members we'd rather not expose (because we can't actually encapsulate anything to be hidden without obfuscation - someone can always reflect inward), we can make them protected and then derive from the class in the test fixture and expose the protected method via a public method on the derived class. It's just another tool to use.



##########
src/log4net/Util/PatternStringConverters/UserNamePatternConverter.cs:
##########
@@ -43,16 +43,12 @@ internal sealed class UserNamePatternConverter : PatternConverter
     /// Write the current threads username to the output <paramref name="writer"/>.
     /// </para>
     /// </remarks>
-    protected internal override void Convert(TextWriter writer, object state)
+    public override void Convert(TextWriter writer, object state)
     {
       try
       {
-        System.Security.Principal.WindowsIdentity windowsIdentity = null;
-        windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
-        if (windowsIdentity != null && windowsIdentity.Name != null)
-        {
-          writer.Write(windowsIdentity.Name);
-        }
+        if (System.Security.Principal.WindowsIdentity.GetCurrent()?.Name is string name)

Review Comment:
   braces please



##########
src/log4net/Appender/EventLogAppender.cs:
##########
@@ -411,32 +411,21 @@ protected override void Append(LoggingEvent loggingEvent)
 
       short category = m_category;
       // Look for the Category property
-      object categoryPropertyObj = loggingEvent.LookupProperty("Category");
-      if (categoryPropertyObj != null)
+      if (loggingEvent.LookupProperty("Category") is object categoryPropertyObj)
       {
-        if (categoryPropertyObj is short)
-        {
-          category = (short)categoryPropertyObj;
-        }
+        if (categoryPropertyObj is short shortValue)
+          category = shortValue;

Review Comment:
   please use braces even for 1-liners - it avoids a class of bugs down the road.



##########
src/log4net/Appender/EventLogAppender.cs:
##########
@@ -494,25 +471,17 @@ protected override bool RequiresLayout
     /// <see cref="Level"/> this is a one way mapping. There is
     /// a loss of information during the conversion.
     /// </remarks>
-    protected internal virtual EventLogEntryType GetEntryType(Level level)
+    public virtual EventLogEntryType GetEntryType(Level level)

Review Comment:
   braces again - please use for all blocks
   
   i used to do "short ifs" many years ago, but I've seen the value in just using the braces consistently, everywhere.



##########
src/log4net/Layout/Pattern/PatternLayoutConverter.cs:
##########
@@ -94,17 +94,12 @@ public virtual bool IgnoresException
     /// </summary>
     /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
     /// <param name="state">The state object on which the pattern converter should be executed.</param>
-    protected internal override void Convert(TextWriter writer, object state)
+    public override void Convert(TextWriter writer, object state)
     {
-      LoggingEvent loggingEvent = state as LoggingEvent;
-      if (loggingEvent != null)
-      {
+      if (state is LoggingEvent loggingEvent)

Review Comment:
   braces please - if you're using Rider or ReSharper, you can alt-enter on the `if` to get options - when you find the one to apply braces style, look at the menu to the right and "apply in file" (i'm normally too cautious to apply on a larger scale, but this beats alt-entering everything in the file)



##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -589,8 +583,8 @@ protected virtual void AdjustFileBeforeAppend()
       }
       finally
       {
-          // if rolling should be locked, release the lock
-          m_mutexForRolling?.ReleaseMutex();
+        // if rolling should be locked, release the lock
+        m_mutexForRolling?.ReleaseMutex();

Review Comment:
   potential bug: should null out the mutex after this so that a subsequent run doesn't attempt to release an unbound mutex. Also, mutexes should be disposed. So I suggest:
   
   ```csharp
   finally
   {
     var mutex = m_mutexForRolling;
     m_mutexForRolling = null;
     mutex?.ReleaseMutex();
     mutex?.Dispose()
   }
   ```



##########
src/log4net/Util/PatternStringConverters/PropertyPatternConverter.cs:
##########
@@ -57,35 +57,24 @@ internal sealed class PropertyPatternConverter : PatternConverter
     /// then all the properties are written as key value pairs.
     /// </para>
     /// </remarks>
-    protected internal override void Convert(TextWriter writer, object state)
+    public override void Convert(TextWriter writer, object state)
     {
       CompositeProperties compositeProperties = new CompositeProperties();
 
-      PropertiesDictionary logicalThreadProperties = LogicalThreadContext.Properties.GetProperties(false);
-      if (logicalThreadProperties != null)
-      {
+      if (LogicalThreadContext.Properties.GetProperties(false) is PropertiesDictionary logicalThreadProperties)
         compositeProperties.Add(logicalThreadProperties);
-      }
 
-      PropertiesDictionary threadProperties = ThreadContext.Properties.GetProperties(false);
-      if (threadProperties != null)
-      {
+      if (ThreadContext.Properties.GetProperties(false) is PropertiesDictionary threadProperties)
         compositeProperties.Add(threadProperties);
-      }
 
-      // TODO: Add Repository Properties
       compositeProperties.Add(GlobalContext.Properties.GetReadOnlyProperties());
 
-      if (Option != null)
-      {
+      if (Option is not null)

Review Comment:
   braces please



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "FreeAndNil (via GitHub)" <gi...@apache.org>.
FreeAndNil commented on PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#issuecomment-1993988753

   > @FreeAndNil I was expanding nullable and strongly typed data structures in the code yesterday. Would it make sense for me to send it along incrementally with new PRs against the 111- branch? Or are you moving fast there and want to minimize interruption?
   
   @erikmav your work is very welcome. One remark: we need to have code coverage for the changed blocks. Most blocks are covered but some are not, so adding missing unit tests is needed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "FreeAndNil (via GitHub)" <gi...@apache.org>.
FreeAndNil commented on code in PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#discussion_r1521205893


##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -589,8 +583,8 @@ protected virtual void AdjustFileBeforeAppend()
       }
       finally
       {
-          // if rolling should be locked, release the lock
-          m_mutexForRolling?.ReleaseMutex();
+        // if rolling should be locked, release the lock
+        m_mutexForRolling?.ReleaseMutex();

Review Comment:
   I don't think disposing the mutex at this point is a good idea.
   The mutex is a class member and will be used again in every AdjustFileBeforeAppend() later.
   I've adjusted the finalizer instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "FreeAndNil (via GitHub)" <gi...@apache.org>.
FreeAndNil commented on code in PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#discussion_r1521192993


##########
src/log4net/Appender/EventLogAppender.cs:
##########
@@ -411,32 +411,21 @@ protected override void Append(LoggingEvent loggingEvent)
 
       short category = m_category;
       // Look for the Category property
-      object categoryPropertyObj = loggingEvent.LookupProperty("Category");
-      if (categoryPropertyObj != null)
+      if (loggingEvent.LookupProperty("Category") is object categoryPropertyObj)
       {
-        if (categoryPropertyObj is short)
-        {
-          category = (short)categoryPropertyObj;
-        }
+        if (categoryPropertyObj is short shortValue)
+          category = shortValue;

Review Comment:
   check



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "FreeAndNil (via GitHub)" <gi...@apache.org>.
FreeAndNil merged PR #122:
URL: https://github.com/apache/logging-log4net/pull/122


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "FreeAndNil (via GitHub)" <gi...@apache.org>.
FreeAndNil commented on code in PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#discussion_r1521246705


##########
src/log4net/Appender/RollingFileAppender.cs:
##########
@@ -222,7 +220,7 @@ protected internal enum RollPoint
       TopOfMonth = 5
     }
 
-    #endregion Protected Enums
+    #endregion

Review Comment:
   I've removed all regions and moved the date classes to an own file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable nullable for FileAppender, RollingFileAppender, and unit tests (logging-log4net)

Posted by "erikmav (via GitHub)" <gi...@apache.org>.
erikmav commented on PR #122:
URL: https://github.com/apache/logging-log4net/pull/122#issuecomment-1992061154

   @FreeAndNil I was expanding nullable and strongly typed data structures in the code yesterday. Would it make sense for me to send it along incrementally with new PRs against the 111- branch? Or are you moving fast there and want to minimize interruption?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org