Reputation: 439
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 Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
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?
Upvotes: 0
Views: 1224
Reputation: 8629
To fix those errors you are getting Could not load file or assembly 'Microsoft.Extensions.Logging
:
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:
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:
References:
c# - How to setup Serilog with Azure Functions v4 correctly? - Stack Overflow
Upvotes: 1