1

I have a Azure Function that triggers when a new service bus message arrives. I want to use Serilog to show messages on my console and in MongoDB.

But, when i enable serilog in my azure function project, a lot of unecessary messages appears. I'm trying to supress this messages in host.json, but this dont't work.

This messages appears when the application starts:

[12:15:26 INF] Initializing Warmup Extension.
[12:15:26 INF] Initializing Host. OperationId: '9cfb7811-e411-4445-b58b-0c702124462e'.
[12:15:26 INF] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=9cfb7811-e411-4445-b58b-0c702124462e
[12:15:26 INF] User profile is available. Using 'C:\Users\codew\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
[12:15:26 INF] LoggerFilterOptions
{
  "MinLevel": "None",
  "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Serilog.Extensions.Logging.SerilogLoggerProvider",
      "CategoryName": null,
      "LogLevel": "Trace",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Azure.Functions.Cli.Diagnostics.ColoredConsoleLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    }
  ]
}
[12:15:26 INF] LoggerFilterOptions
{
  "MinLevel": "None",
  "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Serilog.Extensions.Logging.SerilogLoggerProvider",
      "CategoryName": null,
      "LogLevel": "Trace",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Azure.Functions.Cli.Diagnostics.ColoredConsoleLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    }
  ]
}
[12:15:26 INF] ConcurrencyOptions
{
  "DynamicConcurrencyEnabled": false,
  "MaximumFunctionConcurrency": 500,
  "CPUThreshold": 0.8,
  "SnapshotPersistenceEnabled": true
}
[12:15:26 INF] FunctionResultAggregatorOptions
{
  "BatchSize": 1000,
  "FlushTimeout": "00:00:30",
  "IsEnabled": true
}
[12:15:26 INF] SingletonOptions
{
  "LockPeriod": "00:00:15",
  "ListenerLockPeriod": "00:00:15",
  "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
  "LockAcquisitionPollingInterval": "00:00:05",
  "ListenerLockRecoveryPollingInterval": "00:01:00"
}
[12:15:26 INF] ServiceBusOptions
{
  "ClientRetryOptions": {
    "Mode": "Exponential",
    "TryTimeout": "00:01:00",
    "Delay": "00:00:00.8000000",
    "MaxDelay": "00:01:00",
    "MaxRetries": 3
  },
  "TransportType": "AmqpTcp",
  "WebProxy": "",
  "AutoCompleteMessages": true,
  "PrefetchCount": 0,
  "MaxAutoLockRenewalDuration": "00:05:00",
  "MaxConcurrentCalls": 128,
  "MaxConcurrentSessions": 8,
  "MaxMessageBatchSize": 1000,
  "SessionIdleTimeout": "",
  "EnableCrossEntityTransactions": false
}

Then, this messages appear constantly: enter image description here

This is my code to enable serilog:

private void AddSerilog(IFunctionsHostBuilder builder)
{
    Logger logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .WriteTo.Console()
        .CreateLogger();

    builder.Services.AddLogging(logBuilder =>
    {
        logBuilder.AddSerilog(logger);
    });
}

Thanks!

Michael Willy
  • 61
  • 1
  • 4

1 Answers1

1

One of the workarounds that I have enabled Serilog on Azure Functions .NET 6 Service Bus Queue Trigger Code which gave me less result of metadata in the console:

  • Used the AzureWebJobsStorage Connection as Azure Service Bus Queue.

Startup.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Core;

namespace KrishFunctionApp1205H
{
    internal class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            Logger logger = new LoggerConfiguration()
                       .MinimumLevel.Information()
                       .WriteTo.Console()
                       .CreateLogger();
                       
            builder.Services.AddLogging(logBuilder =>
            {
                logBuilder.AddSerilog(logger);
            });
        }
    }
}

Program.cs:

using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();


host.Run();

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>    
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="4.2.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
    <PackageReference Include="Serilog" Version="2.10.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

ServiceBusTriggerSerilog.cs

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace KrishFunctionApp1205H
{
    public class ServiceBusTriggerSerilog
    {
        private readonly ILogger _logger;

        public ServiceBusTriggerSerilog(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<ServiceBusTriggerSerilog>();
        }

        [Function("ServiceBusTriggerSerilog")]
        public void Run([ServiceBusTrigger("myqueue", Connection = "AzureWebJobsStorage")] string myQueueItem)
        {
            _logger.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }
}

Result :

enter image description here

Few workarounds I have done to reduce the costs of Application Insights and reduce the metadata result coming in console when we run the Azure Functions which is based on the logging levels we set in host.json, please refer here.