Microsoft Extension Framework 将自定义维度添加到记录器而不是每次调用?
Microsoft Extension Framework add custom dimensions to the logger instead of each call?
我有一个 azure 函数:
public void Run([TimerTrigger("0 0 0 * * *", RunOnStartup =true)]TimerInfo myTimer, ILogger log)
{
using (logger.BeginScope(properties))
{
logger.LogTrace(message);
}
}
Dictionary<string, object> _props = new Dictionary<string, object> { { "service", "exchange.rates.procces" } };
如您所见,我通过向 BeginScope 提供字典(属性)来添加自定义属性。有没有办法将字典添加到记录器中,这样我就不必为每次调用都提供字典了?记录器写入 Application Insigths。
要添加自定义维度,您可以使用 ITelemetryInitializer 作为您的 azure 函数。
自己编写ITelemetryInitializer后,需要在azure函数中注册。请参考下面的示例代码:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
//add thie line of code before namespace
[assembly: WebJobsStartup(typeof(FunctionApp2.MyStartup))]
namespace FunctionApp2
{
public class Function1
{
[FunctionName("Function1")]
public void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
log.LogInformation("333 this is a test message...");
}
}
//define your custom ITelemetryInitializer which is used to add custom dimension
internal class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry != null && !telemetry.Context.GlobalProperties.ContainsKey("my_custom_dimen22"))
{
telemetry.Context.GlobalProperties.Add("my_custom_dimen22", "Hello, this is custom dimension for request!!!");
}
}
}
//register your custom ITelemetryInitializer
public class MyStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}
}
}
测试结果:
我有一个 azure 函数:
public void Run([TimerTrigger("0 0 0 * * *", RunOnStartup =true)]TimerInfo myTimer, ILogger log)
{
using (logger.BeginScope(properties))
{
logger.LogTrace(message);
}
}
Dictionary<string, object> _props = new Dictionary<string, object> { { "service", "exchange.rates.procces" } };
如您所见,我通过向 BeginScope 提供字典(属性)来添加自定义属性。有没有办法将字典添加到记录器中,这样我就不必为每次调用都提供字典了?记录器写入 Application Insigths。
要添加自定义维度,您可以使用 ITelemetryInitializer 作为您的 azure 函数。
自己编写ITelemetryInitializer后,需要在azure函数中注册。请参考下面的示例代码:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
//add thie line of code before namespace
[assembly: WebJobsStartup(typeof(FunctionApp2.MyStartup))]
namespace FunctionApp2
{
public class Function1
{
[FunctionName("Function1")]
public void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
log.LogInformation("333 this is a test message...");
}
}
//define your custom ITelemetryInitializer which is used to add custom dimension
internal class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry != null && !telemetry.Context.GlobalProperties.ContainsKey("my_custom_dimen22"))
{
telemetry.Context.GlobalProperties.Add("my_custom_dimen22", "Hello, this is custom dimension for request!!!");
}
}
}
//register your custom ITelemetryInitializer
public class MyStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}
}
}
测试结果: