【问题标题】:Using serilog with azure application insights and .Net core将 serilog 与 Azure 应用程序洞察力和 .Net 核心结合使用
【发布时间】:2020-05-22 15:36:03
【问题描述】:

目前,我直接使用 Azure 应用程序洞察力进行日志记录,如此链接中所示 Use latest version of Application Insight with .net core API 一切正常。

但我现在需要借助 azure application insight 使用 serilog 进行日志记录。甚至我也对 serilog (https://github.com/serilog/serilog-sinks-applicationinsights) 做了一些研发。但没有得到任何想法。您能否建议我如何使用 .Net core 3.0 实现这一目标

【问题讨论】:

  • 有什么问题?您已链接到 Serilog 应用程序洞察接收器,这正是您需要添加到项目中的内容。当我说它有效时,我是根据经验说话
  • @pinkfloydx33 如何链接?

标签: c# azure asp.net-core azure-application-insights serilog


【解决方案1】:

请按照以下步骤操作:

首先,安装以下软件包:

Microsoft.ApplicationInsights.AspNetCore, version 2.14.0

Serilog.AspNetCore, version 3.2.0

Serilog.Sinks.ApplicationInsights, version 3.1.0

Serilog.Settings.Configuration, version 3.1.0

在 Program.cs 中:

public class Program
{     
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
           .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                    .ReadFrom.Configuration(hostingContext.Configuration)
                    .WriteTo.ApplicationInsights(new TelemetryConfiguration{ InstrumentationKey = "xxxxxxxxx" },TelemetryConverter.Traces)
             );                
}

在 controller.cs 中

测试结果

【讨论】:

  • 如何配置采样配置等应用洞察设置?
  • 检测密钥从何而来?
  • @LesairValmont,如果您创建了 Application Insights,请转到 azure 门户 -> 您的 Application Insights -> 概述页面:然后您可以在那里找到 Instrumentation Key。
  • 我的确实在工作,但是......关于为什么“实时遥测”实际上没有工作的任何想法?
  • 我想你忘了添加 appsettings.json 如何查找 serilog
猜你喜欢
  • 2022-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
相关资源
最近更新 更多