NLog File Target的FileName参数配置咨询(.NET应用场景)
FileName Parameter Configuration Guide Hey there! Let's dive into all the ins and outs of configuring the FileName parameter for NLog File Targets, using your existing App.config setup as a starting point.
1. Understanding the Variables in Your Current Setup
First, let's break down the variables you're already using—they're key to flexible log file naming:
${basedir}: Resolves to the base directory of your .NET application (the folder where your executable lives). This is great for relative pathing, so your logs stay tied to the app's location.${shortdate}: Inserts the current date inyyyy-MM-ddformat (e.g.,2024-05-20). YourassemblyLogFiletarget uses this to create a daily subfolder for logs, which is perfect for keeping logs organized by date.
Your existing assemblyLogFile config is a solid example of date-based log organization:
<target name="assemblyLogFile" xsi:type="File" fileName="${basedir}/logs/${shortdate}/assembly.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"> </target>
The createDirs="true" flag ensures NLog automatically creates the daily subfolder if it doesn't exist—no need to handle directory creation in your code.
2. Rolling Log Files (For Your Partial appLogFile Target)
It looks like you started configuring an appLogFile target but didn't finish. A common use case here is rolling logs—either by file size, date, or both. Here are two practical setups:
Option A: Roll by Date (Daily Log Files)
If you want a new app.log file each day (instead of a subfolder), adjust the FileName to include ${shortdate} directly in the filename:
<target name="appLogFile" xsi:type="File" fileName="${basedir}/logs/app-${shortdate}.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"> </target>
This will generate files like app-2024-05-20.log, app-2024-05-21.log, etc.
Option B: Roll by File Size (With Archiving)
If you want to limit individual log file size (e.g., 10MB per file) and keep a set number of archived logs, use these parameters:
<target name="appLogFile" xsi:type="File" fileName="${basedir}/logs/app.log" <!-- Active log file --> archiveFileName="${basedir}/logs/archives/app-{#}.log" <!-- Archived files --> archiveAboveSize="10485760" <!-- 10MB in bytes --> archiveNumbering="Rolling" <!-- Number archives sequentially: app-1.log, app-2.log --> maxArchiveFiles="10" <!-- Keep only the last 10 archived logs --> createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${longdate} ${message}${exception:format=ToString}"> </target>
When app.log hits 10MB, NLog renames it to app-1.log, creates a new app.log, and deletes the oldest archive once you hit 10 files.
3. Advanced Naming Tricks
You can mix other NLog variables to create more granular log files:
${processname}: Include the name of your application process (useful if you have multiple apps sharing a log directory). Example:fileName="${basedir}/logs/${processname}-${shortdate}.log"${level}: Split logs by severity (Info, Warn, Error). Example:fileName="${basedir}/logs/app-${level}.log"${machinename}: Include the machine name (great for multi-server environments). Example:fileName="${basedir}/logs/${machinename}-app.log"
4. Key Notes to Remember
- Relative vs Absolute Paths: Using
${basedir}gives you a relative path, which is portable if you move your app. If you need an absolute path, just hardcode it (e.g.,fileName="C:/MyApplication/logs/app.log"), but relative paths are usually more flexible. keepFileOpen="true": This keeps the log file open between writes, reducing IO overhead. Just make sure your app has the necessary write permissions for the log directory.- Encoding: Your
encoding="utf-8"setting is smart—it ensures special characters (like in exceptions or non-English messages) are logged correctly.
内容的提问来源于stack exchange,提问作者Some User

