I am a bit confused about how to use Elmah in the ASP.NET 5 / MVC 6 projects. I got the package from nuget and it added "Elmah.Mvc": "2.1.2"
to dependencies in project.json.
我对如何在ASP中使用Elmah有点困惑。NET 5 / MVC 6项目。我从nuget那里得到了包裹,里面还加了“Elmah”。在project.json中的依赖关系。
I am not sure where to go from here - back in the day, nuget would add entries to the web.config which is now gone. And I can't seem to find any examples on their github or elsewhere.
我不知道从这里到哪里去——以前,nuget会在web上添加条目。配置,现在没有了。我似乎在他们的github或其他地方找不到任何例子。
Am I missing something simple?
我是不是漏掉了什么简单的东西?
2 个解决方案
#1
10
Instead of using ELMAH, it's not hard to implement error logging manually. This process will catch any exception that occurs in the project and log it to a database table. To do this, add the following to the Configure method in Startup.cs
与其使用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);
}
});
});
}
Include this in Startup.cs as well:
包括在启动。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 { }
}
Note that you will have to create a table in your database with the structure used in this function.
注意,您必须在数据库中创建一个具有此函数中使用的结构的表。
#2
2
ELMAH hasn't been updated to support ASP.NET Core yet. Atif Azis did some work building a web.config free configuration module called Bootstrapper. Bootstrapper doesn't support ASP.NET Core (as of my knowledge). But I'm pretty sure that work supporting the new version will start as soon as we get nearer to RTM.
ELMAH还没有更新以支持ASP。网络核心。Atif Azis做了一些建立网络的工作。无配置配置模块称为引导程序。引导程序不支持ASP。净核(据我所知)。但我很肯定,支持新版本的工作将在我们接近RTM时开始。
#1
10
Instead of using ELMAH, it's not hard to implement error logging manually. This process will catch any exception that occurs in the project and log it to a database table. To do this, add the following to the Configure method in Startup.cs
与其使用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);
}
});
});
}
Include this in Startup.cs as well:
包括在启动。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 { }
}
Note that you will have to create a table in your database with the structure used in this function.
注意,您必须在数据库中创建一个具有此函数中使用的结构的表。
#2
2
ELMAH hasn't been updated to support ASP.NET Core yet. Atif Azis did some work building a web.config free configuration module called Bootstrapper. Bootstrapper doesn't support ASP.NET Core (as of my knowledge). But I'm pretty sure that work supporting the new version will start as soon as we get nearer to RTM.
ELMAH还没有更新以支持ASP。网络核心。Atif Azis做了一些建立网络的工作。无配置配置模块称为引导程序。引导程序不支持ASP。净核(据我所知)。但我很肯定,支持新版本的工作将在我们接近RTM时开始。