0

I am running into issues getting Azure Function v4 running locally with Serilog. I checked NUGET and have all the latest packages.

<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.11.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.AzureTableStorage" Version="8.5.41" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />

In startup.cs, I have the following:

Using section

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;

Configure function

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Warning()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
    .MinimumLevel.Override("Worker", LogEventLevel.Warning)
    .MinimumLevel.Override("Host", LogEventLevel.Warning)
    .MinimumLevel.Override("System", LogEventLevel.Error)
    .MinimumLevel.Override("Function", LogEventLevel.Error)
    .MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Error)
    .MinimumLevel.Override("Azure.Core", LogEventLevel.Error)
    .WriteTo.Console()
    .WriteTo.Async(a => a.AzureTableStorage(
        connectionString: "connection string goes here",
        storageTableName: "table name goes here",
        propertyColumns: new[] { "Application" }), bufferSize: 20, blockWhenFull: true)
    .Enrich.WithCorrelationId()
    .Enrich.WithCorrelationIdHeader("X-Correlation-ID")
    .Enrich.WithProperty("Application", "app name goes here")
    .CreateLogger();

builder.Services.AddLogging(lb =>
{
    lb.ClearProviders();
    lb.AddSerilog(Log.Logger, true);
});

Throughout the function app, I am referring to ILogger from the "Microsoft.Extensions.Logging" library.

I have tried removing the lb.ClearProviders(); line as still no luck.

The project compiles fine but when I run it, func.exe returns the following error:

Exception thrown: 'System.IO.FileNotFoundException' in Microsoft.Azure.WebJobs.Host.dll An exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Azure.WebJobs.Host.dll but was not handled in user code Could not load file or assembly 'Microsoft.Extensions.Logging, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

I also get a code error in WebJobsBuilderExtensions.cs on line 162 startup2.Configure(context, builder); which says:

Could not load file or assembly 'Microsoft.Extensions.Logging, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'

I even tried deleting the AppData\Local\AzureFunctionsTools folder the load/run my project and still no luck.

Any suggestions on how to get Serilog working?

206mph
  • 439
  • 6
  • 14

1 Answers1

1

To fix those errors you are getting Could not load file or assembly 'Microsoft.Extensions.Logging:

  • Install Microsoft.Extensions.Logging in your application with version 6.0.0.
  • Remove lb.ClearProviders(); in your code, otherwise the code won't work.

I have tried with the below code and I didn't get any errors.

Startup class:

 Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Warning()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
    .MinimumLevel.Override("Worker", LogEventLevel.Warning)
    .MinimumLevel.Override("Host", LogEventLevel.Warning)
    .MinimumLevel.Override("System", LogEventLevel.Error)
    .MinimumLevel.Override("Function", LogEventLevel.Error)
    .MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Error)
    .MinimumLevel.Override("Azure.Core", LogEventLevel.Error)
    .WriteTo.Console()
    .WriteTo.Async(a => a.AzureTableStorage(
        connectionString: "<Your_Storage_Connection_String>",
        storageTableName: "<Table_Name>",
        propertyColumns: new[] { "Application" }), bufferSize: 20, blockWhenFull: true)
    .Enrich.WithCorrelationId()
    .Enrich.WithCorrelationIdHeader("Correlation-ID")
    .Enrich.WithProperty("Application", "Application_Name")
    .CreateLogger();

            builder.Services.AddLogging(lb =>
            {
                lb.AddSerilog(Log.Logger, true);
            });

Initially I was getting the same error:

enter image description here

Later I have downgraded version of Microsoft.Extensions.Logging to 6.0.0 and Microsoft.Extensions.Logging.Abstractions to 6.0.0.

Dependencies in .csproj:

<ItemGroup>
          <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
          <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
          <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
          <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
          <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
          <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
          <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
          <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
          <PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
          <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
          <PackageReference Include="Serilog.Sinks.AzureTableStorage" Version="8.5.41" />
          <PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
</ItemGroup>

It worked without any errors:

enter image description here

References:

c# - How to setup Serilog with Azure Functions v4 correctly? - Stack Overflow

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7
  • Thanks for the suggestion! I tried it but it didn't work. I got the same error. I also tried installing v7 of the library (in addition to commenting out the one line) and still no luck. – 206mph Jul 26 '23 at 16:34
  • Copy all the [dependencies](https://i.imgur.com/mfb43n6.png) I have given in the answer and paste in your .csproj and build your project. This error you are getting is because of version compatibility issue. – Pravallika KV Jul 26 '23 at 16:52
  • Thanks again! Here are my [dependencies](https://ibb.co/fNRv6xX) based on your suggestions. Now I'm getting a different [error](https://ibb.co/LC53M3V). – 206mph Jul 26 '23 at 17:24
  • Here's a better description of the new error: Exception thrown: 'System.TypeLoadException' in Microsoft.Azure.WebJobs.Host.dll An exception of type 'System.TypeLoadException' occurred in Microsoft.Azure.WebJobs.Host.dll but was not handled in user code Could not load type 'Microsoft.Azure.WebJobs.ParameterBindingData' from assembly 'Microsoft.Azure.WebJobs, Version=3.0.34.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. – 206mph Jul 26 '23 at 17:25
  • Did you remove any of your existing dependencies? – Pravallika KV Jul 26 '23 at 17:34
  • Yes. I have a lot of code the needs to [dependencies](https://ibb.co/sJVgXGG), so I created a test function app and am getting the exact same result. – 206mph Jul 26 '23 at 18:04
  • Keep your existing dependencies as it is and add the above given dependencies in your project. If any dependencies are duplicate, keep the ones which I have given and delete the other similar dependencies. Regarding the error `Could not load type 'Microsoft.Azure.WebJobs.ParameterBindingData' from assembly 'Microsoft.Azure.WebJobs, Version=3.0.34.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35` install Microsoft.Azure.WebJobs package in your project. – Pravallika KV Jul 27 '23 at 05:21
  • 1
    Thanks for all the suggestions. to fix the issue, I had to update the versions, as you suggestion, but then had to uninstall Azure Function Core Tools, delete the appdata folder, then reinstall. Once I did that, it all worked! So my library versions were not correct and my install of Azure Functions Core Tools was messed up. – 206mph Jul 28 '23 at 22:22