将 serilog 与 azure 应用程序洞察和 .Net 核心结合使用 [英] Using serilog with azure application insights and .Net core

查看:76
本文介绍了将 serilog 与 azure 应用程序洞察和 .Net 核心结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在直接使用 azure 应用程序洞察进行日志记录,如此链接中所述

测试结果:

Currently, I am using azure application insights directly for logging as given in this link Use latest version of Application Insight with .net core API and everything is working fine.

But I need to use the serilog for logging now with the help of azure application insight. Even I do some R&D about serilog (https://github.com/serilog/serilog-sinks-applicationinsights). But didn't get any idea. Could you please suggest me that how can we achieve that with the .Net core 3.0

解决方案

Please follow the steps below:

First, in stall the following packages:

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

In 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)
             );                
}

In controller.cs:

The test result:

这篇关于将 serilog 与 azure 应用程序洞察和 .Net 核心结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆