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