简体   繁体   English

使用 PublishSingleFile 选项集配置 NLOG

[英]Configuring NLOG with PublishSingleFile option set

I am in a corner now and I am not able to resolve this problem to my satisfaction.我现在处于一个角落,我无法满意地解决这个问题。

The problems is caused by the use of NLOG in the Program.cs file of an ASP.NET Core 3.0 application when the application is published using the flag PublishSingleFile in the Visual Studio 2019's Publish options dialog with a SelfContained application.问题是由于使用带有 SelfContained 应用程序的 Visual Studio 2019 发布选项对话框中的标志 PublishSingleFile 发布应用程序时在 ASP.NET Core 3.0 应用程序的 Program.cs 文件中使用 NLOG 引起的。

See: Publishing a single file in NET Core 3.0请参阅: 在 NET Core 3.0 中发布单个文件

If I enable this option the publish procedure creates a single file that contains all the required files including the NLOG assembly.如果我启用此选项,则发布过程会创建一个包含所有必需文件(包括 NLOG 程序集)的文件。
I deploy this standalone executable along with configurations file like appsettings.json and nlog.config in the site's root as well the content folder wwwroot.我在站点的根目录以及内容文件夹 wwwroot 中部署了这个独立的可执行文件以及 appsettings.json 和 nlog.config 等配置文件。

The problem arises when a request arrives to start the application.当请求到达以启动应用程序时,就会出现问题。 At this point I get an immediate crash at the first line of the main method.此时,我在 main 方法的第一行立即崩溃。 After enabling the stdout.log file I can see the reason.启用 stdout.log 文件后,我可以看到原因。

Unhandled exception.未处理的异常。 System.IO.FileNotFoundException: Failed to load NLog LoggingConfiguration. System.IO.FileNotFoundException:无法加载 NLog LoggingConfiguration。 Searched the following locations:搜索了以下位置:

  • C:\Windows\TEMP.net\MyAPI\11jsrg2n.1ad\nlog.config C:\Windows\TEMP.net\MyAPI\11jsrg2n.1ad\nlog.config

File name: 'nlog.config' at NLog.LogFactory.LoadConfiguration(String configFile, Boolean optional) at NLog.LogFactory.LoadConfiguration(String configFile) at NLog.LogManager.LoadConfiguration(String configFile) at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName) at MyAPI.Program.Main(String[] args)文件名:NLog.LogFactory.LoadConfiguration 中的“nlog.config”(String configFile,Boolean 可选)在 NLog.LogFactory.LoadConfiguration(String configFile)在 NLog.LogManager.LoadConfiguration(String configFile)在 NLog.ZC6E190B284633C48E39E55049DALogConfig(String configFile) MyAPI.Program.Main(String[] args) 处的字符串 configFileName)

It seems like the whole exe is run from a different directory than the site's root folder.似乎整个 exe 是从与站点根文件夹不同的目录运行的。

Of course this happens because the first line in my main method tries to initialize the NLOG assembly.当然,这是因为我的 main 方法的第一行尝试初始化 NLOG 程序集。

To complete this question this is the code on my program.cs file要完成这个问题,这是我的 program.cs 文件中的代码

public class Program
{
    public static void Main(string[] args)
    {
        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        try
        {
            logger.Info("----------------------------");
            logger.Info("MyAPI Application Start");
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            logger.Error(ex, "MyAPI stopped on Exception");
            throw;
        }
        finally
        {
            LogManager.Shutdown();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Warning);
            })
            .UseNLog()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

So, thanks to everyone that followed my explanation and here is the question.因此,感谢所有遵循我的解释的人,这就是问题所在。

These is a way to configure NLOG in the main method (as well in the Startup code) when the application is published using the PublishSingleFile flag?当使用 PublishSingleFile 标志发布应用程序时,这是一种在 main 方法(以及在启动代码中)配置 NLOG 的方法?

I forgot to add that if I publish without this settings everything works fine, thus this problem is not a showstopper, but nevertheless I would like to know if there is a workaround.我忘了补充一点,如果我在没有此设置的情况下发布一切正常,因此这个问题不是一个大问题,但我想知道是否有解决方法。

If you want NLog.config included/embedded in the single-exe-file, then try to specify this in csproj-file:如果您希望 NLog.config 包含/嵌入在单个 exe 文件中,请尝试在 csproj 文件中指定:

<Content Include="nlog.config">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>

If you want to have NLog.config to be placed externally/outside the single-exe-file.如果您希望将 NLog.config 放置在单个 exe 文件的外部/外部。 Then try to explicit specify the process-directory:然后尝试显式指定进程目录:

var processPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
var nlogConfigPath = Path.Combine(processPath, "nlog.config");
var logger = NLogBuilder.ConfigureNLog(nlogConfigPath).GetCurrentClassLogger();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM