ElmahCore

repository·master·Indexed 43 days ago

https://github.com/elmahcore/elmahcore

A modern implementation of ELMAH (Error Logging Modules and Handlers) for .NET Standard and .NET Core (3.1, 5, 6) applications. It provides a UI for viewing, searching, and filtering application errors, with support for various log types including Memory, XML, MS SQL, MySQL, and PostgreSQL. Features include source code preview (v2.0.1+), method parameter logging (v2.0.6+), and full-text search and advanced filtering (v2.2.0+).

Tokens
1.9K
Snippets
10
Records
11
Agent score
5%

What's inside ElmahCore

  1. Configure Error Log types

    master

    ElmahCore supports several error log implementations. You can specify the log type via the generic parameter in AddElmah<T>.

    Available built-in logs:

    • MemoryErrorLog: Stores errors in memory (default).
    • XmlFileErrorLog: Stores errors in XML files.
    • SqlErrorLog: Stores errors in MS SQL (requires ElmahCore.Sql package).
    • MysqlErrorLog: Stores errors in MySQL (requires ElmahCore.MySql package).
    • PgsqlErrorLog: Stores errors in PostgreSQL (requires ElmahCore.Postgresql package).

    You can also implement your own by inheriting from ErrorLog.

    // Example: XML File Logging
    services.AddElmah<XmlFileErrorLog>(options =>
    {
        options.LogPath = "~/log"; // Or absolute path like "C:\errors"
    });
    
    // Example: SQL Server Logging
    services.AddElmah<SqlErrorLog>(options =>
    {
        options.ConnectionString = "connection_string";
        options.SqlServerDatabaseSchemaName = "Errors"; // Defaults to dbo
        options.SqlServerDatabaseTableName = "ElmahError"; // Defaults to ELMAH_Error
    });
  2. Search and Filter errors

    master

    Since version 2.2.0, ElmahCore supports full-text search and multiple filters in the UI.

    • Full-text search: Works on analyzed text fields.
    • Filtering: You can add filters via the Add filter button or by clicking the filter icon next to an error field.

    Note: Full-text search and advanced filtering are currently only supported when using MemoryErrorLog or XmlFileErrorLog.

  3. Install and set up ElmahCore

    master

    To use ElmahCore in a .NET Standard or .NET Core (3.1, 5, 6) application, add the elmahcore NuGet package.

    In your Startup.cs:

    1. Call services.AddElmah() in ConfigureServices.
    2. Call app.UseElmah() in Configure.

    Important: app.UseElmah() must be placed after other exception handling middleware (such as UseExceptionHandler or UseDeveloperExceptionPage).

    // 1) In ConfigureServices
    services.AddElmah();
    
    // 2) In Configure
    app.UseElmah();
  4. Enable Source Preview

    master

    Since version 2.0.1, ElmahCore can preview source code in the error dashboard. To enable this, provide the local paths to your source files in the SourcePaths option.

    services.AddElmah(options =>
    {
       options.SourcePaths = new []
       {
          @"D:\tmp\ElmahCore.DemoCore3",
          @"D:\tmp\ElmahCore.Mvc",
          @"D:\tmp\ElmahCore"
       };
    });
  5. Restrict access to the Elmah URL

    master

    You can restrict access to the Elmah dashboard by providing a custom OnPermissionCheck delegate in the AddElmah options.

    Critical Middleware Order: If you use OnPermissionCheck, you must ensure app.UseAuthentication() and app.UseAuthorization() are called before app.UseElmah(). Otherwise, users will be redirected to the sign-in screen even if they are already authenticated.

    services.AddElmah(options =>
    {
        options.OnPermissionCheck = context => context.User.Identity.IsAuthenticated;
    });
    
    // Middleware order requirement:
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseElmah();
  6. Configure Error Filters

    master

    Filters allow you to control which errors are logged or which errors trigger notifications. You can use an XML configuration file or add custom filters that implement IErrorFilter.

    If you add a notifier to the <notifiers> node inside an <errorFilter> in your XML config, that specific notifier will be suppressed for errors matching that filter, but the error will still be logged.

    // Adding a custom filter via code
    services.AddElmah<XmlFileErrorLog>(options =>
    {
        options.FiltersConfig = "elmah.xml";
        options.Filters.Add(new MyFilter());
    });
    
    /* 
    Example elmah.xml content:
    
    <?xml version="1.0" encoding="utf-8" ?>
    <elmah>
    	<errorFilter>
    		<notifiers>
    			<notifier name="Email"/>
    		</notifiers>
    		<test>
    			<and>
    				<greater binding="HttpStatusCode" value="399" type="Int32" />
    				<lesser  binding="HttpStatusCode" value="500" type="Int32" />
    			</and> 
    		</test>
    	</errorFilter>
    </elmah>
    */
  7. Log method parameters

    master

    Since version 2.0.6, you can manually log method parameters to the error log using the LogParams extension method. This is useful for capturing the state of variables at the time of an error.

    using ElmahCore;
    ...
    
    public void TestMethod(string p1, int p2)
    {
        // Logging method parameters
        this.LogParams((nameof(p1), p1), (nameof(p2), p2));
        ...
    }
  8. Manually raise an exception

    master

    You can manually trigger an error log entry from within a controller or service using the RaiseError extension method on HttpContext.

    public IActionResult Test()
    {
        HttpContext.RaiseError(new InvalidOperationException("Test"));
        ...
    }
  9. Configure Notifiers

    master

    Notifiers allow you to send alerts when errors occur. You can implement IErrorNotifier or IErrorNotifierWithId to create custom notification logic. Add them to the Notifiers collection in the Elmah options.

    services.AddElmah<XmlFileErrorLog>(options =>
    {
        options.Path = @"errors";
        options.LogPath = "~/logs";
        options.Notifiers.Add(new ErrorMailNotifier("Email", emailOptions));
    });
  10. Use ElmahExceptionPage

    master

    You can use app.UseElmahExceptionPage() as a replacement for app.UseDeveloperExceptionPage(). This is useful for providing a more detailed or branded error view even in development environments.

    if (env.IsDevelopment())
    {
       // app.UseDeveloperExceptionPage();
       app.UseElmahExceptionPage();
    }