简体   繁体   English

如何使用Microsoft.Extensions.Log和Application Insights记录事件和指标

[英]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.

相关问题 如何从库代码使用 Microsoft.Extensions.Logging? - How use Microsoft.Extensions.Logging from Library code? c# 内核 Microsoft.Extensions.Logging 和日志配置 - c# core Microsoft.Extensions.Logging and Log Config 如何使用LoggerFactory和Microsoft.Extensions.Logging进行.NET Core Console使用C#进行日志记录 - How to Use LoggerFactory and Microsoft.Extensions.Logging for .NET Core Console Logging With C# 如何从 log4net 切换到 Microsoft.Extensions.Logging 多个库 - How to switch from log4net to Microsoft.Extensions.Logging over multiple libraries 如何使用 Microsoft.Extensions.Logging 将 NLog 中的自定义字段记录到数据库? - How do I log a custom field in NLog to database using Microsoft.Extensions.Logging? 如何向 Microsoft.Extensions.Logging 事件添加横切信息? - How do I add cross-cutting information to Microsoft.Extensions.Logging events? 如何使用 Microsoft.Extensions.Logging 配置和查看日志记录 - How to configure and view logging using Microsoft.Extensions.Logging 如何获取 Microsoft.Extensions.Logging<T> 在使用 Serilog 和 AutoFac 的控制台应用程序中? - How to get Microsoft.Extensions.Logging<T> in console application using Serilog and AutoFac? Microsoft.Extensions.Logging - LogError 未记录异常 - Microsoft.Extensions.Logging - LogError is not logging exception 如何在控制台中格式化日志输出?(Microsoft.Extensions.Logging) - How to format the output of logs in the console?(Microsoft.Extensions.Logging)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM