动态更改日志文件的文件路径 .NET Core + NLog
Change dynamically file path for log files .NET Core + NLog
在我的 .NET Core 应用程序中,我使用了一些环境,我想为每个环境做不同的日志文件路径。
例如,
Development - c:\logs
Staging - d\apps\logs
对于每个环境,我在 appsettings.{env}.json
中都有配置部分:
"LocalPaths": {
"LogFileRootDirectory": "c:\logs\"
}
和nlog.config
的一部分:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="${logFileRootDirectory}internal-nlog.txt">
...
</nlog>
实现它的最佳方法是什么?
正如他们在 GitHub repository 上所说:
.NET Core issues:
${basedir}
isn't working will in .NET Core
LogManager.GetCurrentClassLogger()
will use the filename instead of
the full class name (class name and namespace, like in NLog 4). This
will be fixed in the final of NLog 5 (after the release of NETSTANDARD
2.0)
所以你现在不能这样做。也许他们将来会解决这个问题。
您可以使用 (NLog) 变量。
例如
<targets>
<target name="file" xsi:type="File"
fileName="${var:mydir}/logfile.txt" ... >
和 C#
LogManager.Configuration.Variables["mydir"] = "c:\logs";
当您更改变量时,路径会自动更改 - 无需重新加载。
更新:您可以在
之后更改 LogManager.Configuration
env.ConfigureNLog("nlog.config");
例如您的 startup.cs 可能如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
//add NLog.Web
app.AddNLogWeb();
//configure nlog.config in your project root.
env.ConfigureNLog("nlog.config");
LogManager.Configuration.Variables["mydir"] = "c:\logs";
...
NLog.Extension.Logging 版本。 1.4 支持 ${configsetting}
直接从 appsetting.json:
读取
<targets>
<target name="file" xsi:type="File"
fileName="${configsetting:LocalPaths.LogFileRootDirectory}/logfile.txt" ... >
另请参阅:https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
在我的 .NET Core 应用程序中,我使用了一些环境,我想为每个环境做不同的日志文件路径。
例如,
Development - c:\logs
Staging - d\apps\logs
对于每个环境,我在 appsettings.{env}.json
中都有配置部分:
"LocalPaths": {
"LogFileRootDirectory": "c:\logs\"
}
和nlog.config
的一部分:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="${logFileRootDirectory}internal-nlog.txt">
...
</nlog>
实现它的最佳方法是什么?
正如他们在 GitHub repository 上所说:
.NET Core issues:
${basedir}
isn't working will in .NET CoreLogManager.GetCurrentClassLogger()
will use the filename instead of the full class name (class name and namespace, like in NLog 4). This will be fixed in the final of NLog 5 (after the release of NETSTANDARD 2.0)
所以你现在不能这样做。也许他们将来会解决这个问题。
您可以使用 (NLog) 变量。
例如
<targets>
<target name="file" xsi:type="File"
fileName="${var:mydir}/logfile.txt" ... >
和 C#
LogManager.Configuration.Variables["mydir"] = "c:\logs";
当您更改变量时,路径会自动更改 - 无需重新加载。
更新:您可以在
之后更改LogManager.Configuration
env.ConfigureNLog("nlog.config");
例如您的 startup.cs 可能如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
//add NLog.Web
app.AddNLogWeb();
//configure nlog.config in your project root.
env.ConfigureNLog("nlog.config");
LogManager.Configuration.Variables["mydir"] = "c:\logs";
...
NLog.Extension.Logging 版本。 1.4 支持 ${configsetting}
直接从 appsetting.json:
<targets>
<target name="file" xsi:type="File"
fileName="${configsetting:LocalPaths.LogFileRootDirectory}/logfile.txt" ... >
另请参阅:https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer