Ole Albers
Ole Albers

Reputation: 9285

Azure-Environmentvariable not readable on NLog - Config

I deployed a webjob onto Azure (under the home\site\wwwroot\App_Data\jobs\triggered directory). This application contains NLog-logging which is configured in the appsettings and uses environment-variable for the logfile-path:

 "NLog": {
    "throwConfigExceptions": true,
    "targets": {
      "logfile": {
        "type": "File",
        "fileName": "${environment:variable=DEPLOYMENT_SOURCE}\\LogFiles\\timer-${shortdate}.log",
        "layout": "${message} "
      },

The DEPLOYMENT_SOURCE - Environment-variable contains a valid path when displaying it in kudu:

echo %DEPLOYMENT_SOURCE%
C:\home

But Nlog does not seem to be able to resolve that environment var. When enabling Trace-Log I receive the following error message:

Debug Creating file appender: C:\LogFiles\timer-2020-11-13.log

Trace Opening C:\LogFiles\timer-2020-11-13.log with allowFileSharedWriting=False

Error FileTarget(Name=logfile): Failed write to file 'C:\LogFiles\timer-2020-11-13.log'. Exception: > System.UnauthorizedAccessException: Access to the path 'C:\LogFiles\timer-2020-11-13.log' is denied.

So it seems like DEPLOYMENT_SOURCE is simply an empty string.

When testing this locally though with a valid Windows-Env like %TEMP% everything works fine.

What has to be done to access Azure-Environments in Dotnetcoreapps/NLog-Config?

Upvotes: 0

Views: 387

Answers (1)

Ole Albers
Ole Albers

Reputation: 9285

I solved this issue. The problem is that Triggered WebJobs on Azure DevOps do NOT have the same environment variables available as the Kudu console.

So while Kudu displayed different environment variables like DEPLOYMENT_SOURCE, this variable is not available for webjobs.

But there are other environments (in this case "HOME", like Rolf already mentioned in the comments) that also points to C:\home on Azure. (D:\home in the past)

Upvotes: 1

Related Questions