FGV.Lib.Log.ElmahCore 1.1.0

dotnet add package FGV.Lib.Log.ElmahCore --version 1.1.0
                    
NuGet\Install-Package FGV.Lib.Log.ElmahCore -Version 1.1.0
                    
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="FGV.Lib.Log.ElmahCore" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Log.ElmahCore" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="FGV.Lib.Log.ElmahCore" />
                    
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 FGV.Lib.Log.ElmahCore --version 1.1.0
                    
#r "nuget: FGV.Lib.Log.ElmahCore, 1.1.0"
                    
#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 FGV.Lib.Log.ElmahCore@1.1.0
                    
#: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=FGV.Lib.Log.ElmahCore&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=FGV.Lib.Log.ElmahCore&version=1.1.0
                    
Install as a Cake Tool

FGV.Lib.Log.ElmahCore

A comprehensive error logging library for .NET applications that wraps and extends ElmahCore functionality. This library provides a simplified interface for logging errors to a SQL Server database, along with useful extensions and filters.

Overview

FGV.Lib.Log.ElmahCore is designed to streamline error logging in .NET applications by providing:

  • Simple integration with ASP.NET Core applications
  • SQL Server error storage
  • Customizable error filtering
  • Easy-to-use logging interface
  • Error enrichment capabilities

Installation

Add the FGV.Lib.Log.ElmahCore package to your project:

dotnet add package FGV.Lib.Log.ElmahCore

Setup

1. Register Services

In your Program.cs or Startup.cs file, add the following:

using FGV.Lib.Log.ElmahCore.Extensions;

// In ConfigureServices method:
services.AddElmahCore(options =>
{
    options.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
    options.Path = "/elmah"; // Optional: defaults to "/elmah"
    options.LogPath = "~/logs"; // Optional: for local log files if needed
});

// In Configure method:
app.UseElmahCore();

2. Configure Dependency Injection

using FGV.Lib.Log.ElmahCore;
using FGV.Lib.Log.ElmahCore.Interfaces;

// Register the logger
services.AddScoped<ICustomLogger, Logger>();

Usage

Basic Error Logging

public class ExampleService
{
    private readonly ICustomLogger _logger;

    public ExampleService(ICustomLogger logger)
    {
        _logger = logger;
    }

    public void DoSomething()
    {
        try
        {
            // Your code here
        }
        catch (Exception ex)
        {
            // Log the error
            _logger.LogError(ex, "Error occurred in ExampleService.DoSomething");
        }
    }
}

Enriching Error Logs with Additional Data

try
{
    // Your code here
}
catch (Exception ex)
{
    // Log the error with additional context
    _logger.LogError(ex, "Error processing user data", new
    {
        UserId = "123",
        Operation = "DataUpdate",
        Timestamp = DateTime.UtcNow
    });
}

Manual Error Logging

// Log an error without an exception
_logger.LogMessage("Warning: System approaching memory limits", "MemoryWarning");

// Create and log a custom exception
var customException = new InvalidOperationException("Invalid state detected");
_logger.LogError(customException, "System state error");

Configuration Options

Database Connection

In appsettings.json:

{
"ConnectionStrings": {
    "DefaultConnection": "Server=yourserver;Database=YourDb;User Id=username;Password=password;"
}
}

Error Filtering

By default, the library filters out some common errors:

  • NotFound (404) errors
  • MethodNotAllowed (405) errors

You can customize the filters by modifying the ElmahFilters.cs file or extending it with your own filters.

Custom Database Setup

If you need to manually initialize the error log database table:

using FGV.Lib.Log.ElmahCore.Helpers;

// Create the error log table
ElmahErrorLog.CreateLogTable(connectionString);

Viewing Logs

Access the Elmah error log dashboard at /elmah (or your custom path) in your application to view logged errors.

Advanced Usage

Implementing Custom Loggers

You can create your own custom logger by implementing the ICustomLogger interface:

public class MyCustomLogger : ICustomLogger
{
    public void LogError(Exception exception, string message, object additionalInfo = null)
    {
        // Your implementation here
    }

    public void LogMessage(string message, string source)
    {
        // Your implementation here
    }
}

License

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

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on FGV.Lib.Log.ElmahCore:

Package Downloads
FGV.Lib.Api

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 619 6/16/2025
1.0.0 350 5/29/2025