question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serilog.Settings.Configuration is not working when it is referenced as package

See original GitHub issue

I am getting really strange behaviour trying to configure Serilog using appsettings.json.

It only works when I add Serilog.Settings.Configuration to my project using Add Project Reference. When Serilog.Settings.Configuration (3.2.0) is installed as package I am getting Exception:

System.InvalidOperationException: 'Cannot create instance of type 'Serilog.Formatting.ITextFormatter' because it is either abstract or an interface.'

Here is code example that I am using to parse configuration:

    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
        .Build();

    var logger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();

Here is config that I am using:

  "Serilog": {
    "MinimumLevel": {
      "Override": {
        "Microsoft": "Debug",
        "System": "Debug"
      }
    },
    "Enrich": [ "FromLogContext" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": {
            "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
            "template": "[{@t:yyyy-MM-dd HH:mm:ss.fff} {@l:u3}] {#if CorrelationId is not null}{CorrelationId} | {#end}{@m:lj}\n{@x}"
          }
        }
      }
    ]
  }

Here is an example of not working project file (Serilog.Settings.Configuration is PackageReference):

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>0718b2b6-70cb-4a9b-9c43-30625d246f61</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <AssemblyName>SomeName</AssemblyName>
    <RootNamespace>SomeNamespace</RootNamespace>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Expressions" Version="3.2.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
  </ItemGroup>
    
</Project>

Here is an example of working project file (Serilog.Settings.Configuration is ProjectReference):

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>0718b2b6-70cb-4a9b-9c43-30625d246f61</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <AssemblyName>SomeName</AssemblyName>
    <RootNamespace>SomeNamespace</RootNamespace>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Expressions" Version="3.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\..\serilog-settings-configuration-dev\src\Serilog.Settings.Configuration\Serilog.Settings.Configuration.csproj" />
  </ItemGroup>

</Project>

What am I missing? It looks like Serilog.Settings.Configuration is ignored when it is referenced as package, right? How can I fix that?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
skomis-mmcommented, Oct 6, 2021

Hi @Igor-Zlatomrezhev , please use 3.3.0-dev version (see changes log) of the package until stable release

1reaction
nblumhardtcommented, Oct 7, 2021

Thanks @Igor-Zlatomrezhev 👍

@skomis-mm - I’ll hit the button 😎

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Serilog.Settings.Configuration is not working when it is ...
What am I missing? It looks like Serilog.Settings.Configuration is ignored when it is referenced as package, right? How can I fix that?
Read more >
'LoggerSettingsConfiguration' does not contain a definition ...
I get the error you describe if I add a package reference to Serilog but without package reference to Serilog.Settings.Configuration . So, in...
Read more >
Serilog.Settings.Configuration 7.0.0
Version Downloads Last updated 7.0.0 4,452,610 3 months ago 7.0.0‑dev‑00538 77 14 days ago 7.0.0‑dev‑00535 101 a month ago
Read more >
Serilog ConfigurationBuilder not found on .NET Framework ...
After doing that, clearing my packages, and reinstalling dependencies, now I'm missing ConfigurationBuilder from the NuGet package Serilog.
Read more >
Setting up Serilog in ASP.NET Core - Detailed Beginner ...
This article covers the implementation of Serilog in ASP.NET Core which provides structured logging that is easier to be read by programs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found