Application Insights 不使用 Azure Functions C# 进行记录。命名空间似乎是问题所在
Application Insights not logging with Azure Functions C#. Namespace seems the problem
我们的函数将所有逻辑委托给另一个 class“CreateHandler”,我们在其中通过 DI 使用 ILogger。该函数正确记录,但 CreateHandler 没有。我们在本地验证它不会登录到控制台,除非我们将 class 命名空间更改为以“Function”开头的名称。这意味着 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>();
}
}
}
函数:
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"
}
}
host.json:
{
"version": "2.0",
}
我之前在github中注意到这个问题(如果命名空间不是以Function开头),但目前找不到它。
解决方法是,如果命名空间不是以Function开头,则在host.json
中添加完整的namespace+class
名称。如下所示:
{
"version": "2.0",
"logging": {
"logLevel": {
"ExampleProject.Functions.Handlers.CreateHandler": "Information"
}
}
}
或者一般的方式:
"logLevel": { "Default": "Information" }
我们的函数将所有逻辑委托给另一个 class“CreateHandler”,我们在其中通过 DI 使用 ILogger。该函数正确记录,但 CreateHandler 没有。我们在本地验证它不会登录到控制台,除非我们将 class 命名空间更改为以“Function”开头的名称。这意味着 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>();
}
}
}
函数:
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"
}
}
host.json:
{
"version": "2.0",
}
我之前在github中注意到这个问题(如果命名空间不是以Function开头),但目前找不到它。
解决方法是,如果命名空间不是以Function开头,则在host.json
中添加完整的namespace+class
名称。如下所示:
{
"version": "2.0",
"logging": {
"logLevel": {
"ExampleProject.Functions.Handlers.CreateHandler": "Information"
}
}
}
或者一般的方式:
"logLevel": { "Default": "Information" }