20

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

2
  • What is the problem? You've linked to the Serilog application insights sink which is exactly what you need to add to your project. I speak from experience when I say it works Commented May 20, 2020 at 8:33
  • @pinkfloydx33 How can I linked? Commented May 20, 2020 at 8:40

2 Answers 2

31

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{ ConnectionString = "xxxxxxxxx" },TelemetryConverter.Traces)
             );                
}

In controller.cs:

enter image description here

The test result:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

How do you configure application insights settings like sampling configuration?
Where the instrumentation key comes from?
@LesairValmont, if you have an Application Insights created, then please go to azure portal -> your Application Insights -> Overview page: then you can find Instrumentation Key there.
Mine is actually working but... any ideas on why isn't the "live telemetry" actually working?
I think you forgot to add how appsettings.json looks for serilog
|
-2

Here is information how to setup app configuration for asp net core, but it for v6.x

https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core?tabs=netcorenew%2Cnetcore6

appsettings.json (example)

{
"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "Copy connection string from Application Insights Resource Overview"
  }
}

2 Comments

Does not answer the part regarding Serilog in the question
The solution does not mention serilog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.