![]() |
VOOZH | about |
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" />Directory.Packages.props
<PackageReference Include="CB.Serilog.Sinks.AzureLogAnalytics" />Project file
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.0Install as a Cake Addin
#tool nuget:?package=CB.Serilog.Sinks.AzureLogAnalytics&version=1.2.0Install as a Cake Tool
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.
dotnet add package CB.Serilog.Sinks.AzureLogAnalytics
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.
The sink can be configured programmatically or through the Serilog.Settings.Configuration NuGet package via appsettings.json. An instance of AzureLogAnalyticsSinkConfiguration is required.
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
}
}
]
}
}
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
});
});
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. |
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 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
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.