【问题标题】:How does one use Elmah in ASP.NET 5/vNext/Core?如何在 ASP.NET 5/vNext/Core 中使用 Elmah?
【发布时间】:2016-05-07 16:20:30
【问题描述】:

我对如何在 ASP.NET 5 / MVC 6 项目中使用 Elmah 感到有些困惑。我从 nuget 获得了包,它在 project.json 的依赖项中添加了 "Elmah.Mvc": "2.1.2"。

我不确定从这里去哪里 - 过去,nuget 会将条目添加到现在已经消失的 web.config 中。而且我似乎在他们的 github 或其他地方找不到任何示例。

我错过了一些简单的东西吗?

【问题讨论】:

标签: c# asp.net asp.net-core elmah elmah.mvc


【解决方案1】:

不使用 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 { }
}

请注意,您必须使用此函数中使用的结构在数据库中创建一个表。

【讨论】:

  • 嗯,当我抛出一个新异常时似乎没有触发......我不明白这是如何工作的吗?
  • 只是猜测,但是否因为您在开发模式下尝试此操作?如果是这种情况,它将在网页上显示错误详细信息,而不是调用 LogException。这可以通过第一个代码块中的 if 语句进行更改。
【解决方案2】:

ELMAH 尚未更新以支持 ASP.NET Core。 Atif Aziz 做了一些工作,构建了一个名为 Bootstrapper 的免费 web.config 配置模块。 Bootstrapper 不支持 ASP.NET Core(据我所知)。但我很确定,一旦我们接近 RTM,支持新版本的工作就会开始。

【讨论】:

  • 如果你问我,给模块命名 Bootstrapper 是愚蠢的,因为这就是熟悉的 JS 和 CSS 库的已知方式。
猜你喜欢
  • 2015-05-08
  • 1970-01-01
  • 2015-02-17
  • 2020-07-09
  • 2016-03-05
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2016-08-21
相关资源
最近更新 更多