huorswords / Microsoft.Extensions.Logging.Log4Net.AspNetCore

Allows to configure Log4net as Microsoft Extensions Logging handler on any ASP.NET Core application. Original code proposal by @anuraj --> https://dotnetthoughts.net/how-to-use-log4net-with-aspnetcore-for-logging/
Apache License 2.0
249 stars 123 forks source link

Upgrading to net8.0 throws exception on startup. #135

Open danjarvis opened 10 months ago

danjarvis commented 10 months ago

Hello,

I have a Windows Service that was functioning correctly under net6.0. I recently upgraded and everything works correctly in a development environment. However, after installing on a system I get an exception on startup. Here is the code I am using to configure logging in Program.cs:

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService(options => {
        options.ServiceName = "ArcturusIntegrationEngine";
    })
    .ConfigureLogging(logging => {
        logging.ClearProviders();
        logging.AddLog4Net(log4NetConfigFile: "log4net.config");
        logging.AddConsole();
    })
   // Omitted for brevity
   .Build();

await host.RunAsync();

My log4net config gets installed alongside the .exe and it looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
    <root>
        <level value="ALL" />
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>
    <appender name="console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %level %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="file" type="log4net.Appender.RollingFileAppender">
        <file value="${ProgramData}\Vysix\Arcturus\IntegrationEngine\logs\app.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
        </layout>
    </appender>
</log4net>

The error I am seeing in windows event viewer:

Application: Vysix.Arcturus.IntegrationEngine.exe
CoreCLR Version: 8.0.23.53103
.NET Version: 8.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException: The type initializer for 'log4net.Core.LoggerManager' threw an exception.
 ---> System.ArgumentOutOfRangeException: Parameter: defaultRepositoryType, Value: [log4net.Repository.Hierarchy.Hierarchy] out of range. Argument must implement the ILoggerRepository interface (Parameter 'defaultRepositoryType')
Actual value was log4net.Repository.Hierarchy.Hierarchy.
   at log4net.Core.DefaultRepositorySelector..ctor(Type)
   at log4net.Core.LoggerManager..cctor()
   --- End of inner exception stack trace ---
   at log4net.Core.LoggerManager.CreateRepository(Assembly, Type)
   at log4net.LogManager.CreateRepository(Assembly, Type)
   at Microsoft.Extensions.Logging.Log4NetProvider.CreateLoggerRepository(Assembly)
   at Microsoft.Extensions.Logging.Log4NetProvider..ctor(Log4NetProviderOptions)
   at Microsoft.Extensions.Logging.Log4NetExtensions.AddLog4Net(ILoggingBuilder, Log4NetProviderOptions)
   at Microsoft.Extensions.Logging.Log4NetExtensions.AddLog4Net(ILoggingBuilder, String)
   at Program.<>c.<<Main>$>b__0_1(ILoggingBuilder logging)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.<>c__DisplayClass5_0.<ConfigureLogging>b__1(ILoggingBuilder builder)
   at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection, Action`1)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.<>c__DisplayClass5_0.<ConfigureLogging>b__0(HostBuilderContext context, IServiceCollection collection)
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Program.<Main>$(String[])
   at Program.<Main>(String[])

My dependencies:

  <ItemGroup>
    <PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.300.34" />
    <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.300" />
    <PackageReference Include="AWSSDK.S3" Version="3.7.305.9" />
    <PackageReference Include="AWSSDK.SimpleEmail" Version="3.7.300.34" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
    <PackageReference Include="MimeKit" Version="4.3.0" />
    <PackageReference Include="Vysix.Arcturus.ApiClient" Version="1.0.0" />
    <PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.8.0" />
    <PackageReference Include="Quartz.Extensions.Hosting" Version="3.8.0" />
  </ItemGroup>

I am unable to resolve this issue and was wondering if anyone else has experienced it... and if so what a work around would be. I will be downgrading back to net6.0 if I cannot resolve this.

huorswords commented 9 months ago

Sorry to be late on answering you.

First, thank you to use this nuget package.

I have updloaded a Windows Service sample working properly in .NET 8.0, at least in my machine (a litlle kitten just died only by saying this...). Please, could you check if the problem is reproduced in your side with this .NET 8.0 sample?

Thanks. :)