Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor InternalLogger class #2314

Merged
merged 3 commits into from
Oct 14, 2017
Merged

Conversation

ie-zero
Copy link
Contributor

@ie-zero ie-zero commented Oct 8, 2017

Refactor InternalLogger class to improve code readabiity and create further opportunities to improve the code.

There are no functional changes or changes to the public interface.

The method Write() in InternalLogger class split into smaller methods

  • FormatMessage()
  • WriteToLogFile()
  • WriteToTextWriter()
  • WriteToConsole()
  • WriteToErrorConsole()

LoggingEnabled() method renamed to IsLoggingEnabled() in alignment to IsSeriousException(). It also convey better to the reader the purpose of the method.

Refactor InternalLogger class to improve code readabiity and create further opportunities to improve the code.

There are no functional changes or changes to the public interface.

The method Write() in InternalLogger class split into smaller methods
* FormatMessage()
* WriteToLogFile()
* WriteToTextWriter()
* WriteToConsole()
* WriteToErrorConsole()

LoggingEnabled() method renamed to IsLoggingEnabled() in alignment to IsSeriousException(). It also convey better to the reader the purpose of the method.
@codecov
Copy link

codecov bot commented Oct 8, 2017

Codecov Report

Merging #2314 into master will decrease coverage by <1%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #2314    +/-   ##
=======================================
- Coverage      82%     82%   -<1%     
=======================================
  Files         317     317            
  Lines       22716   22736    +20     
  Branches     2784    2782     -2     
=======================================
+ Hits        18618   18624     +6     
- Misses       3396    3410    +14     
  Partials      702     702

Copy link
Member

@304NotModified 304NotModified left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, added some small comments!

var formattedMessage =
(args == null) ? message : string.Format(CultureInfo.InvariantCulture, message, args);

var builder = new StringBuilder(formattedMessage.Length + TimeStampFormat.Length + 8);
Copy link
Member

@304NotModified 304NotModified Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also small change here I see, maybe also check IncludeTimestamp?
(and exception?)

@@ -367,6 +344,64 @@ private static bool LoggingEnabled(LogLevel logLevel)
LogWriter != null;
}

private static void WriteToLogFile(string message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some docs, most important is here that logging will be skipped if not enabled. (multiple times)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do

* Document WriteTo... methods in InternalLogger class
* Reserve large enough buffer space in memory so the StringBuilder can hold the formatted output message without resizing.
@@ -288,7 +288,7 @@ private static string FormatMessage([CanBeNull]Exception ex, LogLevel level, str
var formattedMessage =
(args == null) ? message : string.Format(CultureInfo.InvariantCulture, message, args);

var builder = new StringBuilder(formattedMessage.Length + TimeStampFormat.Length + 8);
var builder = new StringBuilder(formattedMessage.Length + TimeStampFormat.Length + ex?.ToString()?.Length ?? 0 + 25);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 + 25? Should be 8+25?

or 0 + 0+ 0+ 0+ 0+ 0+ 0+ 0 + 25 :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formattedMessage.Length and TimeStampFormat.Length (constant length) should never be 0. ex?.ToString()?.Length ?? 0 might be zero and the rest of white-spaces the literal 'Exception' and the LogLevel as a string sum up, if I recall correctly, to 23 characters.

To be honest, I am not certain if pre-calculating the length of the expected string in such detail for optimisation is positive.

What do you think?

Copy link
Member

@304NotModified 304NotModified Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, pre-optimized could be done later.

I think i was reading it as

ex?.ToString()?.Length ?? (0 + 25)

are you sure it's

 (ex?.ToString()?.Length ?? 0) + 25

I have to lookup the priority of ??

var formattedMessage =
(args == null) ? message : string.Format(CultureInfo.InvariantCulture, message, args);

var builder = new StringBuilder(formattedMessage.Length + TimeStampFormat.Length + ex?.ToString()?.Length ?? 0 + 25);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resharper says this is ex?.ToString()?.Length ?? (0 + 25)

So this is wrong ;)

@304NotModified 304NotModified merged commit a27b964 into NLog:master Oct 14, 2017
@304NotModified 304NotModified modified the milestones: 4.5, 4.5 beta 7 Oct 14, 2017
@304NotModified
Copy link
Member

Merged. Thanks!

@snakefoot snakefoot modified the milestones: 4.5 beta 7, 4.5 Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants