[英]How to use Microsoft.Extensions.Logging with Application Insights to log events and metrics
The ILogger interface has just one Log method: ILogger接口只有一个Log方法:
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);
Application insights gives me a rich api like this: 应用程序见解为我提供了丰富的API,例如:
TrackMetric(string name, double value, IDictionary<string, string> properties = null);
TrackException(Exception exception, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null);
TrackTrace(string message, SeverityLevel severityLevel, IDictionary<string, string> properties);
How should I implement a custom ILogger and still have all the features of application insights? 如何实现自定义ILogger并仍然具有应用程序见解的所有功能? One way I can think of is to define different
TState
types like below: 我能想到的一种方法是定义不同的
TState
类型,如下所示:
class Trace
{
public string Message {get;set;};
public IDictionary<string, string> Properties;
public Trace(string message, IDictionary<string, string> properties)
{
this.Message = message;
this.Properties = properties;
}
}
then when I need to trace, I do something like: 然后当我需要跟踪时,我会执行以下操作:
logger.Log<Trace>(LogLevel.Information, eventId, new Trace("sample trace",null));
In this way I switch which trackXXX
method I used in the Log<TState>
implementation based on the type of TSTate
(I create types for Exception, Metric and Event). 通过这种方式,我根据
TSTate
的类型(我为Exception,Metric和Event创建类型)来切换在Log<TState>
实现中使用的trackXXX
方法。 But this looks too complicated to write a simple trace, any other ways I am missing? 但这看起来太复杂了,无法编写简单的跟踪记录,还有其他我所缺少的方法吗?
When you are using Application Insights SDK for ASP.NET Core and enable Application Insights, internal ILogger implementation is created. 在将Application Insights SDK用于ASP.NET Core并启用Application Insights时,将创建内部ILogger实施。 You can see here how it works.
您可以在此处查看其工作方式。
So to answer your question, if you are using AI SDK for ASP.NET Core, you shouldn't need to implement your own ILogger. 因此,为了回答您的问题,如果您使用AI SDK for ASP.NET Core,则无需实现自己的ILogger。 If you must however, you can use the linked implementation as example.
但是,如果必须,可以使用链接的实现作为示例。
Here's the ILogger implementation for App Insights: https://github.com/Azure/azure-webjobs-sdk/blob/5140983cb163b20bf6af60afccebb705bc80ad80/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs 这是App Insights的ILogger实现: https : //github.com/Azure/azure-webjobs-sdk/blob/5140983cb163b20bf6af60afccebb705bc80ad80/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs
You could do roughly the same thing, wrapping the TelemetryClient, but adding support for events and other things like TrackDependency, which would get called from: 您可以做大致相同的事情,包装TelemetryClient,但是增加对事件和其他诸如TrackDependency之类的支持,这可以从以下调用:
Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)
日志(LogLevel logLevel,EventId,eventId,TState状态,Exception异常,Func格式化程序)
based on the value of EventId. 基于EventId的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.