Uh oh!
There was an error while loading. Please reload this page.
TraceSourceLogger now takes exception into account(adds it to the log… - #42571
Conversation
… message) even if formatter is not null
ghost
commented
Sep 22, 2020
Tagging subscribers to this area: @maryamariyan |
There was a problem hiding this comment.
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: |
KalleOlaviNiemitalo
commented
Sep 22, 2020
@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? |
rizi
commented
Sep 22, 2020
TraceSourceLogger is an internal class, therefore I can't create an instance with new TraceSourceLogger(...); |
KalleOlaviNiemitalo
commented
Sep 22, 2020
@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. |
rizi
commented
Sep 22, 2020
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 |
KalleOlaviNiemitalo
commented
Sep 22, 2020
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. |
KalleOlaviNiemitalo
commented
Sep 22, 2020
The Microsoft.Extensions.Logging.Tests project uses Moq: |
Uh oh!
There was an error while loading. Please reload this page.
tarekgh
commented
Sep 22, 2020
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. |
Uh oh!
There was an error while loading. Please reload this page.
maryamariyan
left a comment
There was a problem hiding this comment.
Thanks for the PR @rizi. I have some nit comments and recommendations on exception delimiter portion of code.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…ilar to SystemdConsoleFormatter.
maryamariyan
left a comment
There was a problem hiding this comment.
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