2016-01-29 52 views
6

我对如何在ASP.NET 5/MVC 6项目中使用Elmah有点困惑。我从nuget那里得到了这个软件包,它在project.json中添加了"Elmah.Mvc": "2.1.2"到dependency。如何在ASP.NET 5/vNext/Core中使用Elmah?

我不知道该从哪里出发 - 当天回来,nuget会将条目添加到web.config中,现在已经不存在了。我似乎无法在他们的github或其他地方找到任何示例。

我是否缺少一些简单的东西?

+0

并非所有软件包都已更新为与ASP.NET Core兼容。考虑到Elmah和ASP.NET堆栈之间所需的集成水平,我会说Elmah还没有更新。 – mason

+0

[ELMAH on ASP.NET vNext?]可能的副本(http://stackoverflow.com/questions/28907017/elmah-on-asp-net-vnext) – blowdart

回答

10

不是使用ELMAH,而是手动实现错误日志记录并不困难。此过程将捕获项目中发生的任何异常并将其记录到数据库表中。要做到这一点,添加以下的配置方法Startup.cs

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 { } 
} 

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

+0

嗯,当我抛出一个新的例外时,似乎没有火......我不明白这是如何工作的? –

+0

只是猜测,但它是因为你在开发模式下尝试这个吗?如果是这样的话,它将在网页上显示错误细节,而不是调用LogException。这可以通过第一个代码块中的if语句进行更改。 – Rono

2

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

+0

如果你问我,一个模块的名字很愚蠢'Bootstrapper',因为这是熟悉的JS和CSS库。 – ProfK