Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SeriLog with AppInsights not writing to AppInsights

I am writing logs using Serilog with appinsights on an .NET CORE 2.0 project.

i have configured SeriLog as follows,

var loggerConfiguration = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .Enrich.FromLogContext()
                .Enrich.WithDemystifiedStackTraces();

and writing to appInsights as follows,

 loggerConfiguration = loggerConfiguration.WriteTo.ApplicationInsightsTraces(appInsightsIntrumentationKey, serilogLevel)
.WriteTo.RollingFile(Path.Combine(contentRoot, "Logs/log-{Date}.log"), retainedFileCountLimit: 14);

i see the log generated inside the logs folder, but i dont see anything in appInsights.

what i am doing wrong?

like image 301
Sajeetharan Avatar asked Feb 22 '18 08:02

Sajeetharan


2 Answers

I've tried to do it as documented

    "ApplicationInsights": {
        "InstrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
        "EnableAdaptiveSampling": false,
        "EnablePerformanceCounterCollectionModule": false
    },
    "Serilog": {
        "WriteTo": [
            { "Name": "Console" },
            {
                "Name": "ApplicationInsights",
                "Args": {
                    "instrumentationKey": "7334f234-42c9-4a1f-b35d-4969d48020d4",
                    "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
                }
            }
        ]
    }

it works from both Azure App Service and dev computer

Program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSerilog() // added to embed serilog
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Startup.cs

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
}

sample controller:

    [Route("[controller]")]
    [ApiController]
    public class BadValuesController : ControllerBase
    {
        private readonly ILogger<BadValuesController> _logger;
        public BadValuesController(ILogger<BadValuesController> logger)
        {
            _logger = logger;
            _logger.LogDebug("ctor");
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> GetData()
        {
            _logger.LogWarning("going to raise 6 from local app");
            throw new NotImplementedException();
        }
    }

There is no need to rebuild application since no services.AddApplicationInsightsTelemetry(); is executed. You may just copy dll files to your application and adjust config to turn the AppInsights logging on.

However you do not have TelemetryClient instance if there is no source code modifications. So you have to wait a while before can select app insights log.

I've installed NuGet packages

  • Microsoft.ApplicationInsights.AspNetCore
  • Serilog.AspNetCore
  • Serilog.Enrichers.Environment
  • Serilog.Enrichers.Process
  • Serilog.Sinks.ApplicationInsight
like image 109
oleksa Avatar answered Oct 23 '22 01:10

oleksa


Make sure to install the SDK prior to the Serilog ApplicationInsights sink. If you don't have the SDK, it seems to work but you will end up without any messages in ApplicationInsights.

https://www.nuget.org/packages/Microsoft.ApplicationInsights.AspNetCore/

like image 1
emp Avatar answered Oct 23 '22 01:10

emp