Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using serilog with azure application insights and .Net core

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

like image 717
Bunty Choudhary Avatar asked May 20 '20 07:05

Bunty Choudhary


People also ask

Does Serilog support .NET Core?

Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets.

What is Serilog in .NET Core?

Serilog is a . NET library that provides diagnostic logging to files, the console, and almost everywhere you would like. Serilog can be used in classic . NET Framework applications and for applications running on the latest and greatest .

How do I log exceptions in application insights?

Diagnose exceptions using Visual StudioOpen the Application Insights Search telemetry window in Visual Studio. While debugging, select the Application Insights dropdown box. Select an exception report to show its stack trace. To open the relevant code file, select a line reference in the stack trace.


1 Answers

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:

enter image description here

The test result:

enter image description here

like image 151
Ivan Yang Avatar answered Oct 19 '22 02:10

Ivan Yang