Reputation: 2019
I've trying to log with Serilog to console and Azure application insights from a dotnet 6.0 console application. I'm loading the config from an appsettings.json file.
However, on startup, i get the exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in Serilog.Settings.Configuration.dll: 'Type Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights was not found.'
at Serilog.Settings.Configuration.StringArgumentValue.ConvertTo(Type toType, ResolutionContext resolutionContext)
at Serilog.Settings.Configuration.ConfigurationReader.<>c__DisplayClass21_2.<CallConfigurationMethods>b__3(<>f__AnonymousType9`2 <>h__TransparentIdentifier0)
at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
at System.Linq.Enumerable.SelectListPartitionIterator`2.ToList()
at Serilog.Settings.Configuration.ConfigurationReader.CallConfigurationMethods(ILookup`2 methods, IList`1 configurationMethods, Object receiver)
at Serilog.Settings.Configuration.ConfigurationReader.Configure(LoggerConfiguration loggerConfiguration)
at Serilog.Configuration.LoggerSettingsConfiguration.Settings(ILoggerSettings settings)
I've picked the code up from various examples on the Serilog website and elsewhere. Can anyone see what the problem is with my code?
I have a sample program here that builds and demo's the problem being encountered:
https://github.com/ossentoo/testlogger
Program.cs
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
IServiceProvider _serviceProvider;
ILogger<Program> _logger;
Console.WriteLine("Hello, World!");
var config = InitConfiguration();
IConfiguration InitConfiguration()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
var services = new ServiceCollection();
services.AddLogging(configure => configure.AddSerilog())
.AddTransient<Program>();
services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Debug);
_serviceProvider = services.BuildServiceProvider();
_logger = _serviceProvider.GetService<ILogger<Program>>();
_logger.LogInformation("Logger initialized!");
var loggerFactory = _serviceProvider.GetService<ILoggerFactory>();
_logger = loggerFactory.CreateLogger<Program>();
return configuration;
}
appsettings.json
https://raw.githubusercontent.com/ossentoo/testlogger/main/appsettings.json
Please feel free to fork the code and issue a change and a pull request.
thanks
Upvotes: 4
Views: 1845
Reputation: 1
I had the same problem. If you use .net6, deinstall NuGet-Package Serilog.Sinks.ApplicationInsights 4.0.0 and install Serilog.Sinks.ApplicationInsights 3.1.0. At least my problem is solved.
Upvotes: 0
Reputation: 2019
Turns out the appsettings.json file typename for Serilog telemetryConverter has changed from:
Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights
to
Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights
Upvotes: 6