Logging no longer working on Azure App Service

565 Views Asked by At

I have a code base that is running on two separate app services. The first is logging fine with version 6.0.8 of the ASP.NET Core Logging Integration. The second which has ASP.NET Core Logging Integration 7.0.0-rc-2-22476-2 is not working. I get the following error when the web service starts:

2022-10-25T21:07:05 sandboxproc.exe D:\DWASFiles\Sites\teams1stdevelopment\Temp\applicationhost.config True True
2022-10-25T21:07:05 env XPROC_TYPENAME=Microsoft.Web.Hosting.Transformers.ApplicationHost.SiteExtensionHelper, Microsoft.Web.Hosting, Version=7.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
2022-10-25T21:07:05 env XPROC_METHODNAME=Transform
2022-10-25T21:07:05 Start 'Microsoft.AspNetCore.AzureAppServices.SiteExtension' site extension transform
2022-10-25T21:07:05 StartSection Executing InsertIfMissing (transform line 7, 23)
2022-10-25T21:07:05 on /configuration/system.webServer
2022-10-25T21:07:05 Applying to 'configuration' element (no source line info)
2022-10-25T21:07:05 EndSection Done executing InsertIfMissing
2022-10-25T21:07:05 StartSection Executing InsertIfMissing (transform line 8, 16)
2022-10-25T21:07:05 on /configuration/system.webServer/runtime
2022-10-25T21:07:05 Applying to 'system.webServer' element (no source line info)
2022-10-25T21:07:05 Inserted 'runtime' element
2022-10-25T21:07:05 EndSection Done executing InsertIfMissing
2022-10-25T21:07:05 StartSection Executing InsertIfMissing (transform line 9, 31)
2022-10-25T21:07:05 on /configuration/system.webServer/runtime/environmentVariables
2022-10-25T21:07:05 Applying to 'runtime' element (no source line info)
2022-10-25T21:07:05 EndSection Done executing InsertIfMissing
2022-10-25T21:07:05 :(10,406), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext)
2022-10-25T21:07:05 :(11,158), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext)
2022-10-25T21:07:05 :(12,146), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext)
2022-10-25T21:07:05 Fail 'Microsoft.AspNetCore.AzureAppServices.SiteExtension' site extension transform
2022-10-25T21:07:05 sandboxproc.exe Elapsed = 426.00 ms failed with System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Fail transforming applicationHost.config with 'C:\home\SiteExtensions\Microsoft.AspNetCore.AzureAppServices.SiteExtension\applicationHost.xdt' with :(10,406), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext):(11,158), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext):(12,146), Microsoft.Web.XmlTransform.XmlNodeException: Could not resolve 'InsertOrAppendAttribute' as a type of Transform ---> Microsoft.Web.XmlTransform.XmlTransformationException: Could not resolve 'InsertOrAppendAttribute' as a type of Transformat Microsoft.Web.XmlTransform.NamedTypeFactory.Construct[ObjectType](String typeName)at Microsoft.Web.XmlTransform.XmlElementContext.CreateObjectFromAttribute[ObjectType](String& argumentString, XmlAttribute& objectAttribute)--- End of inner exception stack trace ---at Microsoft.Web.XmlTransform.XmlElementContext.ConstructTransform(String& argumentString)at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement(XmlElementContext context)at Microsoft.Web.XmlTransform.XmlTransformation.TransformLoop(XmlNodeContext parentContext)at Microsoft.Web.Hosting.Transformers.ApplicationHost.SiteExtensionDefinition.Transform(XmlDocument document, IDictionary`2 environments, Boolean isMainSite, Boolean separateProcessForScm)at Microsoft.Web.Hosting.Transformers.ApplicationHost.SiteExtensionHelper.Transform(XmlDocument doc, List`1 definitions, IDictionary`2 environments, Boolean isMainSite, Boolean separateProcessForScm)at Microsoft.Web.Hosting.Transformers.ApplicationHost.SiteExtensionHelper.Transform(String appHostConfig, String isMainSiteStr, String separateProcessForScmStr)--- End of inner exception stack trace ---at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)at Microsoft.Web.Hosting.ProcessModel.Program.Main(String[] args)

Installed Packages:

Packages

    // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;

namespace Microsoft.BotBuilderSamples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, config) =>
            {
                var keyVaultEndpoint = new Uri(Environment.GetEnvironmentVariable("VaultUri"));
                config.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureLogging((logging) =>
                {
                    logging.AddDebug();
                    logging.AddConsole();
                });
                webBuilder.UseStartup<Startup>();
            });
    }
}
2

There are 2 best solutions below

0
On BEST ANSWER

The band-aid fix for this is to go back to a different revision of the ASP.NET Core Logging Integration. I (fortunately!) had one app service that had not been upgraded. I'm not sure if there is another way to recreate this from source.

First download the "good" extension (in this case 6.0.8)

  • Select Advanced Tools in the App Service

  • From KUDU, select Debug console -> CMD

  • Click SiteExtensions

  • Click the "Download" button to the left of Microsoft.AspNetCore.AzureAppServices.SiteExtension

  • This will create a .zip file in your downloads directory.

Now follow the same steps as above to get to the SiteExtensions directory in your "bad" app service.

  • Click on Microsoft.AspNetCore.AzureAppServices.SiteExtension

  • Delete everything within this directory (using icons to the left)

  • Drag and drop the .zip file from the downloads directory to the KUDU web interface.

  • In the cmd window below type "unzip .zip", with the filename of the .zip file that was created from the download.

Now restart the service and logging will once again flourish! I have a service ticket with Microsoft alerting them to this issue. Will try to post again if they get it fixed!

0
On

If you are running locally, then then configurations are taken from secrets.json.

Right click on the Project folder and click on Manage User Secrets.secrets.json file will be created.

enter image description here

Add the keyvault settings in secrets.json file

{
 "VaultUri": "https://yourkeyvaultname.vault.azure.net/"
}
  • Same settings have to be added in appsettings.json file.

  • Before adding the Key Vault Configuration, Deploy the app to Azure App service.

  • Once the App is deployed, the app is available in Azure Active Directory. If it is not available create a New Registration Copy the Object ID from the Azure Active Directory enter image description here In .NET core latest versions (CORE 6 and 7), we do not have Startup.cs.All the code has to be configured in Prpgram.cs file.

  • Get ObjectID from Azure Active Directory => App Registration

  • To access Azure key vault secrets, we need to give permissions. Run the below command in Azure CLI.

az keyvault set-policy --name "YourKeyVaultName" --object-id  --secret-permissions get list

In Program.cs,add the below lines of code

var builder = WebApplication.CreateBuilder(args);
var keyVaultName = builder.Configuration["VaultUri"];
var secretClient = new SecretClient(new(keyVaultName), new DefaultAzureCredential());
builder.Configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
builder.Logging.AddDebug();
builder.Logging.AddConsole();
  • Install the NuGet Package Microsoft.AspNetCore.AzureAppServices.SiteExtension laetst version

Logging no longer working on Azure App Service ASP.NET Core Logging Integration 7.0.0-rc-2-22476-2 is not working.

  • Currently there is no support for .NET CORE 7 RC2 for Azure App Service. enter image description here

  • If you are using .NET CORE 7 WebApp, change the deployment mode to Self-contained and then publish.

enter image description here