ElmahCore

其他类别 2025-08-03

该项目是根据Apache许可证2.0的条款获得许可的。

使用ElmahCore

Elmah for Net.Standard和Net.Core(3.1,5,6)

添加Nuget软件包ElmahCore

简单用法

startup.cs

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

app.useelmah()必须在初始化其他异常处理中间件之后,例如(useexceptionhandler,useedeveloperexceptionpage等)

默认的Elmah路径〜/Elmah。

更改URL路径

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

限制访问Elmah URL

 services . AddElmah ( options =>
{
        options . OnPermissionCheck = context => context . User . Identity . IsAuthenticated ;
} ) ;

注意: app.useelmah();需要追随

app.UseAuthentication();
app.UseAuthorization();
app.UseElmah();

否则,即使用户对用户进行身份验证,也会将其重定向到屏幕上的符号。

更改错误日志类型

您可以创建自己的错误日志,该日志将在任何地方存储错误。

    class MyErrorLog : ErrorLog
    //implement ErrorLog

此错误列表中可用:

  • MemoryErrorlog - 存储错误在内存中(默认情况下)
  • XMLFileErrorlog - 将错误存储在XML文件中
  • SQLERRORLOG-将错误存储在MS SQL中(添加引用ElmahCore .sql)
  • mysqlerrorlog-将错误存储在mysql中(添加引用ElmahCore .mysql)
  • PGSQLERRORLOG-在PostgreSQL中存储错误(添加引用ElmahCore .postgresql)
 services . AddElmah < XmlFileErrorLog > ( options =>
{
    options . LogPath = "~/log" ; // OR options.LogPath = "с:errors";
} ) ; 
 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
} ) ; 

提高例外

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

microsoft.extensions.logging支持

由于2.0版ElmahCore支持Microsoft.extensions.logging

来源预览

由于版本2.0.1 ElmahCore支持源预览。只需将路径添加到源文件。

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

记录请求主体

由于版本2.0.5 ElmahCore可以记录请求主体。

记录SQL请求主体

由于版本2.0.6 ElmahCore可以记录SQL请求主体。

记录方法参数

由于版本2.0.6 ElmahCore可以记录方法参数。

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

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

使用useelmahexceptionpage

您可以替换二手exceptionpage到useelmahexceptionpage

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

使用通知器

您可以通过实现ierrornotifier或ierrornotifierWithID界面创建自己的通知符,并在Elmah选项中添加Notifier:

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

每个通知器必须具有唯一的名称。

使用过滤器

您可以在单独的文件中使用Elmah XML过滤器配置,创建并添加自定义过滤器:

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

自定义过滤器必须实现iErrorFilter。 XML过滤器配置示例:

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

在这里查看更多

JavaScript过滤器尚未吸引:(

如果您不想发送过滤错误的错误,则将通知添加到错误缩写节点,但不会发送。

搜索和过滤器

由于版本2.2.0 TOU可以使用全文搜索和多个过滤器。

分析文本字段的全文搜索工作。

可以通过添加过滤器按钮获得过滤器。

或者,您可以在错误字段右侧使用过滤器图标

当前仅支持内存和XMLFILE错误日志。

下载源码

通过命令行克隆项目:

git clone https://github.com/ElmahCore/ElmahCore.git