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

TraceSourceLogger now takes exception into account(adds it to the log… #42571

Conversation

rizi
Copy link
Contributor

@rizi rizi commented Sep 22, 2020

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

@ghost
Copy link

ghost commented Sep 22, 2020

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Copy link

@KalleOlaviNiemitalo KalleOlaviNiemitalo left a 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.
@dnfadmin
Copy link

dnfadmin commented Sep 22, 2020

CLA assistant check
All CLA requirements met.

@rizi
Copy link
Contributor Author

rizi commented Sep 22, 2020

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.

@KalleOlaviNiemitalo I fixed the logical issue, that the exception would have been added twice to the log message when the formatter is null.
I also removed the "string formatting".

Regarding tests:
I already asked in the corresponding issue where tests for the LogProvider can be found, I think it would not make sense to add a test for the TraceSourceLogProvider, instead there should already be tests for the TraceSourceLogger, and if there are tests I will of course add one (but I can't find the tests/I'm not sure if there any at all).

@KalleOlaviNiemitalo
Copy link

@rizi, there is TraceSourceLoggerTest.cs in the same directory. TraceSourceLoggerProviderTest.BufferedConsoleTraceListener would be useful for the new test, though.

@rizi
Copy link
Contributor Author

rizi 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:

  • build.cmd clr+libs -rc Release (finished with 0 warnings and 0 errors)
  • build.cmd -vs Microsoft.Extensions.Logging

As soon as VS 2019 (16.8 preview 3) opens I can't compile the solution or run tests:
Error:
1>------ Build started: Project: TestUtilities, Configuration: Debug Any CPU ------
Error occurred while restoring NuGet packages: Invalid restore input. Duplicate frameworks found: 'net5.0, net461, net5.0'. Input files: C:\Dev\Git\runtime\src\libraries\Microsoft.Extensions.Logging\tests\Common\Microsoft.Extensions.Logging.Tests.csproj.
1>CSC : error CS0006: Metadata file 'C:\Dev\Git\runtime\artifacts\bin\System.Runtime.CompilerServices.Unsafe\net45-Debug\System.Runtime.CompilerServices.Unsafe.dll' could not be found
1>Done building project "TestUtilities.csproj" -- FAILED.
1>TestUtilities -> C:\Dev\Git\runtime\artifacts\bin\TestUtilities\net5.0-Debug\TestUtilities.dll
2>------ Build started: Project: Microsoft.Extensions.Logging.Tests, Configuration: Debug Any CPU ------

It seems that VS(nuget restore) are confused with $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent).

As there no way make Visual Studio work?

@rizi
Copy link
Contributor Author

rizi 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:

  • build.cmd clr+libs -rc Release (finished with 0 warnings and 0 errors)
  • build.cmd -vs Microsoft.Extensions.Logging

As soon as VS 2019 (16.8 preview 3) opens I can't compile the solution or run tests:
Error:
1>------ Build started: Project: TestUtilities, Configuration: Debug Any CPU ------
Error occurred while restoring NuGet packages: Invalid restore input. Duplicate frameworks found: 'net5.0, net461, net5.0'. Input files: C:\Dev\Git\runtime\src\libraries\Microsoft.Extensions.Logging\tests\Common\Microsoft.Extensions.Logging.Tests.csproj.
1>CSC : error CS0006: Metadata file 'C:\Dev\Git\runtime\artifacts\bin\System.Runtime.CompilerServices.Unsafe\net45-Debug\System.Runtime.CompilerServices.Unsafe.dll' could not be found
1>Done building project "TestUtilities.csproj" -- FAILED.
1>TestUtilities -> C:\Dev\Git\runtime\artifacts\bin\TestUtilities\net5.0-Debug\TestUtilities.dll
2>------ Build started: Project: Microsoft.Extensions.Logging.Tests, Configuration: Debug Any CPU ------

It seems that VS(nuget restore) are confused with $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent).

As there no way make Visual Studio work?

TraceSourceLogger is an internal class, therefore I can't create an instance with new TraceSourceLogger(...);
Using

 var factory = TestLoggerBuilder.Create(builder => builder.AddTraceSource(testSwitch));
 var logger = factory.CreateLogger("Test");

@KalleOlaviNiemitalo
Copy link

@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
Copy link
Contributor Author

rizi 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.

Thx for the info, I wasn't sure if that's ok.
Is a mock framework (moq, fakeiteasy,..) for the dotnet/runtime in use? Or how do you assert calls that are made against e.q an interface for a object/class under test?

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
Copy link

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
Copy link

Is a mock framework (moq, fakeiteasy,..) for the dotnet/runtime in use?

The Microsoft.Extensions.Logging.Tests project uses Moq:

var factory = new Mock<ILoggerFactory>();
factory.Setup(f => f.CreateLogger(
It.IsAny<string>()))
.Returns(new Mock<ILogger>().Object);

@tarekgh
Copy link
Member

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.

Copy link
Member

@maryamariyan maryamariyan left a 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.

Copy link
Member

@maryamariyan maryamariyan left a 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

@maryamariyan maryamariyan merged commit 5cb5ae9 into dotnet:master Sep 28, 2020
@maryamariyan
Copy link
Member

maryamariyan commented Sep 28, 2020

Just applied two nit commits on your PR. (remove extra new lines or whitespaces) and merging.

Thanks @rizi for your PR submission.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants