Application Insights不会收集ILogger错误日志。
创始人
2024-09-11 01:30:26
0

在使用Application Insights时,ILogger错误日志是默认情况下不会被Application Insights自动收集的。但是,我们可以通过自定义代码来实现将ILogger错误日志发送到Application Insights。

下面是一个示例代码,演示了如何将ILogger错误日志发送到Application Insights:

  1. 首先,确保你的项目中已经添加了Microsoft.ApplicationInsights.AspNetCore包。

  2. 在Startup.cs文件中,添加以下代码:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration); // 添加Application Insights服务

        // 注册自定义ILogger提供程序
        services.AddSingleton();

        services.AddControllersWithViews();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...

        app.UseDeveloperExceptionPage();

        // ...
    }
}
  1. 创建一个自定义的ILoggerProvider实现,用于将ILogger错误日志发送到Application Insights:
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Logging;

public class ApplicationInsightsLoggerProvider : ILoggerProvider
{
    private readonly TelemetryClient _telemetryClient;

    public ApplicationInsightsLoggerProvider(TelemetryConfiguration configuration)
    {
        _telemetryClient = new TelemetryClient(configuration);
    }

    public ILogger CreateLogger(string categoryName)
    {
        return new ApplicationInsightsLogger(_telemetryClient);
    }

    public void Dispose()
    {
        // 执行清理操作(如果需要的话)
    }
}

public class ApplicationInsightsLogger : ILogger
{
    private readonly TelemetryClient _telemetryClient;

    public ApplicationInsightsLogger(TelemetryClient telemetryClient)
    {
        _telemetryClient = telemetryClient;
    }

    public IDisposable BeginScope(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true; // 收集所有日志级别的日志
    }

    public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)
    {
        if (exception != null)
        {
            var logEntry = new TraceTelemetry(formatter(state, exception), logLevel.ToSeverityLevel());
            _telemetryClient.TrackTrace(logEntry);
        }
        else
        {
            var logEntry = new TraceTelemetry(formatter(state, exception), logLevel.ToSeverityLevel());
            _telemetryClient.TrackTrace(logEntry);
        }
    }
}

public static class LogLevelExtensions
{
    public static SeverityLevel ToSeverityLevel(this LogLevel logLevel)
    {
        switch (logLevel)
        {
            case LogLevel.Trace:
                return SeverityLevel.Verbose;
            case LogLevel.Debug:
                return SeverityLevel.Verbose;
            case LogLevel.Information:
                return SeverityLevel.Information;
            case LogLevel.Warning:
                return SeverityLevel.Warning;
            case LogLevel.Error:
                return SeverityLevel.Error;
            case LogLevel.Critical:
                return SeverityLevel.Critical;
            default:
                return SeverityLevel.Information;
        }
    }
}

通过以上步骤,Application Insights将会收集并记录ILogger错误日志。在Application Insights的仪表板中,你将能够查看和分析这些错误日志。

相关内容

热门资讯

透明透视!小程序广东雀神智能插... 透明透视!小程序广东雀神智能插件安装,wepokerplus辅助-原先有挂开挂辅助器;无需打开直接搜...
透视游戏!微信小程序微乐辅助免... 您好:微信小程序微乐辅助免费这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
透明工具!温州茶苑手机版辅助器... 透明工具!温州茶苑手机版辅助器,poker world辅助器-一向有挂开挂辅助软件这是一款可以让一直...
透视苹果版!边锋小程序跑的快辅... 【亲,边锋小程序跑的快辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款边锋小程序跑的快辅助中打...
透视玄学!打两圈兴化辅助,we... 透视玄学!打两圈兴化辅助,wepoker破解游戏盒子-固有有挂开挂辅助插件这是一款可以让一直输的玩家...
透视存在!榆林打锅子辅助器,w... 透视存在!榆林打锅子辅助器,wejoker辅助软件视频-起初有挂开挂辅助软件;无需打开直接搜索加薇1...
透明新版!传送五激k辅助靠谱吗... 透明新版!传送五激k辅助靠谱吗,wepoker有没有插件-最初有挂开挂辅助软件这是一款可以让一直输的...
透视辅助!神途免费辅助软件,h... 透视辅助!神途免费辅助软件,hhpoker透视工具-从来有挂开挂辅助挂;无需打开直接搜索薇:1367...
透明新版!钱柜麻将辅助器,we... 透明新版!钱柜麻将辅助器,wepoker透视脚本下载-原先有挂开挂辅助平台;无需打开直接搜索薇:13...
透视安卓版!心悦填大坑辅助方法... 透视安卓版!心悦填大坑辅助方法,德州真人透视脚本-起初有挂开挂辅助器;亲,心悦填大坑辅助方法这款游戏...