-
Notifications
You must be signed in to change notification settings - Fork 105
Logging scope when using abstractions v3.0.0 #158
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
Comments
Hi Arthur! Thanks for the report. This looks at first glance like it may be a project configuration problem, as far as I'm aware, the code in this repository doesn't use or reference Do you see any package downgrade warnings on build? Are you configuring this with |
The soulution in which this problem occurs currently consists of a library project and a test project. All tests classes inherit from a base class that contains a dependency injection container where logging is set up like this: namespace Test
{
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using ILogger = Microsoft.Extensions.Logging.ILogger;
/// <summary>
/// Base class for tests.
/// </summary>
public abstract class BaseTest
{
/// <summary>
/// Initializes a new instance of the <see cref="BaseTest"/> class.
/// </summary>
/// <param name="serviceRegistrations">Service registrations.</param>
protected BaseTest(Action<IServiceCollection> serviceRegistrations = default)
{
// Setup Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Debug()
.CreateLogger();
// Bootstrap dependency injection.
var collection = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder
.AddSerilog(Log.Logger, true))
.AddSingleton(sp => sp.GetService<ILoggerFactory>().CreateLogger("TEST"));
serviceRegistrations?.Invoke(collection);
this.ServiceProvider = collection.BuildServiceProvider();
}
/// <summary>
/// Gets the service provider.
/// </summary>
protected ServiceProvider ServiceProvider { get; }
/// <summary>
/// Gets the logger.
/// </summary>
protected ILogger Logger => this.ServiceProvider.GetService<ILogger>();
}
} The test project has the following logging-related dependencies: <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.1" />
</ItemGroup> The actual library only uses <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
</ItemGroup> And the library does use the ILogger.BeginScope(string) method to create scopes. Whenever the dependency in the library is set to version 2.2.0, everything works fine: <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
However, if the dependency in the library is set to version 3.0.0, all hell breaks loose: <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
On build / restore, there are no package warnings for any of these situations. |
After all, it could be a problem with |
Yes, seems like in this setup, you end up with a mix of 2.x and 3.x Microsoft.Extensions.Logging* packages, when these two have internal dependencies set up between them. Adding a Microsoft.Extensions.Logging 3.0 dependency to the test project should fix it. Serilog.Extensions.Logging will run successfully on either version - it sticks to the public API surface and the maintainers haven't broken it between versions yet. But the app will still need to be configured to specify matching versions of these packages for them to work together (you'd get the same error using mixed versions, even without Serilog in the mix). |
@nblumhardt Adding an explicit dependency on |
They (.NET Core / ASP.NET Core teams) have such a versioning policy. When new versions of the platform are released, all packages are recompiled and receive the same version (now 3.0.0). This is on the one hand even more convenient. |
Think the suggestion is that you need to add v3 of Microsoft.Extensions.Logging to the test project rather than the Abstractions package, otherwise you might end up with v2.2 of Microsoft.Extensions.Logging and v3.0 of Microsoft.Extensions.logging.Abstractions, which aren't compatible? |
Adding a dependency on It might still be useful to upgrade the dependency of this library on Microsoft.Extensions.Logging to the latest version: serilog-extensions-logging/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj Line 32 in 9f5a6a1
|
Great, thanks for the follow up @ArthurHNL, and the help, @Numpsy (edit: oh, and @sungam3r!) 👍 Currently the package here can serve the most users by keeping 2.x compatibility; we might do as you suggest and bump the dependency version in the future, but for now I think we should maintain the status quo while 2.x is most popular out in the wild. Thanks again for the input! |
On .NET Core 2.2 and
Serilog.Extensions.Logging
version3.0.1
:ILogger.BeginScope
en pair withMicrosoft.Logging.Abstractions
version2.2.0
, everything works fine.ILogger.BeginScope
en pair withMicrosoft.Logging.Abstractions
version3.0.0
, the following exception is thrown:Please fix this, this makes using Serilog with libraries that depend on
Microsoft.Logging.Abstractions
version3.0.0
very difficult.The text was updated successfully, but these errors were encountered: