4

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

2
  • 1
    The best stack overflow answers are the ones where the answerer fixes your but with a pull request? 🤪 Commented Aug 13, 2022 at 20:11
  • how else do you suggest they fix it? At least a pull request shows accurately what needs to change. Else they can just post the answer here. Either works. Commented Aug 15, 2022 at 3:20

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.