[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录
问题描述
在Azure Function代码中,有默认的ILogger对象来记录函数的日志,如果函数引用了一些静态对象,是否有办法使用这个默认的ILogger对象来记录日志呢?
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging; namespace Company.Function
{
public class HttpTrigger1
{
private readonly ILogger _logger; public HttpTrigger1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpTrigger1>();
} [Function("HttpTrigger1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request."); var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8"); response.WriteString("Welcome to Azure Functions!"); return response;
}
}
}
问题解答
可以的,有两种方式来实现:方式一是把_logger对象作为参数传递给静态方法,方式二是自定义一个静态ILogger 对象,然后在静态方法中使用。
方式一:把_logger对象作为参数传递
示例代码:
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
...
//Call Static Fun without _logger
GetLocalStaticFun();
//Call static Fun with _logger
GetLocalStaticFun(_logger);
...
}
public static void GetLocalStaticFun(ILogger _sublogger = null)
{
//TODO Logic process
if (_sublogger != null)
{
_sublogger.LogInformation("this is static fucntion for testing...LogInformation. @2023/10/25");
}
}
测试效果:

方式二:定义静态ILogger对象
示例代码:
static ILoggerFactory _staticLoggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddConsole();
}); static ILogger _staticloger = _staticLoggerFactory.CreateLogger<Function1>(); public static void GetStaticLogFun()
{
_staticloger.LogInformation("Example log message form static class");
_staticloger.LogError("Example error message form static class"); }
注:以此种方式记录的日志,当部署到Azure Function App云服务后,通过在高级工具(kudu)站点查看日志时,与正常的日志不同,在与函数名同名的Folder中,而是在Host Folder中(C:\home\LogFiles\Application\Functions\Host)。
参考资料
ILoggerFactory static create: https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#non-host-console-app
[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录的更多相关文章
- 【Azure Developer】Azure Graph SDK获取用户列表的问题: SDK中GraphServiceClient如何指向中国区的Endpoint:https://microsoftgraph.chinacloudapi.cn/v1.0
问题描述 想通过Java SDK的方式来获取Azure 门户中所列举的用户.一直报错无法正常调用接口,错误信息与AAD登录认证相关,提示tenant not found. 想要实现的目的,通过代码方式 ...
- 【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?
问题描述 通过Azure Logic App(逻辑应用)实现无代码的处理JSON数据.但是如何获取Request Body中的一个属性值呢? 例如:如何来获取以下JSON结构中的 ObjectName ...
- 【Azure Developer】Azure Automation 自动化账号生成的时候怎么生成连接 与证书 (Connection & Certificate)
Azure Automation :The Azure Automation service provides a highly reliable and scalable workflow exec ...
- ENABLE_DDL_LOGGING 参数使用 监控对象的DDL(在alter 日志记录DDL语句)
启用 DDL 日志记录 功能--支持动态调整 alter system set enable_ddl_logging=true; alter system set enable_ddl_logging ...
- (转)MySql中监视增删改查和查看日志记录
转载地址为:http://blog.51cto.com/hades02/1641652 首先在命令行输入 show global variables like '%general%' ,然后出现下面的 ...
- PHP中错误与异常的日志记录用法分析
原文:http://www.jb51.net/article/89548.htm ----------------------------------------------------------- ...
- 【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
问题描述 使用Github Action,通过 Azure/functions-container-action@v1 插件来完成 yaml 文件的配置,并成功部署Function Image 的过程 ...
- 【Azure Application Insights】在Azure Function中启用Application Insights后,如何配置不输出某些日志到AI 的Trace中
问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的tra ...
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...
随机推荐
- postman中js脚本简单用法
1.获取接口相应结果 var jsonData = pm.response.json() 2.设置环境变量 pm.environment.set("variable_key", & ...
- Mysql 8.0前后,实现创建用户,指定用户只能访问指定的数据库中的表
最近在做公司项目的过程中,出现了这样的需求.第三方系统需要将数据写到我们的业务系统中,由于目前这些数据没有实际的使用场景,在讨论下,为简单快捷,选择第二种方案,即不书写接口,第三方系统通过数据库直接将 ...
- github 2fa中国认证及TOTP App
Because of your contributions on GitHub, two-factor authentication will be required for your account ...
- border多层渐变
.content { margin-top: 19px; border-top: 1px dashed rgba(113, 183, 248, 0.6) !important; border-left ...
- 深度学习应用篇-计算机视觉-视频分类[8]:时间偏移模块(TSM)、TimeSformer无卷积视频分类方法、注意力机制
深度学习应用篇-计算机视觉-视频分类[8]:时间偏移模块(TSM).TimeSformer无卷积视频分类方法.注意力机制 1.时间偏移模块(TSM) 视频流的爆炸性增长为以高精度和低成本执行视频理解任 ...
- LyScript 批量搜索反汇编特征
LyScript 插件实现对特定汇编指令片段的批量搜索功能,用户传入一个汇编指令列表,然后循环搜索该列表内的所有指令特征,如果找到了,则返回该指令的内存地址. 插件地址:https://github. ...
- 关于laravel路由无法访问(nginx)
laravel路由无法访问 被这个问题折腾了 好几次了,记录一下希望可以帮到后面的朋友 在laravel路由中配置好了之后无法访问的问题有可能是因为在本地服务配置的问题(我使用的是nginx服务器) ...
- Hadoop-大数据组件版本号查看
1.操作系统 cat /etc/redhat-release ; 2.JDK java -version 3.SCALA scala --version 4.MySQL mysql --versio ...
- Numpy基本使用方法
Numpy基本使用方法 第一节 创建数组 import numpy as np import random # 创建数组 a = [1, 2, 3, 4, 5] a1 = np.array(a) pr ...
- Power BI 13 DAY
分组依据 在PQ下对维度进行汇总,并对其他字段进行不同的计算方式,就需要使用分组依据 将下表中已"店名"为维度对重复店名作排重处理,对"单号"字段进行计数计算, ...