当 运行 作为计划任务时,NLog 环境布局渲染器不起作用
NLog environment layout renderer doesn't work when run as a scheduled task
我在 Windows Server 2012 上使用 NLog 4.4.12 和 .NET 4.6.2。
我已经用 nlog 部分配置了我的 app.config,如下所示:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<include file="conf/nlog.${environment:MY_ENV}.config"/>
</nlog>
这里的想法是它将引入一个特定于环境的配置来设置 NLog。
我设置了一个名为 MY_ENV
的系统环境变量(不是用户),并将其值设置为 production
。
如果我运行程序作为当前登录的用户,找到正确的配置文件,并按预期写入日志。
但是,如果我通过 Windows Task Scheduler 设置计划任务,运行 与刚刚使用的相同用户凭据的程序,似乎找不到环境变量?
我已将计划任务配置为运行是否登录,并已启用运行具有最高权限.
当我启用 NLog 的内部诊断时,我在诊断日志中看到以下内容:
2017-10-19 19:27:42.1744 Error Error when including 'conf/nlog..config'. Exception: System.IO.FileNotFoundException: Included file not found: E:\Utilities\MyApp\conf/nlog..config
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
2017-10-19 19:27:42.1744 Error Parsing configuration from E:\Utilities\MyApp\MyApp.exe.Config failed. Exception: NLog.NLogConfigurationException: Exception when parsing E:\Utilities\MyApp\MyApp.exe.Config. ---> NLog.NLogConfigurationException: Error when including: conf/nlog..config ---> System.IO.FileNotFoundException: Included file not found: E:\Utilities\MyApp\conf/nlog..config
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
--- End of inner exception stack trace ---
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
如您所见,包含文件路径的布局呈现似乎得到空字符串或空值。这导致文件路径呈现为:conf/nlog..config
.
我不确定这是 NLog 特有的问题。我查看了 Environment Layout Renderer, and it eventually calls Environment.GetEnvironmentVariable(string).
的源代码
我觉得这可能与 Windows 中计划任务的性质有更多关系,但我不确定我的选择是什么。
无论用户是否登录,此任务都需要运行自动执行。
谁能解释一下这是怎么回事?为什么 NLog 无法引入正确的环境变量?我该怎么做才能解决这个问题?
我认为是这个问题,在更改环境变量时,需要重新启动Taskeng.exe。
You should terminate Taskeng.exe and the next time scheduled task is run it will get an updated environment.
https://superuser.com/questions/331077/accessing-environment-variables-in-a-scheduled-task
我在 Windows Server 2012 上使用 NLog 4.4.12 和 .NET 4.6.2。
我已经用 nlog 部分配置了我的 app.config,如下所示:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<include file="conf/nlog.${environment:MY_ENV}.config"/>
</nlog>
这里的想法是它将引入一个特定于环境的配置来设置 NLog。
我设置了一个名为 MY_ENV
的系统环境变量(不是用户),并将其值设置为 production
。
如果我运行程序作为当前登录的用户,找到正确的配置文件,并按预期写入日志。
但是,如果我通过 Windows Task Scheduler 设置计划任务,运行 与刚刚使用的相同用户凭据的程序,似乎找不到环境变量?
我已将计划任务配置为运行是否登录,并已启用运行具有最高权限.
当我启用 NLog 的内部诊断时,我在诊断日志中看到以下内容:
2017-10-19 19:27:42.1744 Error Error when including 'conf/nlog..config'. Exception: System.IO.FileNotFoundException: Included file not found: E:\Utilities\MyApp\conf/nlog..config
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
2017-10-19 19:27:42.1744 Error Parsing configuration from E:\Utilities\MyApp\MyApp.exe.Config failed. Exception: NLog.NLogConfigurationException: Exception when parsing E:\Utilities\MyApp\MyApp.exe.Config. ---> NLog.NLogConfigurationException: Error when including: conf/nlog..config ---> System.IO.FileNotFoundException: Included file not found: E:\Utilities\MyApp\conf/nlog..config
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
--- End of inner exception stack trace ---
at NLog.Config.XmlLoggingConfiguration.ParseIncludeElement(NLogXmlElement includeElement, String baseDirectory, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
如您所见,包含文件路径的布局呈现似乎得到空字符串或空值。这导致文件路径呈现为:conf/nlog..config
.
我不确定这是 NLog 特有的问题。我查看了 Environment Layout Renderer, and it eventually calls Environment.GetEnvironmentVariable(string).
的源代码我觉得这可能与 Windows 中计划任务的性质有更多关系,但我不确定我的选择是什么。
无论用户是否登录,此任务都需要运行自动执行。
谁能解释一下这是怎么回事?为什么 NLog 无法引入正确的环境变量?我该怎么做才能解决这个问题?
我认为是这个问题,在更改环境变量时,需要重新启动Taskeng.exe。
You should terminate Taskeng.exe and the next time scheduled task is run it will get an updated environment.
https://superuser.com/questions/331077/accessing-environment-variables-in-a-scheduled-task