【发布时间】: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 数据,包括 p1 和 p2 的自定义维度 - 是的。
调用这个:
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 属性。
所以
CustomLogEntry是ILogger.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.Log 的TState 参数有任何联系(在本例中为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