8

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?

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 1
    Have you checked out [official README](https://github.com/serilog/serilog-sinks-applicationinsights/blob/master/README.md)? They have some tips regarding flushing/persistence of logged events. Also, are you sure these traces don't end up in 'Custom events' section? – orhtej2 Feb 22 '18 at 08:56
  • 1
    yes i read that, i dont see even a single log in app insights – Sajeetharan Feb 22 '18 at 08:58
  • Same prob. Is it going to be fixed? – activebiz May 25 '18 at 16:39

2 Answers2

2

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
oleksa
  • 3,688
  • 1
  • 29
  • 54
1

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/

emp
  • 4,926
  • 2
  • 38
  • 50