adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
148 stars 22 forks source link

Package Version Conflict #19

Closed woutervanranst closed 2 years ago

woutervanranst commented 2 years ago
public static int Main(string[] args)
{
    var services = new ServiceCollection()
        .AddLogging(builder =>
        {
            // Add File Logging
            builder.AddFile(o => o.RootPath = AppContext.BaseDirectory);
        });

    var x = services.BuildServiceProvider().GetRequiredService<ILoggerFactory>();

    return 0;
}

with csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <AssemblyName>arius</AssemblyName>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.2.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
    </ItemGroup>

    <ItemGroup>
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>

yields this exception on the GetRequiredService

System.MethodAccessException: 'Attempt by method 'Microsoft.Extensions.Logging.Configuration.LoggerProviderConfigurationFactory.GetConfiguration(System.Type)' to access method 'Microsoft.Extensions.Logging.ProviderAliasUtilities.GetAlias(System.Type)' failed.'

When changing the csproj file to the following (note the Hosting io. Configuration.Json, DependencyInjection, Logging) and cleaning the solution, it works as expected

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <AssemblyName>arius</AssemblyName>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Karambolo.Extensions.Logging.File" Version="3.2.1" />
        <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
    </ItemGroup>

    <ItemGroup>
        <None Update="appsettings.json">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

</Project>
adams85 commented 2 years ago

Please read this part of the docs carefully. You've been bitten by this:

The package depends on some framework libraries and references the lowest possible versions of these depencencies (e.g. Microsoft.Extensions.Logging.Configuration 3.0.0 in the case of .NET Standard 2.1 target framework). These versions may not (mostly do not) align with the version of your application's target platform since that may be a newer patch, minor (or even major) version (e.g. .NET Core 3.1.11).

You're referencing Microsoft.Extensions.Logging 6.0.0 in your csproj. Either don't do that (as Karambolo.Extensions.Logging.File already does, just version 3.0.0) or explicitly reference the dependencies of Karambolo.Extensions.Logging.File with version 6.0.0.

Otherwise you'll end up with mixed versions of dependencies which leads to MethodAccessException as apparently the logger internals have breaking changes between 3.0.0 and 6.0.0.

Bottom line: add the following package references to your csproj and you're good to go.

<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="6.0.0" />
woutervanranst commented 2 years ago

Hm; RTFM you say? ;)

Thanks -- that worked. Was hoping you'd release a new version on the back of the net6 release? :)

adams85 commented 2 years ago

Unfortunately adding other target frameworks like .NET 6 wouldn't really help. Since the base metapackage Microsoft.NETCore.App doesn't references the *Microsoft.Extensions.* packages, only the Microsoft.AspNetCore.App* does, I'd be compelled to also add a FrameworkReference to the latter. The problem is that not all .NET projects are ASP.NET Core applications (there are e.g. console and desktop apps) and referencing the whole ASP.NET Core enchilada in such projects seems wrong.

Another option would be to make releases with updated dependency versions every time MS release a new version (even a patch version) of the runtime. But this would be kinda nonsensical too.

So I'm afraid currently there's no better solution than explicit references in non-ASP.NET Core projects. Anyway, I've updated the installation instructions in the docs to make this a bit clearer.