问题描述
我有点困惑如何在ASP.NET 5/6 MVC项目中使用ELMAH。我接到的NuGet包并把它添加Elmah.Mvc:2.1.2
在project.json依赖
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.
我不知道在哪里何去何从 - 在当天回来,会的NuGet条目添加到web.config这是现在没有了。我似乎无法找到自己的github上或其他地方的任何实例。
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.
我失去了一些东西简单?
Am I missing something simple?
推荐答案
而不是使用ELMAH的,这不是很难手动实现错误日志。这一过程将捕获发生在项目中的任何异常并记录到一个数据库表。要做到这一点,添加以下到Startup.cs配置方法
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
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包含此还有:
Include this in Startup.cs as well:
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.
这篇关于一个人如何在ASP.NET 5 / vNext /核心使用ELMAH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!