首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Depdency注入和日志记录在正常使用Azure函数时突然开始失败,而没有进行代码更改。

Depdency注入和日志记录在正常使用Azure函数时突然开始失败,而没有进行代码更改。
EN

Stack Overflow用户
提问于 2019-03-15 02:00:54
回答 1查看 2.1K关注 0票数 2

我有一个项目,通过构造函数使用依赖注入和从ASP.NET CorewithAzureFunction2.0进行MS日志记录,该函数在本地和Azure下工作,现在有两个问题:

1)通过构造函数抛出用于注入依赖项的异常,该构造函数以前一直在工作。

2)日志记录不再写入文本文件,而文本文件以前是工作的。

下面是演示上述问题1的示例代码。

1)通过构造函数引发的依赖注入异常

除了下面的示例代码之外:

代码语言:javascript
复制
2019-03-15T01:28:45.311 [Error] Executed 'Test' (Failed, Id=6bdb0e8e-2353-4ed2-83ce-2a5288fd124d)
System.InvalidOperationException : Unable to resolve service for type 'ICar' while attempting to activate 'TestFunctions'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)
   at lambda_method(Closure ,IServiceProvider ,Object[] )
   at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IServiceProvider serviceProvider) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs : 42
   at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs : 32
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.<>c__DisplayClass1_1.<.ctor>b__0(IFunctionInstanceEx i) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs : 20
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.Create(IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs : 26
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance(IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs : 44
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ParameterHelper.Initialize() at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 845
   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsyncCore(IFunctionInstanceEx functionInstance,CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 116

示例代码如下:

代码语言:javascript
复制
 public class TestFunctions
    {
        private readonly ICar _car;
        private readonly ILogger _log;
        private const string RouteTemplate = "";

        public TestFunctions(
            ICar car, 
            ILoggerFactory loggerFactor)
        {
            _car = car;
            _log = loggerFactor.CreateLogger<TestFunctions>();

        }

        [FunctionName(nameof(Test))]
        public IActionResult Test(
            [HttpTrigger(AuthorizationLevel.Anonymous,"Get","post", Route = RouteTemplate + nameof(Test))]
            HttpRequest httpRequest, ExecutionContext context)
        {
            _log.LogInformation($"{nameof(Test)} started processing the request.");
            return new ContentResult { StatusCode = 200, Content = $"OK" };
        }

    }

    public interface ICar
    {
        string Name { get; }
    }

    public class Car : ICar
    {
        public string Name { get => "car name"; }
    }

[assembly: WebJobsStartup(typeof(WebJobsStartup))]
    public class WebJobsStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddSingleton<ICar, Car>();
            builder.Services.AddLogging();            


        }
    }

host.json

代码语言:javascript
复制
{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": "api/v1"
    }
  },
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information"
    }

  }
}

2)日志记录不再写入文本文件

当通过构造函数TestFunctions进行依赖注入时,将调用Test方法。但是,Test方法中的日志记录并不是将内容写入文件。

同样,DI和日志之前都在工作。

代码语言:javascript
复制
 public TestFunctions(
            //ICar car, 
            ILoggerFactory loggerFactor)
        {}

LogInformation内的Test方法

代码语言:javascript
复制
_log.LogInformation($"{nameof(Test)} started processing the request.");

Test方法中的日志记录并不是将内容写入文件。

代码语言:javascript
复制
2019-03-15T01:34:56.360 [Information] Initializing Host. 2019-03-15T01:34:56.371 [Information] Host initialization: ConsecutiveErrors=0, StartupCount=1 2019-03-15T01:34:56.440 [Information] ApplicationInsightsLoggerOptions {   "SamplingSettings": {
    "EvaluationInterval": "00:00:15",
    "InitialSamplingPercentage": 100.0,
    "MaxSamplingPercentage": 100.0,
    "MaxTelemetryItemsPerSecond": 5.0,
    "MinSamplingPercentage": 0.1,
    "MovingAverageRatio": 0.25,
    "SamplingPercentageDecreaseTimeout": "00:02:00",
    "SamplingPercentageIncreaseTimeout": "00:15:00"   },   "SnapshotConfiguration": null } 2019-03-15T01:34:56.441 [Information] LoggerFilterOptions {   "MinLevel": "None",   "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": "Information",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider",
      "CategoryName": null,
      "LogLevel": "Trace",
      "Filter": null
    }   ] } 2019-03-15T01:34:56.441 [Information] LoggerFilterOptions {   "MinLevel": "None",   "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": "Information",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider",
      "CategoryName": null,
      "LogLevel": "Trace",
      "Filter": null
    }   ] } 
2019-03-15T01:34:56.441 [Information] FunctionResultAggregatorOptions
{
  "BatchSize": 1000,
  "FlushTimeout": "00:00:30",
  "IsEnabled": true
}
2019-03-15T01:34:56.441 [Information] SingletonOptions
{
  "LockPeriod": "00:00:15",
  "ListenerLockPeriod": "00:00:15",
  "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
  "LockAcquisitionPollingInterval": "00:00:05",
  "ListenerLockRecoveryPollingInterval": "00:01:00"
}
2019-03-15T01:34:56.448 [Information] Starting JobHost
2019-03-15T01:34:56.451 [Information] Starting Host (HostId=My-azureportal, InstanceId=id, Version=2.0.12353.0, ProcessId=5772, AppDomainId=1, InDebugMode=True, InDiagnosticMode=False, FunctionsExtensionVersion=~2)
2019-03-15T01:34:56.488 [Information] Loading functions metadata
2019-03-15T01:34:56.645 [Information] 1 functions loaded
2019-03-15T01:34:56.810 [Information] Generating 1 job function(s)
2019-03-15T01:34:56.868 [Information] Found the following functions:
TestFunctions.Test
2019-03-15T01:34:56.868 [Information] Host initialized (410ms)
2019-03-15T01:34:56.877 [Information] Host started (418ms)
2019-03-15T01:34:56.877 [Information] Job host started
2019-03-15T01:34:57.324 [Information] Executing 'Test' (Reason='This function was programmatically called via the host APIs.', Id=my id)
2019-03-15T01:34:57.370 [Information] Executed 'Test' (Succeeded, Id=my id)
2019-03-15T01:35:02.019 [Information] Host lock lease acquired by instance ID 'id'.

VS 2017

Azure函数2.x

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-16 16:35:19

若要修复本地Azure函数的运行时问题,请参见Setting Azure Functions Runtime Version for local dev with Visual Studio 2017

溶液

当前版本的Azure函数运行时2.0.12353导致了这个问题。

https://github.com/Azure/azure-functions-host/releases

其他版本如2.0.12342.0有效

在“函数应用程序设置”下,设置如下

代码语言:javascript
复制
FUNCTIONS_EXTENSION_VERSION = 2.0.12342.0
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55174543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档