Muscula.Csharp.Extensions.Logging.AspNetCore 2.0.2

dotnet add package Muscula.Csharp.Extensions.Logging.AspNetCore --version 2.0.2
                    
NuGet\Install-Package Muscula.Csharp.Extensions.Logging.AspNetCore -Version 2.0.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="Muscula.Csharp.Extensions.Logging.AspNetCore" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Muscula.Csharp.Extensions.Logging.AspNetCore" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Muscula.Csharp.Extensions.Logging.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Muscula.Csharp.Extensions.Logging.AspNetCore --version 2.0.2
                    
#r "nuget: Muscula.Csharp.Extensions.Logging.AspNetCore, 2.0.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.
#:package Muscula.Csharp.Extensions.Logging.AspNetCore@2.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Muscula.Csharp.Extensions.Logging.AspNetCore&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Muscula.Csharp.Extensions.Logging.AspNetCore&version=2.0.2
                    
Install as a Cake Tool

Muscula.Csharp.Extensions.Logging.AspNetCore

Overview

Muscula.Csharp.Extensions.Logging.AspNetCore provides seamless integration with ASP.NET Core's built-in logging framework, allowing you to send logs to Muscula error monitoring service. This package implements ILoggerProvider and ILogger interfaces to work natively with Microsoft.Extensions.Logging.

Features

  • Native ASP.NET Core Integration: Works seamlessly with ILogger<T> dependency injection
  • Automatic Exception Tracking: Captures and formats exceptions with full stack trace details
  • Structured Logging Support: Optional IMusculaStructuralLogger for enhanced structured data logging
  • Log Level Mapping: Maps Microsoft.Extensions.Logging levels to Muscula severity levels
  • Configuration Extensions: Simple configuration through ILoggingBuilder extensions
  • Performance Optimized: Asynchronous batch processing minimizes impact on application performance

Installation

Install the package via NuGet:

dotnet add package Muscula.Csharp.Extensions.Logging.AspNetCore

Or via Package Manager:

Install-Package Muscula.Csharp.Extensions.Logging.AspNetCore

Quick Start

Configuration in Program.cs (.NET 6+)

var builder = WebApplication.CreateBuilder(args);

// Add Muscula logging
builder.Logging.AddMuscula(settingsBuilder =>
{
    settingsBuilder.UseLogId("YOUR_MUSCULA_LOG_ID");
    return settingsBuilder.Build();
});

var app = builder.Build();

Configuration in Startup.cs (.NET Core 3.1 - 5.0)

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(builder => builder.AddMuscula(
            settingsBuilder =>
            {
                settingsBuilder.UseLogId("YOUR_MUSCULA_LOG_ID");
                return settingsBuilder.Build();
            }));
        
        // Other service configurations
    }
}

Using the Logger

Inject ILogger<T> into your controllers or services:

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

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

    [HttpGet]
    public IActionResult Get()
    {
        _logger.LogInformation("Weather endpoint called");
        
        try
        {
            // Your logic here
            return Ok(data);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get weather data");
            return StatusCode(500);
        }
    }
}

Advanced Configuration

Enable Structured Logging

builder.Logging.AddMuscula(settingsBuilder =>
{
    settingsBuilder
        .UseLogId("YOUR_MUSCULA_LOG_ID")
        .UseStructuralLogger(true) // Enable IMusculaStructuralLogger
        .UseCustomEndpoint("https://custom.muscula.endpoint.com"); // Optional custom endpoint
    return settingsBuilder.Build();
});

Using Structured Logger

When structured logging is enabled, inject IMusculaStructuralLogger:

public class OrderService
{
    private readonly ILogger<OrderService> _logger;
    private readonly IMusculaStructuralLogger _structuralLogger;

    public OrderService(ILogger<OrderService> logger, IMusculaStructuralLogger structuralLogger)
    {
        _logger = logger;
        _structuralLogger = structuralLogger;
    }

    public async Task ProcessOrderAsync(Order order)
    {
        _structuralLogger.LogStructural(
            Severity.Info,
            message: $"Processing order {order.Id}",
            structuralData: new 
            {
                OrderId = order.Id,
                CustomerId = order.CustomerId,
                Total = order.Total,
                Items = order.Items.Count
            },
            className: nameof(OrderService)
        );

        try
        {
            // Process order
        }
        catch (Exception ex)
        {
            _structuralLogger.LogStructural(
                Severity.Error,
                message: "Order processing failed",
                exception: ex,
                structuralData: order,
                className: nameof(OrderService)
            );
            throw;
        }
    }
}

Log Level Mapping

The provider maps ASP.NET Core log levels to Muscula severity:

ASP.NET Core LogLevel Muscula Severity
Trace Trace
Debug Debug
Information Info
Warning Warning
Error Error
Critical Fatal
None (not logged)

Logging with Custom Exception Data

Use MusculaException to include structured data with exceptions:

try
{
    // Your code
}
catch (Exception ex)
{
    var musculaEx = new MusculaException("Payment processing failed", ex)
    {
        StructuralData = new 
        {
            PaymentId = paymentId,
            Amount = amount,
            Currency = currency,
            Gateway = "Stripe"
        }
    };
    _logger.LogError(musculaEx, "Payment failed for order {OrderId}", orderId);
    throw musculaEx;
}

Configuration from appsettings.json

You can also configure Muscula settings from configuration:

{
  "Muscula": {
    "LogId": "YOUR_MUSCULA_LOG_ID",
    "Url": "https://api.muscula.com",
    "StructuralLogger": true
  }
}
builder.Logging.AddMuscula(settingsBuilder =>
{
    var musculaConfig = builder.Configuration.GetSection("Muscula");
    settingsBuilder
        .UseLogId(musculaConfig["LogId"])
        .UseCustomEndpoint(musculaConfig["Url"])
        .UseStructuralLogger(musculaConfig.GetValue<bool>("StructuralLogger"));
    return settingsBuilder.Build();
});

Requirements

  • .NET 10.0 or higher
  • ASP.NET Core shared framework (Microsoft.AspNetCore.App)
  • Muscula.Csharp.Extensions.Logging package
  • Active Muscula account with valid Log ID

Support

For issues and questions:

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
2.0.2 61 8/3/2026
2.0.0 356 7/23/2026
1.1.2 1,958 8/25/2025
1.1.1 4,523 8/30/2023
1.1.0 9,980 10/15/2021
1.0.3 1,460 12/4/2020
1.0.2 590 12/4/2020
1.0.1 553 12/4/2020
1.0.0 675 9/15/2020