使用log4net在.net5中作为单个文件进行部署会引发配置文件异常 [英] Deploy as single-file in .net5 with log4net throws exception for config-file

查看:180
本文介绍了使用log4net在.net5中作为单个文件进行部署会引发配置文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试部署用.Net 5编写的控制台应用程序,并将log4net作为单个文件.运行已部署的应用程序会引发异常.

Trying to deploy a console-application written in .Net 5 with log4net as a single-file. Running deployed application throws exception.

复制步骤

  • dotnet新控制台--name TestConsole-语言C#(确保.net 5.0)
  • 安装包log4net
  • Program.cs
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure(new FileInfo("log.config"));
var logger = log4net.LogManager.GetLogger("TestLogger");
logger.Info("Hello World!");
}

  • log.config(始终复制)
  • <?xml version="1.0" encoding="utf-8" ?>
    <log4net>
      <logger name="TestLogger">
        <level value="ALL" />
        <appender-ref ref="console" />
      </logger>
      <appender name="console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level - %message%newline" />
        </layout>
      </appender>
    </log4net>
    

    • 发布(没有单个文件)=有效(运行控制台应用程序时)
    • dotnet发布-o.\ Publish-自包含true -r win-x64

      dotnet publish -o .\Publish --self-contained true -r win-x64

      • 发布(没有单个文件)=引发异常(运行控制台应用程序时)

      dotnet publish -o.\ Publish-自包含true -r win-x64 -p:PublishSingleFile = true

      dotnet publish -o .\Publish --self-contained true -r win-x64 -p:PublishSingleFile=true

      抛出异常:

      log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
      System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
       ---> System.IO.FileNotFoundException: Cannot find file. (0x80070002)
         at System.Reflection.RuntimeModule.GetFullyQualifiedName()
         at System.Reflection.RuntimeModule.get_Name()
         at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
         at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
         at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
         at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
         at System.Configuration.ClientConfigurationSystem..ctor()
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
         --- End of inner exception stack trace ---
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
         at System.Configuration.ConfigurationManager.PrepareConfigSystem()
         at System.Configuration.ConfigurationManager.GetSection(String sectionName)
         at System.Configuration.ConfigurationManager.get_AppSettings()
         at log4net.Util.SystemInfo.GetAppSetting(String key)
      log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
      System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
       ---> System.IO.FileNotFoundException: Cannot find file. (0x80070002)
         at System.Reflection.RuntimeModule.GetFullyQualifiedName()
         at System.Reflection.RuntimeModule.get_Name()
         at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
         at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
         at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
         at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
         at System.Configuration.ClientConfigurationSystem..ctor()
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
         --- End of inner exception stack trace ---
         at System.Configuration.ConfigurationManager.PrepareConfigSystem()
         at System.Configuration.ConfigurationManager.GetSection(String sectionName)
         at System.Configuration.ConfigurationManager.get_AppSettings()
         at log4net.Util.SystemInfo.GetAppSetting(String key)
      

      我错过了什么?

      推荐答案

      所以,我认为问题是由于.NET 5的变化引起的.我还假设log4net需要进行更新以使用新的单个文件.格式.

      So I believe the issue is due to a change in .NET 5. I'm also assuming log4net will need to be updated to work with the new single file format.

      这里讨论的是.NET 5如何处理与3.1不同的单个文件.

      There is a discussion here about how .NET 5 handles single files differently than 3.1.

      https://github.com/dotnet/core/issues/5409#issuecomment-715522029

      在3.1中,从技术上讲,单个可执行文件已解压缩并从temp运行(如果以我的经验,这可能会导致本地引用文件出现问题,如果这些文件未在项目文件中设置为包含在单个文件之外).在.NET 5中,他们试图转移到程序集在执行到内存时仍位于同一目录中的情况.但是,显然,这会导致其他问题,就像您在log4net中发现的那样.

      In 3.1, technically the single executable gets unzipped and ran from temp (which could cause issues with local referenced files as least in my experience, if those files were not set in the project file to be included outside the single file). In .NET 5 they were trying to move to a case where the assembly stayed in the same directory when executed into memory. However, clearly, this causes other issues as you found with log4net.

      上面的链接使我进入了答案:

      This link above put me on to the answer:

      dotnet publish -r win-x64 /p:PublishSingleFile=true /p:IncludeAllContentForSelfExtract=true
      

      /p:IncludeAllContentForSelfExtract = true强制.NET 5单个文件的行为与3.1完全相同,并且对我有用.

      The /p:IncludeAllContentForSelfExtract=true forces .NET 5 single files to act exactly the same as 3.1 and is what worked for me.

      在接下来的几个月中,我将继续尝试,希望log4net .NET 5兼容性开始与新格式兼容.但是至少这个标志让我向前走了.

      I'll keep trying over the next few months with the hope that log4net .NET 5 compatibility starts working with the new format. But at least this flag let me move foward.

      这篇关于使用log4net在.net5中作为单个文件进行部署会引发配置文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆