Giter VIP home page Giter VIP logo

elmahcoremagic's Introduction

Build Status Latest Version Version Pre-release Last Commit

Code Quality Dependencies

License Contributors

About

This is a fork of the ElmahCoreEx project's develop branch which is a fork of the ElmahCore project, with changes for PR's that were sitting idle. It should be an almost slot in replacement for ElmahCore v2.1.2.

My long term goal is to add deletion of log entries.

See changelog.md for any changes from ElmahCoreEx develop branch.

The interfaces and namespaces have been kept the same.

Note: At this time there are no NuGet packages for this fork despite this README.md referring to them.

License

This project is licensed under the terms of the Apache license 2.0.

Warnings & Dragons

The source code for the front end appears non-existent, in ElmahCore the front end Vue SPA files are all minified. Consider this a warning sign for the continuation of the front end without a rewrite WITH SOURCE. Source-maps may have enough content to obtain the code but this has not be investigated.

Using ElmahCore

ELMAH for Net.Standard 2.0 and .Net 6

Add NuGet package ElmahCoreMagic

Simple usage

// Startup.cs
services.AddElmah() //in ConfigureServices
// ...
app.UseElmah(); // in Configure, must be positioned after initializing other exception handlers
                // such as UseExceptionHandler and UseDeveloperExceptionPage

Default ELMAH endpoint path ~/elmah.

Change URL path

services.AddElmah(options => options.Path = "you_path_here")

Restrict access to the ELMAH URL

services.AddElmah(options =>
{
    options.OnPermissionCheck = context => context.User.Identity.IsAuthenticated;
});
// startup.cs
app.UseAuthentication();
app.UseAuthorization();
//...
app.UseElmah(); // needs to be positioned after `UseAuthentication` and `UseAuthorization`

or the user will be redirected to the sign in screen even if they are authenticated.

Change Error Log type

You can implement a custom error log adapter, to write logs to alternate locations.

public class MyErrorLog: ErrorLog {
    // Implement ErrorLog
}    

The ErrorLog adapters available:

  • MemoryErrorLog – store errors in memory (by default)
  • XmlFileErrorLog – store errors in XML files.
  • SqlErrorLog - store errors in MS SQL (add reference to ElmahCoreMagic.Sql)
  • MysqlErrorLog - store errors in MySQL (add reference to ElmahCoreMagic.MySql)
  • PgsqlErrorLog - store errors in PostgreSQL (add reference to ElmahCoreMagic.Postgresql)

Example to configure for XML:

services.AddElmah<XmlFileErrorLog>(options =>
{
  options.LogPath = "~/log"; // OR options.LogPath = "с:\errors";
});

Example to configure For MSSQL

services.AddElmah<SqlErrorLog>(options =>
{
  options.ConnectionString = "connection_string";
  options.SqlServerDatabaseSchemaName = "Errors"; // Defaults to dbo if not set
  options.SqlServerDatabaseTableName = "ElmahError"; // Defaults to ELMAH_Error if not set
});

Raise exception

To raise a custom exception to log:

public IActionResult RaiseCustomExceptionExample()
{
    HttpContext.RaiseError(new InvalidOperationException("Test"));
}

Microsoft.Extensions.Logging support

ElmahCoreMagic support Microsoft.Extensions.Logging

Source Preview

ElmahCoreMagic support source preview. Add paths to source files example:

services.AddElmah(options =>
{
   options.SourcePaths = new []
   {
      @"D:\src\ElmahCore.DemoCore6",
      @"D:\src\ElmahCore.Mvc",
      @"D:\src\ElmahCore"
   };
});

Logging request body

V2.0.5+ ElmahCoreMagic can log the request body.

Logging SQL request body

v2.0.6+ ElmahCoreMagic can log the SQL request body.

Logging method parameters

V2.0.6+ ElmahCoreMagic can log method parameters.

using ElmahCore;
...

public void TestMethod(string p1, int p2)
{
    // Logging method parameters
    this.LogParams((nameof(p1), p1), (nameof(p2), p2));
    ...
}

Using UseElmahExceptionPage

You can replace UseDeveloperExceptionPage to UseElmahExceptionPage

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

Using Notifiers

You can create custom notifiers by implementing IErrorNotifier or IErrorNotifierWithId interface and add notifier to Elmah options:

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

Each notifier must have unique name.

Using Filters

You can use Elmah XML filter configuration in a separate file, create and add custom filters:

services.AddElmah<XmlFileErrorLog>(options =>
{
    options.FiltersConfig = "elmah.xml";
    options.Filters.Add(new MyFilter());
})

Custom filter must implement IErrorFilter. An XML filter config example:

<?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>

see more here

JavaScript filters have not been implemented

Add notifiers to errorFilter node if you do not want to send notifications Filtered errors will be logged, but will not be sent.

elmahcoremagic's People

Contributors

jafin avatar elmahcore avatar jtone123 avatar danieldevelops avatar conficient avatar smaglio81 avatar aidan-chang avatar chrisw-cr avatar codingdk avatar fosomo avatar giansalex avatar inhumanellama avatar marsen avatar followynne avatar mike-eason avatar nathangreaves-xplor avatar rlgnak avatar sajidkhanz avatar timmurphy avatar omuleanu avatar yyordanov avatar breggles avatar cvilla38 avatar michaelrall avatar reciko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.