Skip to content
Documentation
Updated 8 minutes to read
Markdown
Release Notes

New 2.0.14

If you want to log using the Microsoft.Extensions.Logging.ILogger, see 'How To Start Logging With Microsoft.Extensions.Logging.ILogger in Azure Functions'.

You can use different settings examples (appsettings.json); the rest of the code examples below are the same regardless of which method you choose.

To enable Serilog Logging, add the required NuGet packages to your project. You can do this using the NuGet Package Manager in Visual Studio or by editing the .csproj file directly:

Update the version when you update the NuGet packages. The versions below are examples. Always update to the latest version.

<ItemGroup>
...
<PackageReference Include="Nodinite.Serilog.AzureBlobStorageSink" Version="2.0.27" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
</ItemGroup>
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((context, services) =>
{
// Create configuration object and read settings
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Create the logger object
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
// Register the logger object as a singleton using dependency injection to achieve great performance when you write multiple logs
services.AddSingleton<Serilog.ILogger, Serilog.Core.Logger>(sp => logger);
})
.Build();
host.Run();

Inject the Logger inside the constructor instead of the method level.

using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Serilog.Core.Enrichers;
namespace Nodinite.Demo.Azure.Functions
{
public class Function1
{
private readonly Serilog.ILogger _logger;
public Function1(Serilog.ILogger logger)
{
_logger = logger;
}
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
#region Nodinite Serilog sample code (should be refactored into a reusable component injected using dependency injection; all part of your documented asynchronous Logging Strategy)
string payload = await new StreamReader(req.Body).ReadToEndAsync();
string correlationId = Guid.NewGuid().ToString();
if (req.Headers.TryGetValue("x-ms-client-tracking-id", out Microsoft.Extensions.Primitives.StringValues id))
{
correlationId = id[0];
}
_logger.ForContext(new PropertyEnricher("ApplicationInterchangeId", $"{correlationId}"))
.Enrich.WithProperty("Color", "Yellow")
.ForContext(new PropertyEnricher("Body", JsonConvert.SerializeObject(payload)))
.ForContext(new PropertyEnricher("OriginalMessageType", "OrderMessage#1.0"))
.Information("Hello from Function");
#endregion
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to Azure Functions!");
return response;
}
}
}

Depending on the type of sink you use, apply the appropriate configuration using any of the following

{
"Serilog": {
"Using": ["Nodinite.Serilog.AzureBlobStorageSink"],
"WriteTo": [
{
"Name": "AzureBlobStorageSink",
"Args": {
"StorageAccountName": "Contoso",
"ContainerName": "logevents",
"Settings": {
...
}
}
}
]
}
}

New 2.0.16

{
"Serilog": {
"Using": ["Nodinite.Serilog.AzureBlobStorageSink"],
"WriteTo": [
{
"Name": "AzureBlobStorageSink",
"Args": {
"StorageAccountName": "Contoso",
"ContainerName": "logevents",
"ManagedIdentityClientId": "00000000-0000-0000-0000-000000000000",
"Settings": {
...
}
}
}
]
}
}

Copy the ConnectionString from the Azure Portal (Access keys) for the Storage Account in use.

{
"Serilog": {
"Using": [ "Nodinite.Serilog.AzureBlobStorageSink" ],
"WriteTo": [
{
"Name": "NodiniteAzureBlobStorageSinkWithConnectionString",
"Args": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=Contoso;AccountKey=xyz;EndpointSuffix=core.windows.net",
"ContainerName": "logevents",
"Settings": {
...
}
}
}
]
}
}
{
"Serilog": {
"Using": ["Nodinite.Serilog.EventHubSink"],
"WriteTo": [
{
"Name": "NodiniteEventHubSink",
"Args": {
"eventHubNameSpace": "Contoso.servicebus.windows.net",
"eventHubName": "eventhub-01",
"Settings": {
...
}
}
}
]
}
}

Use a dedicated Shared access policy to create a connectionstring including the EntityPath=%name%.

{
"Serilog": {
"Using": [ "Nodinite.Serilog.EventHubSink" ],
"WriteTo": [
{
"Name": "NodiniteEventHubSink",
"Args": {
"ConnectionString": "Endpoint=sb://xyz.servicebus.windows.net/;SharedAccessKeyName=Default;SharedAccessKey=secretstuff;EntityPath=yourentityname
",
"Settings": {
...
}
}
}
]
}
}
{
"Serilog": {
"Using": [ "Nodinite.Serilog.FileSink" ],
"WriteTo": [
{
"Name": "NodiniteFileSink",
"Args": {
"Folder": "\\FancyUNCSrv01\\DaShare\\In",
"Settings": {
...
"FileExtension": ".json"
}
}
}
]
}
}
{
"Serilog": {
"Using": [ "Nodinite.Serilog.ApiSink" ],
"WriteTo": [
{
"Name": "NodiniteApiSink",
"Args": {
"NodiniteApiUrl": "https://fancysrv01:50002/LogAPI/api/LogEvent",
"Settings": {
...
}
}
}
]
}
}

Service Bus (Using the default Managed Identity)

Section titled “Service Bus (Using the default Managed Identity)”
{
"Serilog": {
"Using": ["Nodinite.Serilog.ServiceBusSink"],
"WriteTo": [
{
"Name": "NodiniteServiceBusSink",
"Args": {
"NamespaceName": "Contoso.servicebus.windows.net",
"QueueName": "sbq01",
"Settings": {
...
}
}
}
]
}
}

Service Bus (Using an explicit Managed Identity)

Section titled “Service Bus (Using an explicit Managed Identity)”

New 2.0.27

{
"Serilog": {
"Using": ["Nodinite.Serilog.ServiceBusSink"],
"WriteTo": [
{
"Name": "NodiniteServiceBusSink",
"Args": {
"NamespaceName": "Contoso.servicebus.windows.net",
"QueueName": "sbq01",
"ManagedIdentityClientId": "00000000-0000-0000-0000-000000000000",
"Settings": {
...
}
}
}
]
}
}

Copy the ConnectionString from the Azure Portal (Access keys) for the Storage Account in use.

{
"Serilog": {
"Using": [ "Nodinite.Serilog.ServiceBusSink" ],
"WriteTo": [
{
"Name": "NodiniteServiceBusSinkWithConnectionString",
"Args": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=Contoso;AccountKey=xyz;EndpointSuffix=core.windows.net",
"QueueName": "sqb01",
"Settings": {
...
}
}
}
]
}
}

In this example multiple sinks are used. The example uses the Azure Blob Storage Sink and the Service Bus Sink. The settings for each sink are specified in the Settings section of the configuration file.

{
"Serilog": {
"Using": [ "Nodinite.Serilog.AzureBlobStorageSink", "Nodinite.Serilog.ServiceBusSink" ],
"WriteTo": [
{
"Name": "AzureBlobStorageSink",
"Args": {
"StorageAccountName": "Contoso",
"ContainerName": "logevents",
"Settings": {
...
}
}
},
{
"Name": "NodiniteServiceBusSink",
"Args": {
"NamespaceName": "Contoso.servicebus.windows.net",
"QueueName": "sbq01",
"Settings": {
...
}
}
},
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
]
}
}

Configure Message Types - Critical for making your Serilog data searchable and enabling business intelligence features

❔'How To Start Logging With Microsoft.Extensions.Logging.ILogger in Azure Functions'

Pickup Log Events Service Logging Agent
Log Events
JSON Log Event

Logging
Message Types - Critical for Search Field extraction from Serilog events
Search Fields - Extract business data from logged messages
Search Field Expressions - Define extraction patterns for Order Numbers, Customer IDs, Invoice amounts
Log Views - Search logged data by business identifiers
Non-Events Monitoring - Track message volumes and detect missing messages
Business Process Modeling (BPM) - End-to-end transaction tracking across Azure Functions and other systems
Reduce Azure Functions Logging Costs - Lower the Azure cost of logging Azure Functions
Azure Costs for Blob Storage and Event Hub - What staging log events in Azure costs
Reduce Azure Logic Apps Run History Retention to Lower TCO - The same approach for Logic Apps
Reduce Azure API Management Logging Costs - The same approach for API Management

...

Version:
Release date: N/A
Loading...

Explore Nodinite for 30 days – completely free

  • Find out why this is the leading O&M software
  • Prove your business case
  • Test the self-service for the business side

Loading...

See how Nodinite resolves your technical issues, in a LIVE Demo!

Once you submit this request:

  • We will give you a short call to understand the issues you want to tackle.
  • Understand solutions you have tried and why were you not happy with them.
  • Book a convenient slot for a LIVE demo!

Loading...