CB.Serilog.Sinks.AzureLogAnalytics
1.2.0
dotnet add package CB.Serilog.Sinks.AzureLogAnalytics --version 1.2.0
NuGet\Install-Package CB.Serilog.Sinks.AzureLogAnalytics -Version 1.2.0
<PackageReference Include="CB.Serilog.Sinks.AzureLogAnalytics" Version="1.2.0" />
<PackageVersion Include="CB.Serilog.Sinks.AzureLogAnalytics" Version="1.2.0" />
<PackageReference Include="CB.Serilog.Sinks.AzureLogAnalytics" />
paket add CB.Serilog.Sinks.AzureLogAnalytics --version 1.2.0
#r "nuget: CB.Serilog.Sinks.AzureLogAnalytics, 1.2.0"
#:package CB.Serilog.Sinks.AzureLogAnalytics@1.2.0
#addin nuget:?package=CB.Serilog.Sinks.AzureLogAnalytics&version=1.2.0
#tool nuget:?package=CB.Serilog.Sinks.AzureLogAnalytics&version=1.2.0
Serilog Sink for Azure Log Analytics
A bare-bones custom Serilog sink for Azure Log Analytics. Supports batching of logs and utilizes the Azure.Monitor.Ingestion library for ingestion to Log Analytics via the new Log Ingestion API.
Prerequisites
- Azure Subscription
- Log Analytics workspace provisioned
- A configured data collection endpoint and data collection rule in Azure with appropriate permissions.
Get Started
Install Package
dotnet add package CB.Serilog.Sinks.AzureLogAnalytics
Authenticate with Azure
By default, a DefaultAzureCredential is used to authenticate with Azure, meaning no additional code is needed if your environment is already configured. Optionally, a custom TokenCredential can be passed in during configuration.
Configure
The sink can be configured programmatically or through the Serilog.Settings.Configuration NuGet package via appsettings.json. An instance of AzureLogAnalyticsSinkConfiguration is required.
App Config Example
Program.cs
Host.CreateDefaultBuilder()
.UseSerilog((hostingContext, services, loggerConfiguration) =>
{
var assemblies = new[] { typeof(AzureLogAnalyticsSink).Assembly };
var options = new ConfigurationReaderOptions(assemblies);
loggerConfiguration
.Enrich.FromLogContext()
.ReadFrom.Configuration(hostingContext.Configuration, options);
});
appsettings.json
{
"Serilog": {
"Using": [ "CB.Serilog.Sinks.AzureLogAnalytics" ],
"MinimumLevel": "Verbose",
"WriteTo": [
{
"Name": "AzureLogAnalytics",
"Args": {
"DataCollectionEndpointUri": "https://<data-collection-endpoint>.logs1.azure.com",
"RuleId": "dcr-<rule-id>",
"StreamName": "Custom-MyLogs_CL",
"OutputToConsole": true,
"MaxLogEntries": 5
}
}
]
}
}
IHostBuilder Example
Program.cs
Host.CreateDefaultBuilder()
.UseSerilog((hostingContext, services, loggerConfiguration) =>
{
loggerConfiguration
.Enrich.FromLogContext()
.WriteTo.AzureLogAnalytics(new AzureLogAnalyticsSinkConfiguration
{
DataCollectionEndpointUri = new Uri("https://<data-collection-endpoint>.logs1.azure.com"),
RuleId = "dcr-<rule-id>",
StreamName = "Custom-MyLogs_CL",
MaxLogEntries = 10,
OutputToConsole = true
// TokenCredential = new DefaultAzureCredential() // Optional
});
});
Configuration Options
The AzureLogAnalyticsSinkConfiguration type is used to configure the sink.
| Property | Description |
|---|---|
| DataCollectionEndpointUri | The Data Collection Endpoint URI set up in Azure. |
| MaxLogEntries | The maximum number of log entries to buffer before flushing to Log Analytics. |
| RuleId | The Data Collection Rule ID set up in Azure. |
| StreamName | The Data Collection Rule stream name (e.g., your custom Log Analytics table like Custom-MyLogs_CL). |
| OutputToConsole | An optional feature that outputs payload format to the console. This is incredibly useful when configuring and troubleshooting your Data Collection Rule schema and transformations. |
| TokenCredential | An instance of TokenCredential used to authenticate with Azure. This is optional; by default, a DefaultAzureCredential is used. |
| Transform | An optional Func<LogEvent, IDictionary<string, object>> to transform a Serilog LogEvent to an IDictionary<string, object> that will be serialized to JSON and sent to Log Analytics. A default implementation is provided if not set. Note: This method should be thread-safe, as it will be called concurrently by Serilog. |
Default Log Schema
By default, if you don't provide a custom Transform function, the sink maps Serilog properties to the following JSON structure which you will need to map your Data Collection Rule schema to:
{
"Timestamp": "2023-10-01T12:00:00.0000000Z",
"Level": "Information",
"Message": "This is a log message",
"Exception": "Exception details if any",
"Properties": {
"CustomProperty": "CustomValue"
}
}
You should define your custom Log Analytics table columns appropriately to match the default transformation output, or provide your own Transform function if your schema dictates a different structure.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Azure.Identity (>= 1.18.0)
- Azure.Monitor.Ingestion (>= 1.2.0)
- Serilog (>= 4.0.0)
- Serilog.Settings.Configuration (>= 8.0.0)
- Serilog.Sinks.PeriodicBatching (>= 5.0.0)
-
net8.0
- Azure.Identity (>= 1.18.0)
- Azure.Monitor.Ingestion (>= 1.2.0)
- Serilog (>= 4.0.0)
- Serilog.Settings.Configuration (>= 8.0.0)
- Serilog.Sinks.PeriodicBatching (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Added support for .NET 10.0.
- Updated to Serilog 4
- Updated Azure Identity dependency to 1.13.2.
- Removed Newtonsoft dependency, switching to System.Text
- Utilze the PeriodicBatching interface
- Improved log ingestion retry logic.