c# - 如何在Azure Functions V2中记录到azure应用程序日志?

标签 c# azure azure-functions

Azure Web 应用程序以及扩展的 Azure Functions 允许将日志消息发送到“诊断日志”配置部分中配置的 Blob 存储。

但是,传递给 C# 函数 (v2) 实现的默认 ILogger 实例会忽略该设置。相反,您似乎需要手动构建一个记录器才能使用此功能,如下所示。请注意,您需要从 NuGet 添加 Microsoft.Extensions.Logging.AzureAppServices 来编译它。

[FunctionName("myfun")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, 
    ILogger log)
{
    log.LogInformation("This does not end up in the app log.");

    var factory = new LoggerFactory().AddAzureWebAppDiagnostics();

    var log2 = factory.CreateLogger("MyFun");
    log2.LogInformation("This *does* end up in the app log, if configured.");
    log2.LogError("Fake error for testing logging functionality (2)");
}

我的问题:是否有更优雅的方法来获取写入配置的应用程序日志 blob 存储的记录器?我本以为传递给函数的 log 已经具有此功能(可能通过观察配置了应用程序日志而触发),但默认情况下它没有,而且我没有看到如何添加它的清晰路径,既不通过配置,也不通过用其他东西替换 ILogger log 参数。

最佳答案

对于Azure Function V2,您可以使用注入(inject)的ILogger将所有日志记录为应用程序见解,然后您可以从azure门户>应用程序见解>转到您的函数应用程序,然后选择分析(日志)>然后您可以使用此查询检索所有日志:

    traces
    where appName == "YourAppName"

对于旧函数 V2,您可以使用注入(inject)的 TraceWriter 并将其与任何记录器集成,以便您可以在与此函数应用程序相关的自动创建的 Blob 存储表中找到日志,并且可以使用 Microsoft Azure 存储资源管理器轻松检查它。

关于c# - 如何在Azure Functions V2中记录到azure应用程序日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538272/

相关文章:

c# - 为移动 Sprite 计时

javascript - JS : Find a look-alike word in String and replace it

azure - 如何防止重复条目进入Azure存储的Delta Lake

Azure Vnet-to-Site 您的 VPN 连接受到影响,因为 S2S VPN 隧道已断开连接

azure - 使用 terraform 设置 functionTimeout

azure - 在 Shell 中使用 URI 将大文件从其他源复制到 Azure Blob

Visual Studio 2017/19 中的 JavaScript Azure Function 支持(不是 VS Code)

c# - 表没有主键

c# - 调用存储过程需要@Id。尽一切可能指定它

c# - "OlampiadEbtedaee.NeedFunction"的类型初始化抛出异常