windows - 作为计划任务运行时,NLog 环境布局渲染器不起作用

标签 windows nlog taskscheduler

我在 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 的系统环境变量(不是用户),并将其值设置为 生产

如果我以当前登录用户的身份运行该程序,则会找到正确的配置文件,并按预期写入日志。

但是,如果我通过 Windows 任务计划程序设置计划任务,以使用刚刚使用的相同用户凭据运行该程序,则似乎找不到环境变量?

我已将计划任务配置为无论用户是否登录都运行,并且我已启用以最高权限运行

当我启用 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 的源代码,最终调用 Environment.GetEnvironmentVariable(string) .

我觉得这可能与 Windows 中计划任务的性质有更多关系,但我不确定我的选择是什么。

无论用户是否登录,此任务都需要自动运行。

有人可以解释一下这是怎么回事吗?为什么 NLog 无法获取正确的环境变量?我该如何解决这个问题?

最佳答案

我认为这就是问题所在,更改环境变量时,需要重新启动Taseng.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 - 作为计划任务运行时,NLog 环境布局渲染器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46840452/

相关文章:

windows - 构建基于 WebKit 的 Windows 应用程序的最快/最简单方法?

windows - 如何使用 COPY 命令附加到文件

c# - Mono Gtk# 无法编译初学者教程

windows - 如何在命令行中使用 Robocopy 复制带有 TimeStamp 的文件

azure - 在 Azure WebJobs 中使用 NLog 进行日志记录

c# - 如何屏蔽特定请求的敏感数据 (NLog)

c# - 温莎伐木设施 : Control log name

java - 使用 Java ProcessBuilder 从 TaskScheduler 启动的进程运行可见应用程序

windows-server-2012 - Windows任务计划程序 "Launch request ignored, instance already running"