44

I'm trying to call loggerFactory.AddSerilog(); as per this documentation, but the AddSerilog method is not recognized:

"Error CS1061 'ILoggerFactory' does not contain a definition for 'AddSerilog' and no extension method 'AddSerilog' accepting a first...".

I'm using ASP.NET CORE with the full .NET framework. What am I doing wrong?

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
ashilon
  • 1,791
  • 3
  • 24
  • 41
  • 2
    show your project.json file – Joe Audette Jul 31 '16 at 13:19
  • `"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Serilog": "2.1.0", "Serilog.Sinks.MSSqlServer": "4.0.0"` – ashilon Jul 31 '16 at 13:28
  • That's just part of the file. I can't include all of it because of too many characters. – ashilon Jul 31 '16 at 13:29
  • 1
    OK found it, my bad.... forgot to install package **Serilog.Extensions.Logging**... thanks Joe, your comment helped me in the right direction. – ashilon Jul 31 '16 at 13:33
  • 2
    just for future reference, when I asked you to post project.json, I meant edit your question and add it there, it would have fit there but not in comments – Joe Audette Jul 31 '16 at 15:51
  • Great, thanks a lot Joe, didn't think of that. – ashilon Jul 31 '16 at 18:38

5 Answers5

87

You may forget this following line in project.json

"Serilog.Extensions.Logging": "1.0.0",

See also https://carlos.mendible.com/2016/09/19/step-step-serilog-asp-net-core/

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Teerachai
  • 886
  • 7
  • 3
  • 8
    THANKYOU. This has been driving me insane. NO WHERE is this mentioned. all the tutorials, and git hub, all mention the serilog main dll, and serilog.extensions.logging.file, BUT NO ONE, tells you anywhere that you need this one too. To add insult to injury, there's a weak resference to it in the main lib, so dotnetcore resolves the lib base, but not the method, so there's no indication that you need another lib, in intellisense. I'd up vote a million times if I could. – shawty Feb 05 '18 at 20:01
  • 2
    Carlos has updated his post as of Jan 2019 for .NET Core 2.2: https://carlos.mendible.com/2019/01/14/updated-step-step-serilog-asp-net-core/ – joynoele Aug 29 '19 at 21:06
39

Different circumstance, but same problem. In my case, I was using .Net Core 2.1 and had a NuGet reference to Serilog, but was missing a reference to Serilog.AspNetCore. The issue first manifested as .UserSerilog() not being found for the IWebHostBuilder of my CreateWebHostBuilder static method under Program.cs.

Adding the Serilog.AspNetCore NuGet package to my project solved the problem.

nbrosz
  • 874
  • 12
  • 18
22

The posted answer is correct but I will add that you may want to use the NuGet package manager that way you can get the latest version.

Right click on solution

-> Choose "Manage NuGet packages for solution"

-> type "serilog.extensions.logging" into search box

-> Click on Serilog.Extensions.Logging and press install

You will get a dropdownlist of the different versions you should choose the latest.

Or quicker from Package Manager console verify that Default Project drop-down has your project selected and run

install-package Serilog.Extensions.Logging
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
4

On .NET Core 3.1, within a console app, I simply had to install the serilog.extentions.hosting NuGet package.

enter image description here

This will add the below line to the ItemGroup within YourProjectName.csproj

<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
Divan
  • 309
  • 2
  • 3
  • 14
2

If you have package Serilog.Extensions.Logging added to your project already, it could be just a matter of adding using Serilog; to the top of your code file.

Alex Ho
  • 139
  • 1
  • 2