问题
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