![](/img/trans.png)
[英]Need Clarification : Logging data in to Application Insights from Azure Functions
[英]Application Insights not logging with Azure Functions C#. Namespace seems the problem
我們的 function 將所有邏輯委托給另一個 class “CreateHandler”,我們通過 DI 使用 ILogger。 function 正確記錄但 CreateHandler 沒有。 在本地,我們驗證它不會登錄到控制台,除非我們將 class 命名空間更改為以“功能”開頭的名稱。 這意味着 FunctionR 或 FunctionS 將工作,但 RFunction 或 SFunction 不會。 我們正在處理 class 和服務。 顯然,我們的服務有一個完全不同的命名空間,我們需要保留它,同時記錄。 我們如何在不更改命名空間的情況下制作 class 日志?
CreateHandler class(登錄權):
using ExampleProject.Domain.Entities;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace FunctionAnything
{
public class CreateHandler
{
private readonly ILogger<CreateHandler> _logger;
public CreateHandler(
ILogger<CreateHandler> logger)
{
_logger = logger;
}
public async Task Handle(Car car)
{
_logger.LogInformation($"This is logging properly");
}
}
}
CreateHandler class(不記錄):
using ExampleProject.Domain.Entities;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace ExampleProject.Functions.Handlers
{
public class CreateHandler
{
private readonly ILogger<CreateHandler> _logger;
public CreateHandler(
ILogger<CreateHandler> logger)
{
_logger = logger;
}
public async Task Handle(Car car)
{
_logger.LogInformation($"This is not logging");
}
}
}
啟動:
using ExampleProject.Functions.Handlers;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(ExampleProject.Functions.Startup))]
namespace ExampleProject.Functions
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<CreateHandler>();
}
}
}
Function:
using ExampleProject.Domain.Entities;
using ExampleProject.Functions.Handlers;
using ExampleProject.Service.Constants;
using Microsoft.Azure.WebJobs;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ExampleProject.Functions
{
public class Function
{
private readonly CreateHandler _createHandler;
public Function(
CreateHandler createHandler)
{
_createHandler = createHandler;
}
[FunctionName("Create")]
public async Task Create([QueueTrigger(QueueNames.Create)] Car car)
{
log.LogInformation("I'm logged");
await _createHandler.Handle(car);
}
}
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}
主機.json:
{
"version": "2.0",
}
我之前在 github 中注意到了這個問題(如果命名空間不是以 Function 開頭的),但目前找不到。
解決方案是,如果命名空間不是以 Function 開頭,則應在host.json
中添加完整的namespace+class
名。 如下所示:
{
"version": "2.0",
"logging": {
"logLevel": {
"ExampleProject.Functions.Handlers.CreateHandler": "Information"
}
}
}
或者一般的方式:
"logLevel": { "Default": "Information" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.