0

I was trying to do this to configure serilog to write to application insights. it is in app.config file. But it doesn't work. Is there any alternative approach

<add key="serilog:minimum-level" value="Error"/>
<add key="serilog:using:ApplicationInsights" value="Serilog.Sinks.ApplicationInsights" />
<add key="serilog:write-to:ApplicationInsights" value="fxxx104-dd93-xxxx-8601-xxxxxxxxxxx"/>
  • Your question is missing some detail. You haven't shown the code where you initialize the logger. Please provide a [mcve]. Also [check the documentation](https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics#selflog) for how to enable the self-log capability and update your question with the findings. – mason Apr 27 '21 at 22:35
  • If my reply is helpful, please accept it as answer(click on the mark option beside the reply to toggle it from greyed out to fill in.), see https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jason Pan Apr 29 '21 at 06:22

1 Answers1

0

You should add UseSerilog in Program.cs. You use App.config just want the read keys by code, something like ConfigurationManager.AppSettings["key"].

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

Then you can use Log.Information("log details") in yourController.cs to record logs.

Thanks for Ivan's soultion. For more details, you can refer his answer in below post.

Using serilog with azure application insights and .Net core

Jason Pan
  • 15,263
  • 1
  • 14
  • 29