Steve
Steve

Reputation: 9551

Serilog doesn't log to Azure App Insights when deployed to an App Service

I have a .Net Framework API and I am using Serilog with the Application Insights sink for logging.

I have configured Serilog with the AppInsights instrumentation key and using ILogger to trigger logging. When I run my API locally the log message show up in my App Insights Transaction search. However, when I deploy the API to an Azure App Service, the logs seem to never make it to App Insights.

I have tried updating the sink to latest, making sure the Instrumentation Key is correct but nothing seems to help.

My logging config:

var logConfig = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .WriteTo.ApplicationInsights(instrumentationKey, new TraceTelemetryConverter());
Log.Logger = logConfig.CreateLogger();

Then I'm using the following to test logging:

Log.Logger.Error("Log.Logger LogTest: Error");

Has anyone else run into this issue?

Upvotes: 1

Views: 1402

Answers (1)

Harshitha
Harshitha

Reputation: 7297

  • After deploying the Application make sure you have enabled the Application Insights tab in Azure webapp.

enter image description here

To Log the message, use the below format

 Log.Information("Log message Home");
  var log = new LoggerConfiguration()
  .WriteTo.ApplicationInsights("YourInstrumentationKey", new TraceTelemetryConverter());
   Log.Logger = logConfig.CreateLogger();

MyAPIController

   private readonly ILogger<HelpController> _logger;
    public HelpController(ILogger<HelpController> logger)
        {
            _logger = logger;
        }
         public ActionResult Index()
        {
            Log.Information("Log message Help Controller");
        }

Output: enter image description here

  • You can also check the traces in ApplicationInsights = > Transaction search enter image description here

enter image description here

Upvotes: 1

Related Questions