0
votes

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?

1
Looks like DEPLOYMENT_SOURCE contains C:/ but not the expected C:\home, Maybe an environment issue? Thought log-files should be placed here D:/Home/LogFiles/Application that should match %HOME%\LogFiles\Application or ${environment:HOME}/LogFiles/Application/timer-${shortdate}.log. Anyway know little about Azure, but know that NLog has no issue with environment-variables.Rolf Kristensen
You could try to log to a fixed path, and in "layout" also the "${environment}. E.g. "fileName": "c\\logs\\LogFiles\\timer-${shortdate}.log","layout": "${message} env='${environment:variable=DEPLOYMENT_SOURCE}'"Julian
Please note that NLog is using Environment.GetEnvironmentVariable , so nothing special at allJulian
Please also note, not sure if that's the case here. ".NET Core on macOS and Linux does not support per-machine or per-user environment variables." (from MS docs)Julian

1 Answers

1
votes

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)