You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core Web API多环境下NLog日志指定存储至Logs文件夹求助

Fix: NLog Logs Storing in bin Subdirectory Instead of Top-Level Logs Folder

Ah, I’ve dealt with this exact quirk of .NET Core + NLog before! The root issue is that ${baseDir} in .NET Core resolves to the directory containing your compiled application assemblies—so during debugging, that’s the nested bin\netcoreapp3.1\netcoreapp3.1 folder, which is why your logs are ending up there instead of a project-level Logs directory.

Here are three reliable solutions to get all environments (Production, Stage, Development) writing logs to a top-level Logs folder:

Solution 1: Use ASP.NET Core’s Content Root Environment Variable

ASP.NET Core automatically sets the ASPNETCORE_CONTENTROOT environment variable to your project’s root directory (the folder with your .csproj file during debugging, or your publish directory in production). You can reference this directly in your NLog config:

Update your nlog.config target definition to use this variable:

<targets async="true">
  <target xsi:type="File" name="file" layout="${DefaultLayout}" 
          fileName="${environment:ASPNETCORE_CONTENTROOT}\Logs\log-${shortdate}.log" />
</targets>

NLog will automatically create the Logs folder if it doesn’t exist, and logs will land in the correct place across all environments.

If you’re using the NLog.Web.AspNetCore package (standard for ASP.NET Core projects), there’s a purpose-built layout renderer that directly fetches the ASP.NET Core content root—no need to rely on environment variables:

Modify your target’s fileName attribute like this:

<targets async="true">
  <target xsi:type="File" name="file" layout="${DefaultLayout}" 
          fileName="${aspnet-contentroot}\Logs\log-${shortdate}.log" />
</targets>

This is the most reliable approach because it pulls the path directly from ASP.NET Core’s host environment, avoiding any potential issues with environment variable overrides.

Solution 3: Manually Configure the Path via Code (For Advanced Control)

If you need dynamic path customization (e.g., environment-specific subfolders), you can set the log root path in your Program.cs and pass it to NLog:

First, define a variable in nlog.config:

<variable name="LogRoot" value="${aspnet-contentroot}\Logs" />
<targets async="true">
  <target xsi:type="File" name="file" layout="${DefaultLayout}" 
          fileName="${LogRoot}\log-${shortdate}.log" />
</targets>

Then update your Program.cs to override this variable with the content root path:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.SetMinimumLevel(LogLevel.Trace);
        })
        .UseNLog()
        .ConfigureNLog((context, config) =>
        {
            // Set LogRoot to the content root + Logs folder
            config.Variables["LogRoot"] = Path.Combine(context.HostingEnvironment.ContentRootPath, "Logs");
        });

This gives you full control if you need to adjust paths based on environment later.

Quick Verification

After updating your config, start your app—you’ll see a Logs folder appear in your project root (or publish directory) with your log files inside, no more nested bin folder mess!

内容的提问来源于stack exchange,提问作者Bhavin Varsur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:57:58