如何在ASP.NET 5/vNext/Core中使用Elmah?

8

我对如何在ASP.NET 5/MVC 6项目中使用Elmah有点困惑。我从NuGet获取了包,并将"Elmah.Mvc": "2.1.2"添加到project.json的依赖项中。

我不确定接下来该怎么做 - 以前,NuGet会将条目添加到现在已经消失的web.config中。而且我似乎找不到他们在github或其他地方的任何示例。

我是否忽略了一些简单的东西?


并不是所有的包都已经更新以与ASP.NET Core兼容。考虑到Elmah和ASP.NET堆栈之间所需的集成水平,我认为Elmah尚未更新。 - mason
可能是ELMAH on ASP.NET vNext?的重复问题。 - blowdart
1
请查看 https://github.com/ElmahCore/ElmahCore。 - Rosdi Kasim
2个回答

15

不必使用ELMAH,手动实现错误日志记录并不难。这个过程将捕获项目中发生的任何异常并将其记录到数据库表中。为此,请在Startup.cs中的Configure方法中添加以下内容:

  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
    app.UseBrowserLink();
  }
  else
  {
    app.UseExceptionHandler(builder =>
      {
        builder.Run(async context =>
        {
          context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
          context.Response.ContentType = "text/html";

          var error = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>();
          if (error != null)
          {
            LogException(error.Error, context);
            await context.Response.WriteAsync("<h2>An error has occured in the website.</h2>").ConfigureAwait(false);
          }
        });
      });
  }

同时在 Startup.cs 中包含此内容:

private void LogException(Exception error, HttpContext context)
{
  try
  {
    var connectionStr = Configuration["ConnectionString"];
    using (var connection = new System.Data.SqlClient.SqlConnection(connectionStr))
    {
      var command = connection.CreateCommand();
      command.CommandText = @"INSERT INTO ErrorLog (Application, Host, Type, Source, Path, Method, Message, StackTrace, [User],  WhenOccured)
    VALUES (@Application, @Host, @Type, @Source, @Path, @Method, @Message, @StackTrace, @User,  @WhenOccured)";
      connection.Open();

      if (error.InnerException != null)
        error = error.InnerException;

      command.Parameters.AddWithValue("@Application", this.GetType().Namespace);
      command.Parameters.AddWithValue("@Host", Environment.MachineName);
      command.Parameters.AddWithValue("@Type", error.GetType().FullName);
      command.Parameters.AddWithValue("@Source", error.Source);
      command.Parameters.AddWithValue("@Path", context.Request.Path.Value);
      command.Parameters.AddWithValue("@Method", context.Request.Method);
      command.Parameters.AddWithValue("@Message", error.Message);
      command.Parameters.AddWithValue("@StackTrace", error.StackTrace);
      var user = context.User.Identity?.Name;
      if (user == null)
        command.Parameters.AddWithValue("@User", DBNull.Value);
      else
        command.Parameters.AddWithValue("@User", user);
      command.Parameters.AddWithValue("@WhenOccured", DateTime.Now);

      command.ExecuteNonQuery();
    }
  }
  catch { }
}

请注意,您将需要在数据库中创建一个表格,并且该表格的结构必须与此函数中使用的结构相同。


嗯,当我抛出一个新的异常时似乎没有触发...我是不是没有理解这应该如何工作? - Warren LaFrance
只是猜测,你是因为在开发模式下尝试这个吗?如果是这样,它将在网页上显示错误细节而不是调用LogException。可以通过第一个代码块中的if语句进行更改。 - Rono

3

ELMAH目前还没有更新支持ASP.NET Core。Atif Aziz曾经开发了一个名为Bootstrapper的无需web.config配置的模块。据我所知,Bootstrapper并不支持ASP.NET Core。但我相信,随着我们接近RTM,支持新版本的工作很快就会开始。


如果你问我,给一个模块命名为“Bootstrapper”是愚蠢的,因为这就是熟悉的JS和CSS库的名称。 - ProfK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接