Warning

**Deprecation Notice**: The Coralogix .NET SDK (`Coralogix.SDK`, `CoralogixCoreSDK`) will be deprecated in favor of the [OpenTelemetry .NET SDK](https://opentelemetry.io/docs/languages/dotnet/) and will no longer be supported after **June 30, 2026**. See the [end-of-life notice](https://coralogix.com/docs/user-guides/latest-updates/deprecations/dotnet-sdk-deprecation/index.md) for migration details.

# .NET

This guide shows how to send .NET application logs to Coralogix using the [OpenTelemetry .NET SDK](https://opentelemetry.io/docs/languages/dotnet/) with `Microsoft.Extensions.Logging`, the OTLP log exporter, and a `Resource` that sets `service.name`, `cx.application.name`, and `cx.subsystem.name`. This replaces shipping logs through the legacy Coralogix .NET SDK.

Use a supported [.NET](https://dotnet.microsoft.com/download) version (8.0 or later is recommended) with current `OpenTelemetry.*` packages.

## Package dependencies setup

Create a console application and add the OpenTelemetry SDK, OTLP exporter, and logging packages (versions are examples; use the latest compatible releases from NuGet):

```bash
dotnet new console -n DotnetOtelLogsSample -o DotnetOtelLogsSample
cd DotnetOtelLogsSample
dotnet add package OpenTelemetry --version 1.15.3
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.15.3
dotnet add package Microsoft.Extensions.Logging --version 8.0.0
```

Select the https://ingress.\[[DOMAIN_VALUE]\] endpoint that corresponds to your Coralogix [domain](https://coralogix.com/docs/user-guides/account-management/account-settings/coralogix-domain/index.md) using the domain selector at the top of the page.

## Application implementation

1. Build a `ResourceBuilder` with `AddService` for `service.name` (and version), then add `cx.application.name` and `cx.subsystem.name` as resource attributes.
1. Call `LoggerFactory.Create` with `AddOpenTelemetry` and `AddOtlpExporter`, pointing at your OTLP/gRPC endpoint (the sample defaults to `http://localhost:4317` for a local collector).
1. Create an `ILogger` with a category name (for example the class name). Emit logs at the level you need; the sample uses `LogError` to match a simple smoke test.

Replace `Program.cs` with the following:

```csharp
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

namespace DotnetOtelLogsSample;

internal static class Program
{
    private static void Main()
    {
        var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT")
            ?? "http://localhost:4317";
        var serviceName = Environment.GetEnvironmentVariable("OTEL_SERVICE_NAME")
            ?? "dotnet-otel-logs-sample";
        var applicationName = Environment.GetEnvironmentVariable("CORALOGIX_APPLICATION")
            ?? "dotnet-otel-app";
        var subsystemName = Environment.GetEnvironmentVariable("CORALOGIX_SUBSYSTEM")
            ?? "worker";

        var resourceBuilder = ResourceBuilder.CreateDefault()
            .AddService(serviceName: serviceName, serviceVersion: "1.0.0")
            .AddAttributes(new Dictionary<string, object>
            {
                ["cx.application.name"] = applicationName,
                ["cx.subsystem.name"] = subsystemName,
            });

        using var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder.AddOpenTelemetry(logging =>
            {
                logging.SetResourceBuilder(resourceBuilder);
                logging.AddOtlpExporter(options =>
                {
                    options.Endpoint = new Uri(endpoint);
                });
            });
        });

        var logger = loggerFactory.CreateLogger("My class");

        var i = 0;
        do
        {
            logger.LogError("Hello World Coralogix");
            i++;
            Thread.Sleep(1000);
        } while (i < 5);

        Thread.Sleep(1500);
        Console.WriteLine("Done: flush window completed");
    }
}
```

Run the sample:

```bash
dotnet run
```

**Notes**

- The OTLP exporter batches records. Keep the process alive briefly after your last log (as in the sample) so batches can export before exit.
- If you upgrade packages, follow the [OpenTelemetry .NET logs documentation](https://opentelemetry.io/docs/languages/dotnet/instrumentation/#logging) for any API or package layout changes.
- For a broader walkthrough (shared resource builders, traces, and metrics), see [.NET OpenTelemetry instrumentation](https://coralogix.com/docs/opentelemetry/instrumentation-options/dotnet-opentelemetry-instrumentation/index.md).

### Logging output

With the OpenTelemetry SDK, you can send logs either to a local [OpenTelemetry Collector](https://coralogix.com/docs/opentelemetry/kubernetes-observability/kubernetes-observability-using-opentelemetry/index.md) or directly to Coralogix using an OTLP endpoint.

#### OpenTelemetry Collector

Set `OTEL_EXPORTER_OTLP_ENDPOINT` to your collector’s OTLP/gRPC address (often `http://localhost:4317` or `http://collector:4317`). With a `debug` exporter on the collector, log records show resource attributes such as `cx.application.name`, `cx.subsystem.name`, and `service.name`, plus the log body and severity.

#### Coralogix OpenTelemetry endpoint

Authenticate with your [Send-Your-Data API key](https://coralogix.com/docs/user-guides/account-management/api-keys/send-your-data-api-key/index.md) and point the OTLP exporter at your Coralogix endpoint. The sample reads `CORALOGIX_APPLICATION` and `CORALOGIX_SUBSYSTEM` for `cx.application.name` and `cx.subsystem.name` (defaults match a local collector; override them when sending to Coralogix).

```bash
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingress.[[DOMAIN_VALUE]]:443
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer send_your_data_key"
OTEL_SERVICE_NAME=dotnet-otel-logs-sample
CORALOGIX_APPLICATION=hello
CORALOGIX_SUBSYSTEM=world
```

In **Explore logs**, application and subsystem filters reflect those resource attributes; expanded rows show `logRecord.body`, severity, and `resource.attributes.service.name`.

### Additional resources

|                     |                                                                                             |
| ------------------- | ------------------------------------------------------------------------------------------- |
| OpenTelemetry .NET  | [OpenTelemetry .NET docs](https://opentelemetry.io/docs/languages/dotnet/)                  |
| Coralogix Endpoints | [Coralogix Endpoints](https://coralogix.com/docs/integrations/coralogix-endpoints/index.md) |

### Support

For help, use in-app chat or email [support@coralogix.com](mailto:support@coralogix.com).
