LogAnalytics.Client 6.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package LogAnalytics.Client --version 6.2.2                
NuGet\Install-Package LogAnalytics.Client -Version 6.2.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LogAnalytics.Client" Version="6.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LogAnalytics.Client --version 6.2.2                
#r "nuget: LogAnalytics.Client, 6.2.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LogAnalytics.Client as a Cake Addin
#addin nuget:?package=LogAnalytics.Client&version=6.2.2

// Install LogAnalytics.Client as a Cake Tool
#tool nuget:?package=LogAnalytics.Client&version=6.2.2                

LogAnalytics Client for .NET Core

The easiest way to send logs to Azure Log Analytics from your apps. Construct a custom object and send it to Log Analytics. It will be represented as a log entry in the logs. This helps make logging easy in your applications, and you can focus on more important business logic.

Build Status

CI - Integration Tests CodeQL Defender for DevOps - GitHub

NuGet

The LogAnalytics.Client is available on NuGet.

Support for .NET 6

The LogAnalytics.Client project has been upgraded to .NET 6, and bumped the major version to 6.x.

LogAnalytics.Client currently support the below versions of .NET.

Version Supported
.NET 6.x
.NET 5.x
.NET Core 3.x

How to use the LogAnalytics Client

Installing the package

Install with the dotnet CLI
dotnet add package LogAnalytics.Client
Install with NuGet Package Manager
Install-Package LogAnalytics.Client
Install by adding a PackageReference to csproj
<PackageReference Include="LogAnalytics.Client" Version="6.2.2" />
Install using Paket CLI
paket add LogAnalytics.Client

Initialize the LogAnalyticsClient

Initialize a new LogAnalyticsClient object with a Workspace Id and a Key:

LogAnalyticsClient logger = new LogAnalyticsClient(
                workspaceId: "LAW ID",
                sharedKey: "LAW KEY");

Send a single log entry

Synchronous execution (non-HTTP applications):

logger.SendLogEntry(new TestEntity
{
    Category = GetCategory(),
    TestString = $"String Test",
    TestBoolean = true,
    TestDateTime = DateTime.UtcNow,
    TestDouble = 2.1,
    TestGuid = Guid.NewGuid()
}, "demolog").Wait();

Asynchronous execution (HTTP-based applications):

await logger.SendLogEntry(new TestEntity
{
    Category = GetCategory(),
    TestString = $"String Test",
    TestBoolean = true,
    TestDateTime = DateTime.UtcNow,
    TestDouble = 2.1,
    TestGuid = Guid.NewGuid()
}, "demolog")
.ConfigureAwait(false); // Optionally add ConfigureAwait(false) here, depending on your scenario

Send a batch of log entries with one request

If you need to send a lot of log entries at once, it makes better sense to send them as a batch/collection instead of sending them one by one. This saves on requests, resources and eventually costs.

// Example: Wiring up 5000 entities into an "entities" collection.
List<DemoEntity> entities = new List<DemoEntity>();
for (int ii = 0; ii < 5000; ii++)
{
    entities.Add(new DemoEntity
    {
        Criticality = GetCriticality(),
        Message = "lorem ipsum dolor sit amet",
        SystemSource = GetSystemSource()
    });
}

// Send all 5000 log entries at once, in a single request.
await logger.SendLogEntries(entities, "demolog").ConfigureAwait(false);

Ship logs to another Azure Sovereign Cloud

To send logs to another Azure Sovereign Cloud, for example the Government cloud, you can specify an enum value for the the optional parameter azureSovereignCloud.

Here's an example:

LogAnalyticsClient logger = new LogAnalyticsClient(
                workspaceId: "LAW ID",
                sharedKey: "LAW KEY",
                EndPointOverride: "ods.opinsights.azure.us"); // Use Azure Government instead of the (default) Azure Public cloud.

The available sovereign clouds are currently:

  • Azure Public Cloud (Commercial): ods.opinsights.azure.com
  • Azure Government: ods.opinsights.azure.us
  • Azure China: ods.opinsights.azure.cn

Development

If you want to develop the project locally and enhance it with your custom logic, or want to contribute to the GitHub repository with a PR, it's a good idea to verify that the code works and tests are flying.

Configure Tests secrets

If you want to develop and run local tests, it is a good idea to set up your custom Log Analytics Workspace Id and Key in the project. This can be done using user secrets.

Using the dotnet CLI from the LogAnalyticsClient.Tests project directory:

dotnet user-secrets set "LawConfiguration:LawId" "YOUR LOG ANALYTICS INSTANCE ID"
dotnet user-secrets set "LawConfiguration:LawKey" "YOUR LOG ANALYTICS WORKSPACE KEY"
dotnet user-secrets set "LawServicePrincipalCredentials:ClientId" "CLIENT ID HERE"
dotnet user-secrets set "LawServicePrincipalCredentials:ClientSecret" "CLIENT SECRET HERE"
dotnet user-secrets set "LawServicePrincipalCredentials:Domain" "TENANT NAME OR DOMAIN ID HERE"

You should now have a secrets.json file in your local project, with contents similar to this:

{
  "LawConfiguration": {
    "LawId": "YOUR LOG ANALYTICS INSTANCE ID",
    "LawKey": "YOUR LOG ANALYTICS WORKSPACE KEY"
  },
  "LawServicePrincipalCredentials": {
    "ClientId": "Principal Client ID Here",
    "ClientSecret": "Principal Client Secret Here",
    "Domain": "Your tenant guid here, or tenant name"
  }
}
Where can I get the credentials?

To add the secrets with the correct values, you need to add the Workspace Id (LawId), and the Key. You can find these from the Log Analytics Workspace in the Azure Portal, for example.

To add the Service Principal secrets, you should create a new service principal and add a secret, then grant it reader access on the Log Analytics resource. There are steps outlines for that part here: https://zimmergren.net/retrieve-logs-from-application-insights-programmatically-with-net-core-c/ - refer to "Step 1".

Read more about configuring user secrets in .NET Core projects: https://docs.microsoft.com/aspnet/core/security/app-secrets

Additional notes

This project is a spin-off from code samples. The examples and usage grew over time, and an interest was shown of a wrapper or client for Log Analytics.

Related blog posts:

Keeping it simple.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.2.3 12,756 7/8/2024
6.2.2 49,283 9/26/2023
6.2.1 10,579 7/7/2023
6.2.0 28,687 1/17/2023
6.1.3 4,401 11/15/2022
6.1.2 34,836 4/20/2022
6.1.1 5,325 2/1/2022
6.1.0 8,658 11/20/2021
6.0.0 764 11/11/2021
5.2.1 13,445 8/26/2021
5.2.0 1,736 7/13/2021
5.1.0 457 7/8/2021
5.0.0 6,211 3/26/2021
1.3.2 19,060 3/26/2021
1.3.1 5,220 2/26/2021
1.3.0 2,518 2/8/2021
1.2.0 2,051 2/8/2021
1.1.3 465 2/4/2021
1.1.2 785 1/27/2021
1.1.1 781 12/8/2020
1.1.0 783 12/4/2020
1.0.7 4,373 11/16/2020
1.0.6 912 11/5/2020
1.0.5 860 10/30/2020
1.0.4 7,056 9/29/2020
1.0.3 738 9/10/2020
1.0.2 625 8/28/2020
1.0.1 1,030 8/14/2020
1.0.0 594 7/17/2020