Azure Functions - 类 中的 ILogger 日志记录
Azure Functions - ILogger Logging across classes
在 Azure 函数中,运行 方法创建了一个 ILogger,在我的主 class 中,我可以使用它来记录错误和信息,但是,我正在使用助手 classes 来运行 我的图表 API 和 Cosmos DB 查询。
在单独的 classes 中记录错误和信息的最佳方式是什么,我是否应该在所有 classes 或方法中传递日志?还是有更好的方法让所有 classes 在 运行 时间都可以使用 ILogger?
public class AzureFunction
{
public Task Run([TimerTrigger("0 0 1 * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation("This message logs correctly");
GraphHelper graphHelper = new GraphHelper();
graphHelper.GetGraphData();
}
}
public class GraphHelper
{
public void GetGraphData()
{
try
{
asyncTask()
}
catch (Exception ex)
{
log.LogInformation($"{ex.Message} - How can I log this without passing ILogger to every class or method?");
}
}
}
正确的方法(恕我直言)是通过依赖注入。
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace FunctionApp1
{
public class HelperClass : IHelperClass
{
private static ILogger<IHelperClass> _logger;
public HelperClass(ILogger<IHelperClass> logger)
{
_logger = logger;
}
public void Dowork()
{
_logger.LogInformation("Dowork: Execution Started");
/* rest of the functionality below
.....
.....
*/
_logger.LogInformation("Dowork: Execution Completed");
}
}
}
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"FunctionApp1.HelperClass": "Information"
}
}
}
帮手class
using System;
using System.Collections.Generic;
using System.Text;
namespace FunctionApp1
{
public interface IHelperClass
{
void Dowork();
}
}
主要函数
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp1
{
public class MainFunction // Ensure class is not static (which comes by default)
{
private IHelperClass _helper;
public MainFunction(IHelperClass helper)
{
_helper = helper;
}
[FunctionName("MainFunction")]
public void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
// call helper
_helper.Dowork();
}
}
}
Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection; // install nuget - "Microsoft.Azure.Functions.Extensions"
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IHelperClass,HelperClass>();
}
}
}
可以在 https://gist.github.com/nareshnagpal06/82c6b4df2a987087425c32adb58312c2
上找到完整示例
在 Azure 函数中,运行 方法创建了一个 ILogger,在我的主 class 中,我可以使用它来记录错误和信息,但是,我正在使用助手 classes 来运行 我的图表 API 和 Cosmos DB 查询。
在单独的 classes 中记录错误和信息的最佳方式是什么,我是否应该在所有 classes 或方法中传递日志?还是有更好的方法让所有 classes 在 运行 时间都可以使用 ILogger?
public class AzureFunction
{
public Task Run([TimerTrigger("0 0 1 * * *")] TimerInfo myTimer, ILogger log)
{
log.LogInformation("This message logs correctly");
GraphHelper graphHelper = new GraphHelper();
graphHelper.GetGraphData();
}
}
public class GraphHelper
{
public void GetGraphData()
{
try
{
asyncTask()
}
catch (Exception ex)
{
log.LogInformation($"{ex.Message} - How can I log this without passing ILogger to every class or method?");
}
}
}
正确的方法(恕我直言)是通过依赖注入。
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace FunctionApp1
{
public class HelperClass : IHelperClass
{
private static ILogger<IHelperClass> _logger;
public HelperClass(ILogger<IHelperClass> logger)
{
_logger = logger;
}
public void Dowork()
{
_logger.LogInformation("Dowork: Execution Started");
/* rest of the functionality below
.....
.....
*/
_logger.LogInformation("Dowork: Execution Completed");
}
}
}
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"FunctionApp1.HelperClass": "Information"
}
}
}
帮手class
using System;
using System.Collections.Generic;
using System.Text;
namespace FunctionApp1
{
public interface IHelperClass
{
void Dowork();
}
}
主要函数
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp1
{
public class MainFunction // Ensure class is not static (which comes by default)
{
private IHelperClass _helper;
public MainFunction(IHelperClass helper)
{
_helper = helper;
}
[FunctionName("MainFunction")]
public void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
// call helper
_helper.Dowork();
}
}
}
Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection; // install nuget - "Microsoft.Azure.Functions.Extensions"
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]
namespace FunctionApp1
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IHelperClass,HelperClass>();
}
}
}
可以在 https://gist.github.com/nareshnagpal06/82c6b4df2a987087425c32adb58312c2
上找到完整示例