Closed CarlosOnline closed 3 years ago
Host json issue
@CarlosOnline What was your issue exactly? I might have the same thing on my side but can't figure why it happens other than being on version 2.17.0 of Application Insights
I'm having same issue, what did you do @CarlosOnline to resolve?
I sort of got it working. If I had to do it again, I wouldn't use Serilog for .net core applications. There is no documentation for .net core console apps that actually works. Lots of documentation on web pointing to different solutions but really nothing works right.
/// <summary>
/// Configure Serilog logging properly for Application Insights.
/// NOTE: appsettings.json requires proper entries as well.
/// </summary>
/// <param name="hostBuilder">The Microsoft.Extensions.Hosting.IHostBuilder to configure.</param>
/// <param name="configFiles">Additional config files.</param>
/// <returns>The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.</returns>
public static IHostBuilder UseSerilogWithApplicationInsights(this IHostBuilder hostBuilder, string[] configFiles)
{
CreateLogger(configFiles);
return hostBuilder.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.AddSerilog(Log.Logger, dispose: true);
});
}
/// <summary>
/// Create default logger.
/// </summary>
/// <param name="configFiles">Additional config files.</param>
/// <returns>The created logger.</returns>
public static ILogger CreateLogger(string[] configFiles)
{
var configBuilder = new ConfigurationBuilder()
.AddJsonFile("serilogconfig.json");
foreach (var configFile in configFiles)
{
configBuilder.AddJsonFile(configFile, true);
}
var config = configBuilder.Build();
var applicationInsightsKey = config.GetValue<string>("APPINSIGHTS_INSTRUMENTATIONKEY");
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.WriteTo.ApplicationInsights(applicationInsightsKey, new SerilogApplicationInsightsConverter())
.CreateLogger();
return Log.Logger;
}
appsettings.json
{
"Logging": {
// Set Application Insights logging levels
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
},
// Prevent duplicate logging using default logger. - Don't know why this happens.
"LogLevel": {
"Default": "None",
"Microsoft": "None",
"Microsoft.Hosting.Lifetime": "None"
}
},
Application Insights works, configured through
services.AddApplicationInsightsTelemetryWorkerService();
. See app insights flowing.telemetryClient.TrackTrace
("Shows up in application insights") works.this.logger.LogInformation
("Doesn't show up in Application Insights")Debug out shows that App Insights is unconfigured.
Any idea why Serilog is not finding the configured Application Insights?
Program.cs
csproj:
loggerConfig.json