dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.43k stars 10.01k forks source link

AddAzureWebAppDiagnostics throws an exception if used in combination with azure webapp zip deploy #32964

Closed saithis closed 2 years ago

saithis commented 4 years ago

Describe the bug

AddAzureWebAppDiagnostics throws an exception if used in combination with azure webapp zip deploy. There is no exception if we deploy the code with Visual Studio (no zip deploy).

To Reproduce

Steps to reproduce the behavior:

  1. Using version '3.0.0' of package 'Microsoft.Extensions.Logging.AzureAppServices'
  2. Run this code:

        private static readonly LoggerProviderCollection LoggerProvider = new LoggerProviderCollection();
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    .UseSerilog(providers: LoggerProvider)
                    .ConfigureLogging(config =>
                    {
                        config.ClearProviders();
                        config.SetMinimumLevel(LogLevel.Trace);
                        config.AddAzureWebAppDiagnostics();
                    })
                    .UseStartup<Startup>();
                });
  3. Deploy to Azure with Zip Deployment/WEBSITE_RUN_FROM_PACKAGE: 1
  4. See error:

System.IO.FileNotFoundException: Error reading the D:\home\site\diagnostics\ directory. at System.IO.FileSystemWatcher.StartRaisingEvents() at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed() at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value) at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher() at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter) at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter) at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b1_0() at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration1..ctor(Func1 changeTokenProducer, Action1 changeTokenConsumer, TState state) at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func1 changeTokenProducer, Action changeTokenConsumer) at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source) at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at Microsoft.Extensions.Logging.AzureAppServices.SiteConfigurationProvider.GetAzureLoggingConfiguration(IWebAppContext context) at Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder, IWebAppContext context) at Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder) at Prosoft.Tenant.Api.Program.<>c.b__1_2(ILoggingBuilder config) in C:\j\ws\Recruiting\prosoft-recruiting-qa\Prosoft.Tenant.Api\Program.cs:line 66 at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure) at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>cDisplayClass8_0.b0(IServiceCollection collection) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass9_0.b0(WebHostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass10_0.b__0(HostBuilderContext context, IServiceCollection builder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build()

Expected behavior

It should find and be able to read the D:\home\site\diagnostics\settings.json, as it is definitelly there.

Additional context

We use AspNetCoreModuleV2 with InProcess hosting and .NET Core 3.0

M-Wildgruber commented 4 years ago

We encounter the exact same error for one of our solutions. When deploying from VS2019, the application starts without a problem, but when deployed through an Azure Pipelines Release pipeline, the application fails to start with the error message shown above. The release task defaults to "run from package". The Azure Web App uses a windows host. We also used Kudu to verify that the directory exists and even accessed D:\home\site\diagnostics from our code successfully (as long as we did not include the code to configure logging with AddAzureWebAppDiagnostics). Unfortunately, we were unable to reproduce the behavior for a smaller solution. For now, we were able to solve the problem by setting the deployment method to "WebDeploy" in the Azure Release pipeline.

saithis commented 4 years ago

I did some further testing and it seems like the diagnostics folder is the problem.

I used this code:

var dirPath = "D:\\home\\site\\diagnostics";
var filePath = $"{dirPath}\\settings.json";
Log.Error($"Directory.Exists: {Directory.Exists(dirPath)}");
Log.Error($"File.Exists: {File.Exists(filePath)}");
logpathfiles("D:\\home\\site");
logpathfiles("D:\\home\\site\\diagnostics");
void logpathfiles(string path)
{
    try
    {
        Log.Error($"Path: {path}");
        Log.Error($"Directory.GetDirectories: {string.Join(",", Directory.GetDirectories(path))}");
        Log.Error($"Directory.GetFiles: {string.Join(",", Directory.GetFiles(path))}");
    }
    catch (Exception e)
    {
        Log.Error(e, "logpathfiles failed");
    }
}

and got this result:

[ERR] Directory.Exists: True
[ERR] File.Exists: False
[ERR] Path: D:\home\site
[ERR] Directory.GetDirectories: D:\home\site\deployments,D:\home\site\diagnostics,D:\home\site\locks,D:\home\site\logs,D:\home\site\wwwroot
[ERR] Directory.GetFiles: 
[ERR] Path: D:\home\site\diagnostics
[ERR] logpathfiles failed 
System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\diagnostics'.
   at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
   at System.IO.Enumeration.FileSystemEnumerator`1.Init()
   at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, Boolean isNormalized, EnumerationOptions options)
   at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options, Boolean isNormalized)
   at System.IO.Enumeration.FileSystemEnumerableFactory.UserDirectories(String directory, String expression, EnumerationOptions options)
   at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
   at System.IO.Directory.GetDirectories(String path)
   at (omitted).Program.<Main>g__logpathfiles|2_0(String path) in C:\(omitted)\Program.cs:line 98

Somehow the diagnostics folder is there, but not accessible from code. If I try to open the folder and settings.json from kudu, it works.

BrennanConroy commented 4 years ago

@suwatch Is there anything odd about the D:\home\site\diagnostics directory that would cause failures when trying to path traverse it in .NET?

ghost commented 3 years ago

Tagging subscribers to this area: @eerhardt, @maryamariyan See info in area-owners.md if you want to be subscribed.

Issue Details
# Describe the bug AddAzureWebAppDiagnostics throws an exception if used in combination with azure webapp zip deploy. There is no exception if we deploy the code with Visual Studio (no zip deploy). ### To Reproduce Steps to reproduce the behavior: 1. Using version '3.0.0' of package 'Microsoft.Extensions.Logging.AzureAppServices' 2. Run this code: ``` private static readonly LoggerProviderCollection LoggerProvider = new LoggerProviderCollection(); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { .UseSerilog(providers: LoggerProvider) .ConfigureLogging(config => { config.ClearProviders(); config.SetMinimumLevel(LogLevel.Trace); config.AddAzureWebAppDiagnostics(); }) .UseStartup(); }); ``` 3. Deploy to Azure with Zip Deployment/WEBSITE_RUN_FROM_PACKAGE: 1 4. See error: > System.IO.FileNotFoundException: Error reading the D:\home\site\diagnostics\ directory. > at System.IO.FileSystemWatcher.StartRaisingEvents() > at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed() > at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value) > at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher() > at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter) > at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter) > at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b__1_0() > at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state) > at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer) > at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source) > at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder) > at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() > at Microsoft.Extensions.Logging.AzureAppServices.SiteConfigurationProvider.GetAzureLoggingConfiguration(IWebAppContext context) > at Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder, IWebAppContext context) > at Microsoft.Extensions.Logging.AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics(ILoggingBuilder builder) > at Prosoft.Tenant.Api.Program.<>c.b__1_2(ILoggingBuilder config) in C:\j\ws\Recruiting\prosoft-recruiting-qa\Prosoft.Tenant.Api\Program.cs:line 66 > at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure) > at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>c__DisplayClass8_0.b__0(IServiceCollection collection) > at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass9_0.b__0(WebHostBuilderContext context, IServiceCollection services) > at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass10_0.b__0(HostBuilderContext context, IServiceCollection builder) > at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() > at Microsoft.Extensions.Hosting.HostBuilder.Build() ### Expected behavior It should find and be able to read the D:\home\site\diagnostics\settings.json, as it is definitelly there. ### Additional context We use AspNetCoreModuleV2 with InProcess hosting and .NET Core 3.0
Author: saithis
Assignees: -
Labels: `area-Extensions-Hosting`, `untriaged`
Milestone: -
ZenwalkerD commented 3 years ago

Hi What is the solution this?

We are also facing the same issue in Azure App Service. Dotnet 5 is being used.

asilverstein commented 3 years ago

We are also experiencing the same exact error as described.

The only solution for us was to change (in web.config) AspNetCoreModuleV2 to AspNetCoreModule.

We don't think this is a good solution, since (I believe) it takes us out of process, although it works.

Please advise.

saithis commented 3 years ago

Unfortunately we never found a solution and stopped using azure web app diagnostics.

ZenwalkerD commented 3 years ago

Unfortunately we never found a solution and stopped using azure web app diagnostics.

Then how are you analyzing the app logs? Diagnostics is for debugging of app right?

saithis commented 3 years ago

We use Serilog to log to a different place now.

Edit: For us the amount of apps increased to such an amount, that a single sink for all logs of all apps was the way to go anyway. But if you only have one or a few apps, then this is much more cumbersome and a fix to this bug would probably be preferable to you.

eerhardt commented 3 years ago

Since the AzureAppServicesLoggerFactoryExtensions.AddAzureWebAppDiagnostics code is in dotnet/aspnetcore, I am transferring this issue to that repo.

BrennanConroy commented 3 years ago

I've tried to repro the issue and been unsuccessful so far. New Azure App Service, turn on diagnostics, zip deploy app that uses AzureWebAppDiagnostics, see that app is running and logs are streaming through portal.

Is there some specific steps to hit the issue or is it random?

asilverstein commented 3 years ago

Probably was already stated, but it only occurred for me with net5 projects

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

adityamandaleeka commented 3 years ago

Can anyone confirm whether this issue is still occurring? There was a recent App Service update that fixed a file system issue that this sounds very similar to.

adityamandaleeka commented 2 years ago

No response on this issue, so I'll close it. If anyone hits it we can reopen.