【问题标题】:Extending ApplicationInsights logging telemetry扩展 ApplicationInsights 日志记录遥测
【发布时间】:2021-10-04 04:34:46
【问题描述】:

我正在将 net4.8 应用程序转移到 net50,其中一部分工作是将日志记录从 Microsoft.EnterpriseLibrary 重构为 ILogger。我想将现有的结构化日志数据传递给 ApplicationInsights。

调用这个:

logger.LogInformation("My message called with {p1} and {p2}", p1, p2);

生成 Application Insights 数据,包括 p1p2 的自定义维度 - 是的。

调用这个:

logger.Log(LogLevel.Information, 
  new EventId(127, "CustomLog"), 
  new CustomLogEntry(), 
  null, 
  (entry, ex) => { return "Testing custom"; });

地点:

public class CustomLogEntry
{
    public int Id { get; set; } = 128;
    public string Name { get; set; } = Guid.NewGuid().ToString();
    public DateTimeOffset Expiration { get; set; } = DateTimeOffset.UtcNow.AddDays(1);
}

在 Application Insights 中不会产生任何 CustomLogEntry 属性。

所以CustomLogEntryILogger.Log接口的TState参数。这只是一个模拟;我真正想要利用的是 Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry 的等价物。

我的下一步是设置一个自定义ITelemetryInitializer 以将CustomLogEntry 属性添加到我的ITelemetry

public class EventTelemetryInitializer : ITelemetryInitializer
{

    public EventTelemetryInitializer()
    { }

    public void Initialize(ITelemetry telemetry)
    {
        var trace = telemetry as TraceTelemetry;
        if (trace == null) return;
        trace.Properties.Add("Custom", "something");
            
    }
}

当我在EventTelemetryInitializer.Initialize 方法中调试时,telemetry 参数的任何部分似乎都没有与ILogger.LogTState 参数有任何联系(在本例中为CustomLogEntry)。

是否必须使用格式将自定义维度传递给 Application Insights?例如,而不是:

logger.Log(LogLevel.Information, new EventId(127, "CustomLog"), new CustomLogEntry(), null, (entry, ex) => { return "Testing custom"; });

使用:

var entry = new CustomLogEntry();
logger.LogInformation("Testing custom {Name}, {Expiration}, {Id}", entry.Name, entry.Expiration, entry.Id);

如果是这样,我倾向于为ILogger 发明一些扩展糖,但这感觉就像我在糖上加糖,并且在从 ILogger 到 Application Insight 遥测的映射中遗漏了一些东西。

【问题讨论】:

    标签: azure-application-insights telemetry ilogger


    【解决方案1】:

    当您使用独立包时,TelemetryClient 不会注入到 DI 容器中,因此您需要创建 TelemetryClient 的新实例并使用与记录器提供程序相同的配置,如以下代码所示。这可确保所有自定义遥测以及来自 ILogger 的遥测使用相同的配置。

    public class MyController : ApiController
    {
       // This TelemetryClient can be used to track additional telemetry using TrackXXX() api.
       private readonly TelemetryClient _telemetryClient;
       private readonly ILogger _logger;
    
       public MyController(IOptions<TelemetryConfiguration> options, ILogger<MyController> logger)
       {
            _telemetryClient = new TelemetryClient(options.Value);
            _logger = logger;
       }  
    }
    

    ApplicationInsightsLoggerProvider 捕获 ILogger 日志并从中创建 TraceTelemetry。如果将 Exception 对象传递给 ILogger 上的 Log 方法,则会创建 ExceptionTelemetry 而不是 TraceTelemetry。这些遥测项目可以在与 Application Insights 的任何其他 TraceTelemetry 或 ExceptionTelemetry 相同的位置找到,包括门户、分析或 Visual Studio 本地调试器。

    如果您希望始终发送 TraceTelemetry,请使用此 sn-p:

    builder.AddApplicationInsights(
        options => options.TrackExceptionsAsExceptionTelemetry = false);
    

    你可以参考 Log some additional custom telemetry manuallyRecording custom telemetry with Azure Application Insights

    【讨论】:

    • "ApplicationInsightsLoggerProvider 捕获 ILogger 日志并从中创建 TraceTelemetry。" => 这是在哪里发生的,它可以被覆盖吗?具体来说,这似乎忽略了传递给 ILogger.Log 的 TState 对象,我想将其序列化为自定义维度。
    猜你喜欢
    • 2019-08-25
    • 2013-12-29
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多