-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
TraceSourceLogger now takes exception into account(adds it to the log… #42571
TraceSourceLogger now takes exception into account(adds it to the log… #42571
Conversation
… message) even if formatter is not null
Tagging subscribers to this area: @maryamariyan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this appends the exception to the message twice if formatter == null
. Once would be enough.
The string.Format
and CultureInfo.InvariantCulture
references may be unnecessary; string concatenation could suffice. EventLogLogger does not use those when it appends an Exception to a StringBuilder. Exception classes typically do not implement IFormattable
and thus cannot receive an IFormatProvider
as a parameter; they just use the ambient cultures. On the other hand, exceptions should be rare, and exception-logging code might then not be performance-sensitive.
Should perhaps add a test in TraceSourceLoggerProviderTest.cs, to verify that Exception.Message occurs in the trace message exactly once. That file has #if NETFRAMEWORK
though, so I am not sure the test class is even compiled for .NET 5.
…he log message when the formatter would have been null.
@KalleOlaviNiemitalo I fixed the logical issue, that the exception would have been added twice to the log message when the formatter is null. Regarding tests: |
@rizi, there is TraceSourceLoggerTest.cs in the same directory. TraceSourceLoggerProviderTest.BufferedConsoleTraceListener would be useful for the new test, though. |
Thx for clarifying, I will add tests in the TraceSourceLoggerTest.cs file. Can someone please help me to compile the Microsoft.Extensions.Logging.sln Here is what I did so far:
As soon as VS 2019 (16.8 preview 3) opens I can't compile the solution or run tests: It seems that VS(nuget restore) are confused with As there no way make Visual Studio work? |
TraceSourceLogger is an internal class, therefore I can't create an instance with new TraceSourceLogger(...);
|
@rizi, Microsoft.Extensions.Caching.Memory has InternalsVisibleToAttribute to let tests access the internals. I guess you can do the same in Microsoft.Extensions.Logging.TraceSource if you need to. Perhaps that would require SkipUseReferenceAssembly (described in #35606 (comment)) as well, because the reference assembly would not have the InternalsVisibleToAttribute. |
Thx for the info, I wasn't sure if that's ok. Any idea how I can make the solution work in visual studio, it's really cumbersome to work with vs code and the command line if you are used to have a feature rich ide. Br |
To be clear, I don't have any authority in this project. But if it is OK to use InternalsVisibleToAttribute for tests in Microsoft.Extensions.Caching.Memory, I think it will be OK to use that elsewhere as well. |
The Microsoft.Extensions.Logging.Tests project uses Moq: Line 24 in f4a7e3d
runtime/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerFactoryExtensionsTest.cs Lines 19 to 22 in f4a7e3d
|
src/libraries/Microsoft.Extensions.Logging.TraceSource/src/TraceSourceLogger.cs
Outdated
Show resolved
Hide resolved
I think for testing, we can just use the public APIs which will exercise this internal code and get the expected results. I am not sure why we need to test the internal classes? if it is really necessary, I would say use reflection to do so. |
src/libraries/Microsoft.Extensions.Logging/tests/Common/TraceSourceLoggerTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @rizi. I have some nit comments and recommendations on exception delimiter portion of code.
src/libraries/Microsoft.Extensions.Logging/tests/Common/TraceSourceLoggerTest.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Logging.TraceSource/src/TraceSourceLogger.cs
Show resolved
Hide resolved
…ilar to SystemdConsoleFormatter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from nit comments, LGTM.
Thanks @rizi
Just applied two nit commits on your PR. (remove extra new lines or whitespaces) and merging. Thanks @rizi for your PR submission. |
TraceSourceLogger now takes the exception into account even if the formatter is not null, therefore the exception will be added to the log message.
This pull request fixes #42341
Please let me know if there is a better way to link the PR to the issue.
Br