[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 ...
随机推荐
- es6新增的运算符-链判断运算符的诞生[?.]和null的判断运算符??
指数运算符 ** console.log(2 ** 2 ) //4 console.log(2 ** 3 ) //8 console.log(2 ** 4) //16 链判断运算符的诞生(?.) 在实 ...
- 【小测试】golang中数组边界检查的开销大约是1.87%~3.12%
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 对比C/C++, golang等类型安全的语言会在数组访问 ...
- 【一文搞定】Linux、Mac、Windows安装Docker与配置教程!
目录 一.Windows 安装 1.1 安装与启用 Hyper-V 1.2 安装 WSL 1.3 Docker Desktop 官方下载 1.4 安装Docker Desktop 二.MacOS 安装 ...
- windowsbat删除命令
widnwosbat命令 DEL /F /A /Q \?%1 用于删除指定路径下的文件,参数含义如下: /F: Force delete,即强制删除: /A: 用于指定文件属性,A代表存档,D代表目录 ...
- pandas高效读取大文件的探索之路
使用 pandas 进行数据分析时,第一步就是读取文件.在平时学习和练习的过程中,用到的数据量不会太大,所以读取文件的步骤往往会被我们忽视. 然而,在实际场景中,面对十万,百万级别的数据量是家常便饭, ...
- Python 调用Zoomeye搜索接口
钟馗之眼是一个强大的搜索引擎,不同于百度谷歌,它主要收集网络中的主机,服务等信息,国内互联网安全厂商知道创宇开放了他们的海量数据库,对之前沉淀的数据进行了整合.整理,打造了一个名符其实的网络空间搜索引 ...
- django 处理请求
本文基于 django runsever 入口 执行 python manage.py runserver 调用 django.core.management.commands.runserver.C ...
- asp.net 生成word,处理图片,富文本框内容图片处理
//基本导出方法public void Download() { Random rd = new Random(); string fileName = DateTime.Now.ToString(& ...
- 深入浅出Java多线程(六):Java内存模型
引言 大家好,我是你们的老伙计秀才!今天带来的是[深入浅出Java多线程]系列的第六篇内容:Java内存模型.大家觉得有用请点赞,喜欢请关注!秀才在此谢过大家了!!! 在并发编程中,有两个关键问题至关 ...
- [ Skill ] append1, append, nconc, tconc, lconc, cons 效率对比
https://www.cnblogs.com/yeungchie/ 先说结论:cons > tconc, lconc >> nconc > append1, append a ...