ILogger not Injected in Durable Functions v2.0

核能气质少年 提交于 2019-12-11 04:46:42

问题


At the moment I'm trying to add an ILogger or ILogger<> to a Azure Durable Function so as to use logging in Activity Functions.

Logging in the Orchestration Function works fine and is injected in the method itself, but attempts at constructor injection for ILogger always results in a Null Exception.

builder.Services.AddLogging();

The above does not seem to work when added to the Startup file (Bootstrapper) and neither does variations on:

builder.Services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));

Anyone solved this?


回答1:


Remove either of these lines from your Startup file:

builder.Services.AddLogging();
builder.Services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));

Then, wherever you are injecting your ILogger, add the type that your logger is being injected into using ILogger<T> i.e:

public class Function1
{
    private readonly ILogger _logger;

    public Function1(ILogger<Function1> logger)
    {
        _logger = logger;
    }

    [FunctionName("Function1")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}


来源:https://stackoverflow.com/questions/58752340/ilogger-not-injected-in-durable-functions-v2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!