# Implementing OpenTelemetry Logging in .NET Applications with OpenObserve

> Learn how to implement OpenTelemetry logging in .NET applications using OpenObserve. Step-by-step guide to configure, collect, and analyze logs.

Source: https://openobserve.ai/blog/opentelemetry-logging-in-dotnet-applications/
Published: 2025-03-11
Authors: Manas Sharma
Category: Engineering
Tags: Application, OpenTelemetry

---

## Introduction

Effective logging is crucial for understanding and troubleshooting application behavior. In .NET applications, OpenTelemetry provides a standardized way to collect and export logs alongside traces and metrics.

In this comprehensive guide, you'll learn how to implement OpenTelemetry logging in a .NET 6+ Web API application and visualize logs using OpenObserve.

### Why OpenTelemetry for Logging?

OpenTelemetry offers several advantages for logging in .NET applications:

- Unified logging standard across different languages and platforms
- Seamless integration with existing .NET logging infrastructure
- Ability to correlate logs with traces for better debugging
- Vendor-neutral approach with support for multiple backends

## Prerequisites

Before we begin, ensure you have:

- .NET SDK 6.0 or newer installed
- An OpenObserve account (You can use [OpenObserve Cloud](https://cloud.openobserve.ai) or set up a [self-hosted installation](https://openobserve.ai/docs/quickstart/#self-hosted-installation))

## Getting Started

We'll use our order processing API to demonstrate logging implementation. The application already has tracing configured, and we'll add logging to provide more detailed insights into its operation.

```bash
git clone https://github.com/openobserve/dotnet-opentelemetry-tracing-application
cd dotnet-opentelemetry-tracing-application
```

## Implementing OpenTelemetry Logging in .NET

Add the OpenTelemetry logging packages to your project:

```bash
dotnet add package OpenTelemetry.Extensions.Logging --version 1.7.0
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.7.0
```

### Configuring OpenTelemetry Logging

Configure OpenTelemetry logging in your `Program.cs`:

```csharp
builder.Logging.ClearProviders();

var resourceBuilder = ResourceBuilder.CreateDefault()
    .AddService("OrderProcessingService")
    .AddAttributes(new Dictionary<string, object>
    {
        ["environment"] = "development",
        ["service.version"] = "1.0.0"
    });

builder.Logging.AddOpenTelemetry(logging => {
    logging.IncludeFormattedMessage = true;
    logging.SetResourceBuilder(resourceBuilder)
        .AddConsoleExporter()
        .AddOtlpExporter(otlpOptions => {
            otlpOptions.Endpoint = new Uri("your_openobserve_url/v1/logs");
            otlpOptions.Headers = "Authorization=Basic YOUR_AUTH_TOKEN";
            otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
        });
});
```

> **Note**: Replace the placeholder values with your OpenObserve details:
>
> 1. Add `/v1/logs` to your OTLP HTTP endpoint
> 2. Use your Base64 encoded credentials in the Authorization header

### Adding Logs to Your Controllers

Example of logging in an API controller:

```csharp
[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
    private readonly ILogger<OrderController> _logger;

    public OrderController(ILogger<OrderController> logger)
    {
        _logger = logger;
    }

    [HttpPost]
    public async Task<IActionResult> CreateOrder(Order order)
    {
        _logger.LogInformation(
            "Creating order for customer: {CustomerName} with amount: {Amount}",
            order.CustomerName,
            order.Amount
        );

        try
        {
            // Order processing logic...
            return Ok(order);
        }
        catch (Exception ex)
        {
            _logger.LogError(
                ex,
                "Failed to create order for customer {CustomerName}",
                order.CustomerName
            );
            throw;
        }
    }
}
```

### Testing Your Logging Setup

Run your application:

```bash
dotnet run
```

Send some test requests to generate logs:

```bash
# Create an order
curl -X POST http://localhost:5069/order \
  -H "Content-Type: application/json" \
  -d '{"customerName":"Test User","amount":150.00}'

# Try to get a non-existent order (generates warning logs)
curl http://localhost:5069/order/999
```

## Viewing Logs in OpenObserve

1. Log into your OpenObserve instance
2. Navigate to the Logs section
3. You should see a `default` logs stream containing:
   - Order creation attempts
   - Successful order creations
   - Warning logs for non-existent orders

![Screen Recording 2025-03-11 at 11.30.45 AM.gif](/assets/Screen_Recording_2025_03_11_at_11_30_45_AM_3fbdc00b75.gif)

## Correlating Logs with Traces

OpenTelemetry automatically correlates logs with traces in your application. When you make a request:

```csharp
[HttpPost]
public async Task<IActionResult> CreateOrder(Order order)
{
    _logger.LogInformation(
        "Creating order for customer: {CustomerName} with amount: {Amount}",
        order.CustomerName,
        order.Amount
    );

    using var activity = TracingInstrumentation.ActivitySource.StartActivity("CreateOrder");

}
```

Each log entry includes:

- Trace ID: Links to the corresponding distributed trace
- Span ID: Shows which operation generated the log
- Service name and version

![Screenshot 2025-03-11 at 3.13.36 PM.png](/assets/Screenshot_2025_03_11_at_3_13_36_PM_ae57247fbf.png)

## Troubleshooting Common Issues

If you're not seeing logs in OpenObserve:

- Verify the logs endpoint includes `/v1/logs`
- Ensure your authentication token is correct
- Make test requests to generate some logs
- Check console output for any export errors

## Next Steps

Now that you have OpenTelemetry logging set up in your .NET application:

1. Watch our <a href="https://www.youtube.com/watch?v=UghiP6zhSUw" target="_blank" rel="noopener noreferrer">detailed video guide</a> on correlating logs with traces for better debugging
2. Follow our [.NET tracing guide](https://openobserve.ai/blog/distributed-tracing-in-dotnet-application/) to implement distributed tracing in your .NET applications.
3. Set up log-based alerts, explore structured logging patterns, or add custom attributes to your logs. The complete sample code is available in our <a href="https://github.com/openobserve/dotnet-opentelemetry-tracing-application" target="_blank" rel="noopener noreferrer">GitHub repository</a>.

Need help? Join our [Slack community](https://short.openobserve.ai/community) for support.

Happy Monitoring! 🚀
