1
votes

I have an ASP.NET Web API deployed on Azure App Service as a Web App.

I've turned on Diagnostic Logging for the App Service.

enter image description here

From within the API code, when I try to write a log message to a file, apparently, the file system relative path "." resolves to "D:\Windows\system32\" on the target machine/VM/container.

I know this because I have my test action method set up to print the entire exception as follows:

[HttpGet]
[Route("~/test/log")]
public async Task<string> LogAsync()
{
  try
  {
    System.IO.File.WriteAllText(".\\log.txt", "Test log entry.");

    return await Task.FromResult("Passed");
  }
  catch(Exception ex)
  {
    return ex.ToString();
  }
}

And it returns the following result:

System.UnauthorizedAccessException: Access to the path 'D:\Windows\system32\log.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding) at System.IO.File.InternalAppendAllText(String path, String contents, Encoding encoding) at System.IO.File.AppendAllText(String path, String contents) at My.Web.API.Controllers.TestController.d__5.MoveNext() in C:\My.Web.API\My.Web.API\Controllers\TestController.cs:line 125

1
You might need to look at app insights docs.microsoft.com/en-us/azure/azure-monitor/app/…Anass Kartit

1 Answers

1
votes

Currently You are trying to save logs under D:\Windows\System32 folder which shouldn't be allowed as you are getting access denied error. Please try to change the path to D:\Home\Logfiles\Application which is the source directory for application logging.

Also just to update , For Application logging, you can turn on the file system option temporarily for debugging purposes. This option turns off automatically in 12 hours. You can also turn on the blob storage option to select a blob container to write logs to.

See https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs for more information on the default locations for logs.

Let me know if you need any other information on this.

This is hierarchy of your azure app when you will see it form KUDU

enter image description here